From 26a6e7d7124a97e4d1e3d1945ef6b009029f08ec Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sat, 27 May 2023 14:08:38 +0100 Subject: [PATCH] More tests --- src/PendingCalls/TestCall.php | 2 +- src/Support/Backtrace.php | 32 +++++++++++++++---- tests/.snapshots/todo.txt | 8 ++--- tests/Features/AfterEach.php | 10 +++++- tests/Features/BeforeEach.php | 16 ++++++++-- tests/Features/Covers.php | 8 +++++ ...gherOrderTests.php => DescriptionLess.php} | 6 ++++ tests/Features/Incompleted.php | 4 +++ tests/Features/It.php | 8 +++++ tests/Features/Notices.php | 8 +++++ tests/Features/ThrowsNoExceptions.php | 6 ++++ tests/Features/Warnings.php | 8 +++++ 12 files changed, 101 insertions(+), 15 deletions(-) rename tests/Features/{PendingHigherOrderTests.php => DescriptionLess.php} (71%) diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 76de51f5..74d88377 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -347,7 +347,7 @@ final class TestCall { if (! is_null($this->describing)) { $this->testCaseMethod->describing = $this->describing; - $this->testCaseMethod->description = sprintf('`%s` > %s', $this->describing, $this->testCaseMethod->description); + $this->testCaseMethod->description = sprintf('`%s` → %s', $this->describing, $this->testCaseMethod->description); } $this->testSuite->tests->set($this->testCaseMethod); diff --git a/src/Support/Backtrace.php b/src/Support/Backtrace.php index 8f683ff7..729bf08d 100644 --- a/src/Support/Backtrace.php +++ b/src/Support/Backtrace.php @@ -78,9 +78,7 @@ final class Backtrace */ public static function file(): string { - $trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; - - assert(array_key_exists(self::FILE, $trace)); + $trace = self::backtrace(); return $trace[self::FILE]; } @@ -90,9 +88,7 @@ final class Backtrace */ public static function dirname(): string { - $trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; - - assert(array_key_exists(self::FILE, $trace)); + $trace = self::backtrace(); return dirname($trace[self::FILE]); } @@ -102,8 +98,30 @@ final class Backtrace */ public static function line(): int { - $trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; + $trace = self::backtrace(); return $trace['line'] ?? 0; } + + /** + * @return array{function: string, line?: int, file: string, class?: class-string, type?: string, args?: mixed[], object?: object} + */ + private static function backtrace(): array + { + $backtrace = debug_backtrace(self::BACKTRACE_OPTIONS); + + foreach ($backtrace as $trace) { + if (! isset($trace['file'])) { + continue; + } + + if (str_contains($trace['file'], 'pest/src')) { + continue; + } + + return $trace; + } + + throw ShouldNotHappen::fromMessage('Backtrace not found.'); + } } diff --git a/tests/.snapshots/todo.txt b/tests/.snapshots/todo.txt index 84a117aa..1d3687a5 100644 --- a/tests/.snapshots/todo.txt +++ b/tests/.snapshots/todo.txt @@ -9,10 +9,10 @@ TODO Tests\Features\Describe - 5 todos ↓ todo - ↓ todo on hook > should not fail - ↓ todo on hook > should run - ↓ todo on describe > should not fail - ↓ todo on describe > should run + ↓ todo on hook → should not fail + ↓ todo on hook → should run + ↓ todo on describe → should not fail + ↓ todo on describe → should run TODO Tests\Features\Todo - 3 todos ↓ something todo later diff --git a/tests/Features/AfterEach.php b/tests/Features/AfterEach.php index cdaed1d3..47d1fb60 100644 --- a/tests/Features/AfterEach.php +++ b/tests/Features/AfterEach.php @@ -7,7 +7,11 @@ beforeEach(function () use ($state) { }); afterEach(function () { - $this->state->bar = 2; + $this->state->bar = 1; +}); + +afterEach(function () { + unset($this->state->bar); }); it('does not get executed before the test', function () { @@ -18,3 +22,7 @@ it('gets executed after the test', function () { expect($this->state)->toHaveProperty('bar'); expect($this->state->bar)->toBe(2); }); + +afterEach(function () { + $this->state->bar = 2; +}); diff --git a/tests/Features/BeforeEach.php b/tests/Features/BeforeEach.php index a2e70d61..7ef6144b 100644 --- a/tests/Features/BeforeEach.php +++ b/tests/Features/BeforeEach.php @@ -4,12 +4,24 @@ beforeEach(function () { $this->bar = 2; }); +beforeEach(function () { + $this->bar++; +}); + +beforeEach(function () { + $this->bar = 0; +}); + it('gets executed before each test', function () { - expect($this->bar)->toBe(2); + expect($this->bar)->toBe(1); $this->bar = 'changed'; }); it('gets executed before each test once again', function () { - expect($this->bar)->toBe(2); + expect($this->bar)->toBe(1); +}); + +beforeEach(function () { + $this->bar++; }); diff --git a/tests/Features/Covers.php b/tests/Features/Covers.php index 12ec4ac5..31074e09 100644 --- a/tests/Features/Covers.php +++ b/tests/Features/Covers.php @@ -68,3 +68,11 @@ it('throws exception if no class nor method has been found', function () { $testCall->covers('fakeName'); })->throws(InvalidArgumentException::class, 'No class or method named "fakeName" has been found.'); + +describe('a "describe" group of tests', function () { + it('does not append CoversNothing to method attributes', function () { + $phpDoc = (new ReflectionClass($this))->getMethod($this->name()); + + expect(str_contains($phpDoc->getDocComment(), '* @coversNothing'))->toBeTrue(); + }); +})->coversNothing(); diff --git a/tests/Features/PendingHigherOrderTests.php b/tests/Features/DescriptionLess.php similarity index 71% rename from tests/Features/PendingHigherOrderTests.php rename to tests/Features/DescriptionLess.php index a22cf027..971d5cd3 100644 --- a/tests/Features/PendingHigherOrderTests.php +++ b/tests/Features/DescriptionLess.php @@ -29,3 +29,9 @@ trait Gettable get('foo'); // not incomplete because closure is created... get('foo')->get('bar')->expect(true)->toBeTrue(); get('foo')->expect(true)->toBeTrue(); + +describe('a "describe" group of tests', function () { + get('foo'); // not incomplete because closure is created... + get('foo')->get('bar')->expect(true)->toBeTrue(); + get('foo')->expect(true)->toBeTrue(); +}); diff --git a/tests/Features/Incompleted.php b/tests/Features/Incompleted.php index b32a1954..c55c1c2b 100644 --- a/tests/Features/Incompleted.php +++ b/tests/Features/Incompleted.php @@ -15,3 +15,7 @@ it('is not incompleted because of assert')->assertTrue(true); it('is not incompleted because of test with assertions', function () { expect(true)->toBeTrue(); }); + +describe('a "describe" group of tests', function () { + it('is incompleted'); +}); diff --git a/tests/Features/It.php b/tests/Features/It.php index dc483a2e..a9582b2a 100644 --- a/tests/Features/It.php +++ b/tests/Features/It.php @@ -5,3 +5,11 @@ it('is a test', function () { }); it('is a higher order message test')->expect(true)->toBeTrue(); + +describe('a "describe" group of tests', function () { + it('is a test', function () { + expect(['key' => 'foo'])->toHaveKey('key')->key->toBeString(); + }); + + it('is a higher order message test')->expect(true)->toBeTrue(); +}); diff --git a/tests/Features/Notices.php b/tests/Features/Notices.php index dd245450..fb94fcf9 100644 --- a/tests/Features/Notices.php +++ b/tests/Features/Notices.php @@ -5,3 +5,11 @@ test('notice', function () { expect(true)->toBeTrue(); }); + +describe('a "describe" group of tests', function () { + test('notice', function () { + trigger_error('This is a notice description', E_USER_NOTICE); + + expect(true)->toBeTrue(); + }); +}); diff --git a/tests/Features/ThrowsNoExceptions.php b/tests/Features/ThrowsNoExceptions.php index 98f38574..5ae0422c 100644 --- a/tests/Features/ThrowsNoExceptions.php +++ b/tests/Features/ThrowsNoExceptions.php @@ -9,3 +9,9 @@ it('allows access to the underlying expectNotToPerformAssertions method', functi it('allows performing no expectations without being risky', function () { $result = 1 + 1; })->throwsNoExceptions(); + +describe('a "describe" group of tests', function () { + it('allows performing no expectations without being risky', function () { + $result = 1 + 1; + }); +})->throwsNoExceptions(); diff --git a/tests/Features/Warnings.php b/tests/Features/Warnings.php index e638b3f0..76a51f2c 100644 --- a/tests/Features/Warnings.php +++ b/tests/Features/Warnings.php @@ -11,3 +11,11 @@ test('user warning', function () { expect(true)->toBeTrue(); }); + +describe('a "describe" group of tests', function () { + test('user warning', function () { + trigger_error('This is a warning description', E_USER_WARNING); + + expect(true)->toBeTrue(); + }); +});