Repository: Codeception/Codeception (php)

Issue:
`LogicException` thrown when `canSeeResponseContainsJson` partially matches
#### What are you trying to achieve?

I attempt to run my API tests and match JSON with `canSeeResponseContainsJson`.

#### What do you get instead?

When I run an API test against a dataset where a return value is random, i.e. cannot be hard-coded as a result, I use `canSeeResponseContainsJson` and remove the random property from the file JSON entirely. If I don't remove the random property, I would expect the test to simply fail and tell me the data does not match, but instead it gives me this error, which is pretty misleading. It didn't use to do this, so it must have broken recently.

```bash
1) MyTestClass: Test_name
 Test  path/to/test/MyTestClass.php:Test_name
                                                                                                 
  [LogicException] This method should not be called, TestCaseWrapper class must be used instead  
                                                                                                 
/var/www/html/vendor/codeception/codeception/src/Codeception/Test/Unit.php:45
/var/www/html/vendor/codeception/codeception/src/Codeception/Scenario.php:77
/var/www/html/app/tests/_support/_generated/ApiTesterActions.php:588
/var/www/html/app/tests/path/to/test/MyTestClass.php:1260
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php:1548
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php:1154
/var/www/html/vendor/codeception/codeception/src/Codeception/Test/TestCaseWrapper.php:133
/var/www/html/vendor/codeception/codeception/src/Codeception/Test/Test.php:169
/var/www/html/vendor/codeception/codeception/src/Codeception/Suite.php:130
/var/www/html/vendor/codeception/codeception/src/Codeception/SuiteManager.php:148
/var/www/html/vendor/codeception/codeception/src/Codeception/Codecept.php:260
/var/www/html/vendor/codeception/codeception/src/Codeception/Codecept.php:216
/var/www/html/vendor/codeception/codeception/src/Codeception/Command/Run.php:646
/var/www/html/vendor/codeception/codeception/src/Codeception/Command/Run.php:467
/var/www/html/vendor/symfony/console/Command/Command.php:308
/var/www/html/vendor/symfony/console/Application.php:1014
/var/www/html/vendor/symfony/console/Application.php:301
/var/www/html/vendor/symfony/console/Application.php:171
/var/www/html/vendor/codeception/codeception/src/Codeception/Application.php:112
/var/www/html/vendor/codeception/codeception/app.php:45
/var/www/html/vendor/codeception/codeception/app.php:46
/var/www/html/vendor/codeception/codeception/codecept:7
/var/www/html/vendor/bin/codecept:120
```

```php
public function test_name()
{
  // paths etc redacted for privacy
  $this->tester->sendPOST("api_path", [
    'data' => 'data'
  ]);
  $this->tester->seeResponseCodeIs(200);
  $this->tester->canSeeResponseContainsJson(
    json_decode(file_get_contents(__DIR__ . '/path/to/json.json'), true) // This is line 1260 from the trace above
  );
}
```

`'/path/to/json.json'` content:
```json
{
  "id" : "9",
  "available" : true,
  "currency" : "USD",
  "date" : "2031-04-29",
  "sales_deadline" : 1935216000,
  "link_id" : "RANDOM NON-MATCHING STRING GOES HERE"
}
```
### Details

* Codeception version: 5.0.5
* PHP Version: 8.1.12 on Apache, JIT disabled
* Operating System: Linux
* Installation type: composer
* List of installed packages (`composer show`): n/a
* Suite configuration:

```yml
paths:
  tests: app/tests
  output: app/tests/_output
  data: app/tests/_data
  support: app/tests/_support
  envs: app/tests/_envs
actor_suffix: Tester
settings:
  colors: true
  memory_limit: 1024M
params:
  - app/.env
```


Pull request description:
Fix conditional assertions in Unit format
Fixes #6608 

Patch:
diff --git a/src/Codeception/SuiteManager.php b/src/Codeception/SuiteManager.php
index deee4a4f9..764674cc4 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) {


Test patch:
diff --git a/src/Codeception/Test/Unit.php b/src/Codeception/Test/Unit.php
index 048f72f11..ac1f5116c 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 48bd284b9..3b5701253 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 000000000..c56f3f817
--- /dev/null
+++ b/tests/data/claypit/tests/order/CanCantFailTest.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Order;
+
+use OrderGuy;
+
+class CanCantFailTest extends \Codeception\Test\Unit
+{
+    protected OrderGuy $tester;
+
+    public function testOne()
+    {
+        $I = $this->tester;
+        $I->appendToFile('T');
+        $I->canSeeFailNow();
+        $I->appendToFile('T');
+    }
+
+    public function testTwo()
+    {
+        $I = $this->tester;
+        $I->appendToFile('T');
+        $I->canSeeFailNow();
+        $I->appendToFile('T');
+    }
+}