From 18c54162ea356c626a2c57fdb18a500a58ddc185 Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Sat, 3 Dec 2022 18:53:58 +0200 Subject: [PATCH] Fix conditional assertions in Unit format --- src/Codeception/SuiteManager.php | 7 +++-- src/Codeception/Test/Unit.php | 7 +++-- tests/cli/OrderCest.php | 12 +++++++++ .../claypit/tests/order/CanCantFailTest.php | 26 +++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/data/claypit/tests/order/CanCantFailTest.php diff --git a/src/Codeception/SuiteManager.php b/src/Codeception/SuiteManager.php index deee4a4f9e..764674cc40 100644 --- a/src/Codeception/SuiteManager.php +++ b/src/Codeception/SuiteManager.php @@ -216,8 +216,9 @@ protected function isExecutedInCurrentEnvironment(TestInterface $test): bool protected function configureTest(TestInterface $test): void { + $di = clone($this->di); $test->getMetadata()->setServices([ - 'di' => clone($this->di), + 'di' => $di, 'dispatcher' => $this->dispatcher, 'modules' => $this->moduleContainer ]); @@ -227,9 +228,11 @@ protected function configureTest(TestInterface $test): void 'modules' => $this->moduleContainer->all() ]); if ($test instanceof TestCaseWrapper) { + $di->set(new Scenario($test)); + $testCase = $test->getTestCase(); if ($testCase instanceof Unit) { - $this->configureTest($testCase); + $testCase->setMetadata($test->getMetadata()); } } if ($test instanceof ScenarioDriven) { diff --git a/src/Codeception/Test/Unit.php b/src/Codeception/Test/Unit.php index 048f72f11e..ac1f5116ca 100644 --- a/src/Codeception/Test/Unit.php +++ b/src/Codeception/Test/Unit.php @@ -40,6 +40,11 @@ public function getMetadata(): Metadata return $this->metadata; } + public function setMetadata(?Metadata $metadata): void + { + $this->metadata = $metadata; + } + public function getResultAggregator(): ResultAggregator { throw new \LogicException('This method should not be called, TestCaseWrapper class must be used instead'); @@ -59,8 +64,6 @@ protected function _setUp() /** @var Di $di */ $di = $this->getMetadata()->getService('di'); - $di->set(new Scenario($this)); - // auto-inject $tester property if (($this->getMetadata()->getCurrent('actor')) && ($property = lcfirst(Configuration::config()['actor_suffix']))) { $this->$property = $di->instantiate($this->getMetadata()->getCurrent('actor')); diff --git a/tests/cli/OrderCest.php b/tests/cli/OrderCest.php index 48bd284b95..3b57012539 100644 --- a/tests/cli/OrderCest.php +++ b/tests/cli/OrderCest.php @@ -46,6 +46,18 @@ public function checkForCanCantFailsInCest(CliGuy $I) $I->seeFileContentsEqual("BIB([TFT][TFT])"); } + public function checkForCanCantFailsInTest(CliGuy $I) + { + $I->amInPath('tests/data/sandbox'); + $I->executeCommand('run order CanCantFailTest.php --no-exit'); + $I->seeFileFound('order.txt', 'tests/_output'); + $I->expect( + 'global bootstrap, initialization, beforeSuite, before, bootstrap, test,' + . ' fail, fail, test, test, fail, fail, test, after, afterSuite' + ); + $I->seeFileContentsEqual("BIB([TFT][TFT])"); + } + public function checkSimpleFiles(CliGuy $I) { $I->amInPath('tests/data/sandbox'); diff --git a/tests/data/claypit/tests/order/CanCantFailTest.php b/tests/data/claypit/tests/order/CanCantFailTest.php new file mode 100644 index 0000000000..c56f3f8178 --- /dev/null +++ b/tests/data/claypit/tests/order/CanCantFailTest.php @@ -0,0 +1,26 @@ +tester; + $I->appendToFile('T'); + $I->canSeeFailNow(); + $I->appendToFile('T'); + } + + public function testTwo() + { + $I = $this->tester; + $I->appendToFile('T'); + $I->canSeeFailNow(); + $I->appendToFile('T'); + } +}