diff --git a/src/Expectation.php b/src/Expectation.php new file mode 100644 index 00000000..2afe4088 --- /dev/null +++ b/src/Expectation.php @@ -0,0 +1,65 @@ +value = $value; + } + + /** + * Creates the opposite expectation for the value. + */ + public function not(): OppositeExpectation + { + return new OppositeExpectation($this); + } + + /** + * Asserts that two variables have the same type and + * value. Used on objects, it asserts that two + * variables reference the same object. + * + * @param mixed $value + */ + public function toBe($value): Expectation + { + Assert::assertSame($value, $this->value); + + return $this; + } + + /** + * Dynamically calls methods on the class without any arguments. + * + * @return Expectation + */ + public function __get(string $name) + { + /* @phpstan-ignore-next-line */ + return $this->{$name}(); + } +} diff --git a/src/OppositeExpectation.php b/src/OppositeExpectation.php new file mode 100644 index 00000000..08a7ebc6 --- /dev/null +++ b/src/OppositeExpectation.php @@ -0,0 +1,45 @@ +original = $original; + } + + /** + * Handle dynamic method calls into the original expectation. + * + * @param array $arguments + */ + public function __call(string $name, array $arguments): Expectation + { + try { + /* @phpstan-ignore-next-line */ + $this->original->{$name}(...$arguments); + } catch (ExpectationFailedException $e) { + return $this->original; + } + + throw new ExpectationFailedException(sprintf('@todo')); + } +} diff --git a/src/globals.php b/src/globals.php index a15f6bea..07ec9d6e 100644 --- a/src/globals.php +++ b/src/globals.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Pest\Datasets; +use Pest\Expectation; use Pest\PendingObjects\AfterEachCall; use Pest\PendingObjects\BeforeEachCall; use Pest\PendingObjects\TestCall; @@ -104,3 +105,11 @@ function afterAll(Closure $closure = null): void { TestSuite::getInstance()->afterAll->set($closure); } + +/** + * Creates a new expectation. + */ +function expect($value): Expectation +{ + return new Expectation($value); +} diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 09e41270..bcff1d84 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2,6 +2,11 @@ PASS Tests\CustomTestCase\ExecutedTest ✓ that gets executed + PASS Tests\Expect\ToBe + ✓ strict comparisons + ✓ failures + ✓ not failures + PASS Tests\Features\AfterAll ✓ deletes file after all @@ -167,5 +172,5 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - Tests: 6 skipped, 96 passed - Time: 3.43s + Tests: 6 skipped, 99 passed + Time: 3.62s diff --git a/tests/Expect/ToBe.php b/tests/Expect/ToBe.php new file mode 100644 index 00000000..f3f84608 --- /dev/null +++ b/tests/Expect/ToBe.php @@ -0,0 +1,19 @@ +toBe($nuno); + expect($nuno)->not->toBe($dries); +}); + +test('failures', function () { + expect(1)->toBe(2); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(1)->not->toBe(1); +})->throws(ExpectationFailedException::class);