refacto: structure

This commit is contained in:
Nuno Maduro
2021-12-05 14:40:08 +00:00
parent e64b6fe924
commit b1f9ce2283
17 changed files with 79 additions and 61 deletions

View File

@ -43,7 +43,7 @@ use function array_values;
use function basename;
use function class_exists;
use function get_declared_classes;
use Pest\IgnorableTestCase;
use Pest\TestCases\IgnorableTestCase;
use Pest\TestSuite;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Pest\Exceptions;
use Exception;
/**
* @internal
*/
final class ExpectationNotFound extends Exception
{
/**
* Creates a new ExpectationNotFound instance from the given name.
*/
public static function fromName(string $name): ExpectationNotFound
{
return new self("Expectation [$name] does not exist.");
}
}

View File

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Exceptions;
use Exception;
final class PipeException extends Exception
{
public static function expectationNotFound(string $expectationName): PipeException
{
return new self("Expectation $expectationName was not found in Pest");
}
}

View File

@ -5,13 +5,15 @@ declare(strict_types=1);
namespace Pest;
use BadMethodCallException;
use Carbon\Traits\Mixin;
use Closure;
use Pest\Concerns\Extendable;
use Pest\Concerns\Pipeable;
use Pest\Concerns\Retrievable;
use Pest\Exceptions\ExpectationNotFound;
use Pest\Exceptions\InvalidExpectationValue;
use Pest\Exceptions\PipeException;
use Pest\Expectations\EachExpectation;
use Pest\Expectations\HigherOrderExpectation;
use Pest\Expectations\OppositeExpectation;
use Pest\Support\ExpectationPipeline;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException;
@ -21,16 +23,16 @@ use PHPUnit\Framework\ExpectationFailedException;
*
* @template TValue
*
* @property Expectation $not Creates the opposite expectation.
* @property Each $each Creates an expectation on each element on the traversable value.
* @property Expectation $not Creates the opposite expectation.
* @property EachExpectation $each Creates an expectation on each element on the traversable value.
*
* @mixin Mixins\Expectation<TValue>
*/
final class Expectation
{
use Retrievable;
use Pipeable;
use Extendable;
use Pipeable;
use Retrievable;
/**
* Creates a new expectation.
@ -114,9 +116,9 @@ final class Expectation
/**
* Creates an expectation on each item of the iterable "value".
*
* @return Each<TValue>
* @return EachExpectation<TValue>
*/
public function each(callable $callback = null): Each
public function each(callable $callback = null): EachExpectation
{
if (!is_iterable($this->value)) {
throw new BadMethodCallException('Expectation value is not iterable.');
@ -128,7 +130,7 @@ final class Expectation
}
}
return new Each($this);
return new EachExpectation($this);
}
/**
@ -278,6 +280,11 @@ final class Expectation
return $this;
}
/**
* Creates a new expectation closure from the given name.
*
* @throws ExpectationNotFound
*/
private function getExpectationClosure(string $name): Closure
{
if (method_exists(Mixins\Expectation::class, $name)) {
@ -293,13 +300,13 @@ final class Expectation
}
}
throw PipeException::expectationNotFound($name);
throw ExpectationNotFound::fromName($name);
}
/**
* Dynamically calls methods on the class without any arguments or creates a new higher order expectation.
*
* @return Expectation<TValue>|OppositeExpectation<TValue>|Each<TValue>|HigherOrderExpectation<Expectation<TValue>, TValue|null>|TValue
* @return Expectation<TValue>|OppositeExpectation<TValue>|EachExpectation<TValue>|HigherOrderExpectation<Expectation<TValue>, TValue|null>|TValue
*/
public function __get(string $name)
{

View File

@ -2,7 +2,10 @@
declare(strict_types=1);
namespace Pest;
namespace Pest\Expectations;
use function expect;
use Pest\Expectation;
/**
* @internal
@ -11,7 +14,7 @@ namespace Pest;
*
* @mixin Expectation<TValue>
*/
final class Each
final class EachExpectation
{
private bool $opposite = false;
@ -43,7 +46,7 @@ final class Each
*
* @return self<TValue>
*/
public function not(): Each
public function not(): EachExpectation
{
$this->opposite = true;
@ -57,7 +60,7 @@ final class Each
*
* @return self<TValue>
*/
public function __call(string $name, array $arguments): Each
public function __call(string $name, array $arguments): EachExpectation
{
foreach ($this->original->value as $item) {
/* @phpstan-ignore-next-line */
@ -74,7 +77,7 @@ final class Each
*
* @return self<TValue>
*/
public function __get(string $name): Each
public function __get(string $name): EachExpectation
{
/* @phpstan-ignore-next-line */
return $this->$name();

View File

@ -2,9 +2,10 @@
declare(strict_types=1);
namespace Pest;
namespace Pest\Expectations;
use Pest\Concerns\Retrievable;
use Pest\Expectation;
/**
* @internal
@ -19,9 +20,9 @@ final class HigherOrderExpectation
use Retrievable;
/**
* @var Expectation<TValue>|Each<TValue>
* @var Expectation<TValue>|EachExpectation<TValue>
*/
private Expectation|Each $expectation;
private Expectation|EachExpectation $expectation;
private bool $opposite = false;

View File

@ -2,8 +2,9 @@
declare(strict_types=1);
namespace Pest;
namespace Pest\Expectations;
use Pest\Expectation;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Exporter\Exporter;

View File

@ -150,7 +150,7 @@ final class TestCaseFactory
eval("
namespace $namespace;
use Pest\Datasets as __PestDatasets;
use Pest\Repositories\DatasetsRepository as __PestDatasets;
use Pest\TestSuite as __PestTestSuite;
final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {

View File

@ -5,9 +5,9 @@ declare(strict_types=1);
namespace Pest\Factories;
use Closure;
use Pest\Datasets;
use Pest\Exceptions\ShouldNotHappen;
use Pest\Factories\Concerns\HigherOrderable;
use Pest\Repositories\DatasetsRepository;
use Pest\Support\Str;
use Pest\TestSuite;
use PHPUnit\Framework\Assert;
@ -154,7 +154,7 @@ final class TestCaseMethodFactory
*/
private function buildDatasetForEvaluation(string $methodName, string $dataProviderName): string
{
Datasets::with($this->filename, $methodName, $this->datasets);
DatasetsRepository::with($this->filename, $methodName, $this->datasets);
return <<<EOF

View File

@ -2,12 +2,12 @@
declare(strict_types=1);
use Pest\Datasets;
use Pest\Expectation;
use Pest\PendingCalls\AfterEachCall;
use Pest\PendingCalls\BeforeEachCall;
use Pest\PendingCalls\TestCall;
use Pest\PendingCalls\UsesCall;
use Pest\Repositories\DatasetsRepository;
use Pest\Support\Backtrace;
use Pest\Support\HigherOrderTapProxy;
use Pest\TestSuite;
@ -61,7 +61,7 @@ if (!function_exists('dataset')) {
*/
function dataset(string $name, Closure|iterable $dataset): void
{
Datasets::set($name, $dataset);
DatasetsRepository::set($name, $dataset);
}
}

View File

@ -42,7 +42,7 @@ final class PestDatasetCommand extends Command
/** @var string $name */
$name = $this->argument('name');
$relativePath = sprintf(testDirectory('Datasets/%s.php'), ucfirst($name));
$relativePath = sprintf(testDirectory('DatasetsRepository/%s.php'), ucfirst($name));
/* @phpstan-ignore-next-line */
$target = base_path($relativePath);

View File

@ -7,8 +7,7 @@ namespace Pest;
final class Plugin
{
/**
* The lazy callables to be executed
* once the test suite boots.
* The lazy callables to be executed once the test suite boots.
*
* @var array<int, callable>
*

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Pest;
namespace Pest\Repositories;
use Closure;
use Pest\Exceptions\DatasetAlreadyExist;
@ -15,7 +15,7 @@ use Traversable;
/**
* @internal
*/
final class Datasets
final class DatasetsRepository
{
/**
* Holds the datasets.

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Pest;
namespace Pest\TestCases;
use PHPUnit\Framework\TestCase;

View File

@ -1,9 +1,9 @@
<?php
use Pest\Datasets;
use Pest\Exceptions\DatasetAlreadyExist;
use Pest\Exceptions\DatasetDoesNotExist;
use Pest\Plugin;
use Pest\Repositories\DatasetsRepository;
beforeEach(function () {
$this->foo = 'bar';
@ -13,28 +13,28 @@ it('throws exception if dataset does not exist', function () {
$this->expectException(DatasetDoesNotExist::class);
$this->expectExceptionMessage("A dataset with the name `first` does not exist. You can create it using `dataset('first', ['a', 'b']);`.");
Datasets::resolve('foo', ['first']);
DatasetsRepository::resolve('foo', ['first']);
});
it('throws exception if dataset already exist', function () {
Datasets::set('second', [[]]);
DatasetsRepository::set('second', [[]]);
$this->expectException(DatasetAlreadyExist::class);
$this->expectExceptionMessage('A dataset with the name `second` already exist.');
Datasets::set('second', [[]]);
DatasetsRepository::set('second', [[]]);
});
it('sets closures', function () {
Datasets::set('foo', function () {
DatasetsRepository::set('foo', function () {
yield [1];
});
expect(Datasets::resolve('foo', ['foo']))->toBe(['foo with (1)' => [1]]);
expect(DatasetsRepository::resolve('foo', ['foo']))->toBe(['foo with (1)' => [1]]);
});
it('sets arrays', function () {
Datasets::set('bar', [[2]]);
DatasetsRepository::set('bar', [[2]]);
expect(Datasets::resolve('bar', ['bar']))->toBe(['bar with (2)' => [2]]);
expect(DatasetsRepository::resolve('bar', ['bar']))->toBe(['bar with (2)' => [2]]);
});
it('gets bound to test case object', function () {

View File

@ -2,6 +2,7 @@
declare(strict_types=1);
use Pest\Exceptions\ExpectationNotFound;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertEqualsIgnoringCase;
use function PHPUnit\Framework\assertInstanceOf;

View File

@ -1,9 +1,9 @@
<?php
use Pest\Datasets;
use Pest\Repositories\DatasetsRepository;
it('show only the names of named datasets in their description', function () {
$descriptions = array_keys(Datasets::resolve('test description', [
$descriptions = array_keys(DatasetsRepository::resolve('test description', [
[
'one' => [1],
'two' => [[2]],
@ -15,7 +15,7 @@ it('show only the names of named datasets in their description', function () {
});
it('show the actual dataset of non-named datasets in their description', function () {
$descriptions = array_keys(Datasets::resolve('test description', [
$descriptions = array_keys(DatasetsRepository::resolve('test description', [
[
[1],
[[2]],
@ -27,7 +27,7 @@ it('show the actual dataset of non-named datasets in their description', functio
});
it('show only the names of multiple named datasets in their description', function () {
$descriptions = array_keys(Datasets::resolve('test description', [
$descriptions = array_keys(DatasetsRepository::resolve('test description', [
[
'one' => [1],
'two' => [[2]],
@ -45,7 +45,7 @@ it('show only the names of multiple named datasets in their description', functi
});
it('show the actual dataset of multiple non-named datasets in their description', function () {
$descriptions = array_keys(Datasets::resolve('test description', [
$descriptions = array_keys(DatasetsRepository::resolve('test description', [
[
[1],
[[2]],
@ -63,7 +63,7 @@ it('show the actual dataset of multiple non-named datasets in their description'
});
it('show the correct description for mixed named and not-named datasets', function () {
$descriptions = array_keys(Datasets::resolve('test description', [
$descriptions = array_keys(DatasetsRepository::resolve('test description', [
[
'one' => [1],
[[2]],