From 1b117990ce0911db2e564cd30429cedeee2d7808 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Thu, 12 Jan 2023 21:53:20 +0100 Subject: [PATCH 1/5] Migrate PHPUnit config --- phpunit.xml.dist | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9c251d1..b844b9f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,15 +1,18 @@ - - - - - ./tests/ - - - - - - ./src - - + + + + ./tests/ + + + + + ./src + + From 37818dab0f5bb162612865b78cf526db3a2358c2 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Thu, 12 Jan 2023 15:35:27 +0100 Subject: [PATCH 2/5] Migrate test suite to E2E approach using PHPT tests Migrate all tests to PHPT end to end tests --- fixtures/{Error.php => WrongCall.php} | 2 +- phpunit.xml.dist | 3 +- tests/MockFailure.phpt | 30 +++++++++ tests/NoProphecy.phpt | 17 +++++ tests/ProphecyTraitTest.php | 90 --------------------------- tests/SpyFailure.phpt | 25 ++++++++ tests/Success.phpt | 17 +++++ tests/WrongCall.phpt | 25 ++++++++ xdebug_filter.php | 13 ++++ 9 files changed, 130 insertions(+), 92 deletions(-) rename fixtures/{Error.php => WrongCall.php} (90%) create mode 100644 tests/MockFailure.phpt create mode 100644 tests/NoProphecy.phpt delete mode 100644 tests/ProphecyTraitTest.php create mode 100644 tests/SpyFailure.phpt create mode 100644 tests/Success.phpt create mode 100644 tests/WrongCall.phpt create mode 100644 xdebug_filter.php diff --git a/fixtures/Error.php b/fixtures/WrongCall.php similarity index 90% rename from fixtures/Error.php rename to fixtures/WrongCall.php index e951786..4327fdc 100644 --- a/fixtures/Error.php +++ b/fixtures/WrongCall.php @@ -5,7 +5,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; -class Error extends TestCase +class WrongCall extends TestCase { use ProphecyTrait; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b844b9f..724dfd1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,8 @@ ./tests/ - + ./tests/ + diff --git a/tests/MockFailure.phpt b/tests/MockFailure.phpt new file mode 100644 index 0000000..c73d423 --- /dev/null +++ b/tests/MockFailure.phpt @@ -0,0 +1,30 @@ +--TEST-- +A test with a mock fails due to a missing expected call +--FILE-- +run(['phpunit', 'fixtures/MockFailure.php'], false); +--EXPECTF-- +PHPUnit %s + +F %s 1 / 1 (100%) + +Time: %s + +There was 1 failure: + +1) Prophecy\PhpUnit\Tests\Fixtures\MockFailure::testMethod +Some predictions failed: +Double\DateTime\P1: + Expected exactly 2 calls that match: + Double\DateTime\P1->format(exact("Y-m-d")) + but 1 were made: + - format("Y-m-d") @ fixtures/MockFailure.php:%d + +%s/src/ProphecyTrait.php:%d + +FAILURES! +Tests: 1, Assertions: 1, Failures: 1. diff --git a/tests/NoProphecy.phpt b/tests/NoProphecy.phpt new file mode 100644 index 0000000..c3a7e69 --- /dev/null +++ b/tests/NoProphecy.phpt @@ -0,0 +1,17 @@ +--TEST-- +A test without Prophecy is executed with no additional assertions counted +--FILE-- +run(['phpunit', 'fixtures/NoProphecy.php'], false); +--EXPECTF-- +PHPUnit %s + +. %s 1 / 1 (100%) + +Time: %s + +OK (1 test, 1 assertion) diff --git a/tests/ProphecyTraitTest.php b/tests/ProphecyTraitTest.php deleted file mode 100644 index aec18a3..0000000 --- a/tests/ProphecyTraitTest.php +++ /dev/null @@ -1,90 +0,0 @@ -run(); - - $this->assertSame(0, $result->errorCount()); - $this->assertSame(0, $result->failureCount()); - $this->assertCount(1, $result); - $this->assertSame(1, $test->getNumAssertions()); - $this->assertSame(BaseTestRunner::STATUS_PASSED, $test->getStatus()); - } - - public function testSpyPredictionFailure(): void - { - $test = new SpyFailure('testMethod'); - - $result = $test->run(); - - $this->assertSame(0, $result->errorCount()); - $this->assertSame(1, $result->failureCount()); - $this->assertCount(1, $result); - $this->assertSame(1, $test->getNumAssertions()); - $this->assertSame(BaseTestRunner::STATUS_FAILURE, $test->getStatus()); - } - - public function testMockPredictionFailure(): void - { - $test = new MockFailure('testMethod'); - - $result = $test->run(); - - $this->assertSame(0, $result->errorCount()); - $this->assertSame(1, $result->failureCount()); - $this->assertCount(1, $result); - $this->assertSame(1, $test->getNumAssertions()); - $this->assertSame(BaseTestRunner::STATUS_FAILURE, $test->getStatus()); - } - - public function testDoublingError(): void - { - $test = new Error('testMethod'); - - $result = $test->run(); - - $this->assertSame(1, $result->errorCount()); - $this->assertSame(0, $result->failureCount()); - $this->assertCount(1, $result); - $this->assertSame(0, $test->getNumAssertions()); - $this->assertSame(BaseTestRunner::STATUS_ERROR, $test->getStatus()); - } - - public function testNoProphecy(): void - { - $test = new NoProphecy('testMethod'); - - $result = $test->run(); - - $this->assertSame(0, $result->errorCount()); - $this->assertSame(0, $result->failureCount()); - $this->assertCount(1, $result); - $this->assertSame(1, $test->getNumAssertions()); - $this->assertSame(BaseTestRunner::STATUS_PASSED, $test->getStatus()); - } -} diff --git a/tests/SpyFailure.phpt b/tests/SpyFailure.phpt new file mode 100644 index 0000000..a2efd64 --- /dev/null +++ b/tests/SpyFailure.phpt @@ -0,0 +1,25 @@ +--TEST-- +A test with a spy fails due to expected call not made +--FILE-- +run(['phpunit', 'fixtures/SpyFailure.php'], false); +--EXPECTF-- +PHPUnit %s + +F %s 1 / 1 (100%) + +Time: %a + +There was 1 failure: + +1) Prophecy\PhpUnit\Tests\Fixtures\SpyFailure::testMethod +No calls have been made that match: + Double\DateTime\P1->format(exact("Y-m-d")) +but expected at least one. + +FAILURES! +Tests: 1, Assertions: 1, Failures: 1. diff --git a/tests/Success.phpt b/tests/Success.phpt new file mode 100644 index 0000000..7fda117 --- /dev/null +++ b/tests/Success.phpt @@ -0,0 +1,17 @@ +--TEST-- +A test with a mock is executed successfully +--FILE-- +run(['phpunit', 'fixtures/Success.php'], false); +--EXPECTF-- +PHPUnit %s + +. %s 1 / 1 (100%) + +Time: %s + +OK (1 test, 1 assertion) diff --git a/tests/WrongCall.phpt b/tests/WrongCall.phpt new file mode 100644 index 0000000..81fab7d --- /dev/null +++ b/tests/WrongCall.phpt @@ -0,0 +1,25 @@ +--TEST-- +A test fails due to calling an unexisting method on a mock +--FILE-- +run(['phpunit', 'fixtures/WrongCall.php'], false); +--EXPECTF-- +PHPUnit %s + +E %s 1 / 1 (100%) + +Time: %a + +There was 1 error: + +1) Prophecy\PhpUnit\Tests\Fixtures\WrongCall::testMethod +Prophecy\Exception\Doubler\MethodNotFoundException: Method `Double\stdClass\P1::talk()` is not defined. + +%a%s/fixtures/WrongCall.php:%d + +ERRORS! +Tests: 1, Assertions: 0, Errors: 1. diff --git a/xdebug_filter.php b/xdebug_filter.php new file mode 100644 index 0000000..6cb12dd --- /dev/null +++ b/xdebug_filter.php @@ -0,0 +1,13 @@ + Date: Fri, 13 Jan 2023 11:33:40 +0100 Subject: [PATCH 3/5] Refactor test to have a centralized script to execute test fixtures --- tests/MockFailure.phpt | 6 +++--- tests/NoProphecy.phpt | 5 ++--- tests/SpyFailure.phpt | 7 ++++--- tests/Success.phpt | 5 ++--- tests/WrongCall.phpt | 6 +++--- tests/run_test.php | 18 ++++++++++++++++++ 6 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 tests/run_test.php diff --git a/tests/MockFailure.phpt b/tests/MockFailure.phpt index c73d423..b80a873 100644 --- a/tests/MockFailure.phpt +++ b/tests/MockFailure.phpt @@ -3,10 +3,9 @@ A test with a mock fails due to a missing expected call --FILE-- run(['phpunit', 'fixtures/MockFailure.php'], false); +\Prophecy\PhpUnit\Tests\runTest('MockFailure'); --EXPECTF-- PHPUnit %s @@ -25,6 +24,7 @@ Double\DateTime\P1: - format("Y-m-d") @ fixtures/MockFailure.php:%d %s/src/ProphecyTrait.php:%d +%s/tests/run_test.php:%d FAILURES! Tests: 1, Assertions: 1, Failures: 1. diff --git a/tests/NoProphecy.phpt b/tests/NoProphecy.phpt index c3a7e69..42d5228 100644 --- a/tests/NoProphecy.phpt +++ b/tests/NoProphecy.phpt @@ -3,10 +3,9 @@ A test without Prophecy is executed with no additional assertions counted --FILE-- run(['phpunit', 'fixtures/NoProphecy.php'], false); +\Prophecy\PhpUnit\Tests\runTest('NoProphecy'); --EXPECTF-- PHPUnit %s diff --git a/tests/SpyFailure.phpt b/tests/SpyFailure.phpt index a2efd64..532454c 100644 --- a/tests/SpyFailure.phpt +++ b/tests/SpyFailure.phpt @@ -3,10 +3,9 @@ A test with a spy fails due to expected call not made --FILE-- run(['phpunit', 'fixtures/SpyFailure.php'], false); +\Prophecy\PhpUnit\Tests\runTest('SpyFailure'); --EXPECTF-- PHPUnit %s @@ -21,5 +20,7 @@ No calls have been made that match: Double\DateTime\P1->format(exact("Y-m-d")) but expected at least one. +%s/tests/run_test.php:%d + FAILURES! Tests: 1, Assertions: 1, Failures: 1. diff --git a/tests/Success.phpt b/tests/Success.phpt index 7fda117..84a7c64 100644 --- a/tests/Success.phpt +++ b/tests/Success.phpt @@ -3,10 +3,9 @@ A test with a mock is executed successfully --FILE-- run(['phpunit', 'fixtures/Success.php'], false); +\Prophecy\PhpUnit\Tests\runTest('Success'); --EXPECTF-- PHPUnit %s diff --git a/tests/WrongCall.phpt b/tests/WrongCall.phpt index 81fab7d..0485d3f 100644 --- a/tests/WrongCall.phpt +++ b/tests/WrongCall.phpt @@ -3,10 +3,9 @@ A test fails due to calling an unexisting method on a mock --FILE-- run(['phpunit', 'fixtures/WrongCall.php'], false); +\Prophecy\PhpUnit\Tests\runTest('WrongCall'); --EXPECTF-- PHPUnit %s @@ -20,6 +19,7 @@ There was 1 error: Prophecy\Exception\Doubler\MethodNotFoundException: Method `Double\stdClass\P1::talk()` is not defined. %a%s/fixtures/WrongCall.php:%d +%s/tests/run_test.php:%d ERRORS! Tests: 1, Assertions: 0, Errors: 1. diff --git a/tests/run_test.php b/tests/run_test.php new file mode 100644 index 0000000..1b3b458 --- /dev/null +++ b/tests/run_test.php @@ -0,0 +1,18 @@ +run(['phpunit', $filename], false); +} From 709d60edf1417b015eed7b2bfa0ff11e315ed003 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 13 Jan 2023 11:59:15 +0100 Subject: [PATCH 4/5] Move xdebug_filter.php into tests folder --- tests/run_test.php | 2 +- xdebug_filter.php => tests/xdebug_filter.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename xdebug_filter.php => tests/xdebug_filter.php (59%) diff --git a/tests/run_test.php b/tests/run_test.php index 1b3b458..71540d3 100644 --- a/tests/run_test.php +++ b/tests/run_test.php @@ -4,7 +4,7 @@ use PHPUnit\TextUI\Command; -require_once dirname(__DIR__) . '/xdebug_filter.php'; +require_once __DIR__ . '/xdebug_filter.php'; require_once dirname(__DIR__) . '/vendor/autoload.php'; function runTest(string $fixtureName): void diff --git a/xdebug_filter.php b/tests/xdebug_filter.php similarity index 59% rename from xdebug_filter.php rename to tests/xdebug_filter.php index 6cb12dd..6e4279d 100644 --- a/xdebug_filter.php +++ b/tests/xdebug_filter.php @@ -3,11 +3,11 @@ xdebug_set_filter( XDEBUG_FILTER_CODE_COVERAGE, XDEBUG_PATH_INCLUDE, - [ __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR ] + [ dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR ] ); xdebug_set_filter( XDEBUG_FILTER_CODE_COVERAGE, XDEBUG_PATH_EXCLUDE, - [ __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR ] + [ dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR ] ); } From 86437cec302a30a87d13b7c5623e942f0f1ca61f Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Fri, 13 Jan 2023 11:59:29 +0100 Subject: [PATCH 5/5] Remove older test references --- phpunit.xml.dist | 1 - 1 file changed, 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 724dfd1..7898f95 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,7 +7,6 @@ > - ./tests/ ./tests/