diff --git a/src/Concerns/TestCase.php b/src/Concerns/TestCase.php index fecd488e..6c1d98ce 100644 --- a/src/Concerns/TestCase.php +++ b/src/Concerns/TestCase.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest\Concerns; use Closure; +use Pest\Expectation; use Pest\Support\ExceptionTrace; use Pest\TestSuite; use PHPUnit\Util\Test; @@ -87,6 +88,16 @@ trait TestCase parent::tearDownAfterClass(); } + /** + * Creates a new expectation. + * + * @param mixed $value + */ + public function expect($value): Expectation + { + return new Expectation($value); + } + /** * Gets executed before the test. */ diff --git a/src/Expectation.php b/src/Expectation.php index b5d775a3..61ac5795 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -30,6 +30,16 @@ final class Expectation $this->value = $value; } + /** + * Creates a new expectation. + * + * @param mixed $value + */ + public function and($value): Expectation + { + return new self($value); + } + /** * Creates the opposite expectation for the value. */ diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index a6c9b499..613a4a97 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Pest\PendingObjects; use Closure; -use Pest\Expectation; use Pest\Factories\TestCaseFactory; use Pest\Support\Backtrace; use Pest\Support\NullClosure; @@ -13,6 +12,8 @@ use Pest\TestSuite; use SebastianBergmann\Exporter\Exporter; /** + * @method \Pest\Expectation expect(mixed $value) + * * @internal */ final class TestCall @@ -85,16 +86,6 @@ final class TestCall return $this; } - /** - * Creates a new expectation. - * - * @param mixed $value the Value - */ - public function expect($value): Expectation - { - return expect($value); - } - /** * Sets the test depends. */ diff --git a/src/globals.php b/src/globals.php index 59e5e784..7e85e9a6 100644 --- a/src/globals.php +++ b/src/globals.php @@ -110,8 +110,10 @@ function afterAll(Closure $closure = null): void * Creates a new expectation. * * @param mixed $value the Value + * + * @return Expectation */ -function expect($value): Expectation +function expect($value) { - return new Expectation($value); + return test()->expect($value); } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index e66bf634..f06d1d9f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -3,6 +3,7 @@ ✓ that gets executed PASS Tests\Expect\toBe + ✓ expect true → toBeTrue → and false → toBeFalse ✓ strict comparisons ✓ failures ✓ not failures @@ -211,9 +212,9 @@ PASS Tests\Features\HigherOrderTests ✓ it proxies calls to object - WARN Tests\Features\It + PASS Tests\Features\It ✓ it is a test - ! it is a higher order message test → This test did not perform any assertions /Users/nunomaduro/pestphp/pest/src/Factories/TestCaseFactory.php(204) : eval()'d code:4 + ✓ it is a higher order message test PASS Tests\Features\Macro ✓ it can call chained macro method @@ -223,8 +224,8 @@ ✓ it has bar PASS Tests\Features\PendingHigherOrderTests - ✓ get 'foo' → get 'bar' - ✓ get 'foo' + ✓ get 'foo' → get 'bar' → expect true → toBeTrue + ✓ get 'foo' → expect true → toBeTrue WARN Tests\Features\Skip ✓ it do not skips @@ -235,9 +236,9 @@ ✓ it do not skips with falsy closure condition - it skips with condition and message → skipped because foo - WARN Tests\Features\Test + PASS Tests\Features\Test ✓ a test - ! higher order message test → This test did not perform any assertions /Users/nunomaduro/pestphp/pest/src/Factories/TestCaseFactory.php(204) : eval()'d code:4 + ✓ higher order message test PASS Tests\Fixtures\DirectoryWithTests\ExampleTest ✓ it example 1 @@ -307,5 +308,5 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - Tests: 2 risked, 6 skipped, 178 passed + Tests: 6 skipped, 181 passed Time: 5.70s diff --git a/tests/Expect/toBe.php b/tests/Expect/toBe.php index f3f84608..ef2bce5a 100644 --- a/tests/Expect/toBe.php +++ b/tests/Expect/toBe.php @@ -2,12 +2,13 @@ use PHPUnit\Framework\ExpectationFailedException; +expect(true)->toBeTrue()->and(false)->toBeFalse(); + test('strict comparisons', function () { $nuno = new stdClass(); $dries = new stdClass(); - expect($nuno)->toBe($nuno); - expect($nuno)->not->toBe($dries); + expect($nuno)->toBe($nuno)->not->toBe($dries); }); test('failures', function () { diff --git a/tests/Features/TestCycle.php b/tests/Features/TestCycle.php index efca9588..7d3b785d 100644 --- a/tests/Features/TestCycle.php +++ b/tests/Features/TestCycle.php @@ -1,5 +1,7 @@ beforeAll = false; $foo->beforeEach = false; @@ -20,8 +22,8 @@ afterAll(function () { }); register_shutdown_function(function () use ($foo) { - expect($foo->beforeAll)->toBeFalse(); - expect($foo->beforeEach)->toBeFalse(); - expect($foo->afterEach)->toBeFalse(); - expect($foo->afterAll)->toBeFalse(); + assertFalse($foo->beforeAll); + assertFalse($foo->beforeEach); + assertFalse($foo->afterEach); + assertFalse($foo->afterAll); }); diff --git a/tests/Visual/Success.php b/tests/Visual/Success.php index a4f5fae6..14af013e 100644 --- a/tests/Visual/Success.php +++ b/tests/Visual/Success.php @@ -22,6 +22,7 @@ test('visual snapshot of test suite on success', function () { $output = explode("\n", $output()); array_pop($output); array_pop($output); + expect(file_get_contents($snapshot))->toContain(implode("\n", $output)); } })->skip(!getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE'))