1010use DEVizzent \CodeceptionMockServerHelper \Config \ExpectationsPath ;
1111use DEVizzent \CodeceptionMockServerHelper \Config \NotMatchedRequest ;
1212use GuzzleHttp \Client ;
13+ use Jfcherng \Diff \DiffHelper ;
1314use PHPUnit \Framework \Assert ;
1415use PHPUnit \Framework \AssertionFailedError ;
1516use PHPUnit \Framework \ExpectationFailedException ;
@@ -25,6 +26,7 @@ class MockServerHelper extends Module
2526 private CleanUpBefore $ cleanUpBefore ;
2627 private NotMatchedRequest $ notMatchedRequest ;
2728 private ExpectationsPath $ expectationPath ;
29+
2830 /** @param array<string, string>|null $config */
2931 public function __construct (ModuleContainer $ moduleContainer , ?array $ config = null )
3032 {
@@ -49,7 +51,7 @@ public function _initialize(): void
4951 $ this ->expectationPath = new ExpectationsPath ($ this ->config [self ::CONFIG_EXPECTATIONS_PATH ]);
5052 }
5153 $ this ->mockserver = new MockServer (new Client ([
52- 'base_uri ' => $ this ->config [self ::CONFIG_URL ]
54+ 'base_uri ' => $ this ->config [self ::CONFIG_URL ]
5355 ]));
5456 if ($ this ->notMatchedRequest ->isEnabled ()) {
5557 $ this ->createMockRequestFromJsonFile (__DIR__ . '/not-matched-request.json ' );
@@ -84,7 +86,29 @@ public function _before(TestInterface $test): void
8486
8587 public function seeMockRequestWasCalled (string $ expectationId , ?int $ times = null ): void
8688 {
87- $ this ->mockserver ->verify ($ expectationId , $ times );
89+ try {
90+ $ this ->mockserver ->verify ($ expectationId , $ times );
91+ } catch (AssertionFailedError $ exception ) {
92+ //throw $exception;
93+ preg_match ('#(.|\n)* expected:<(?<expected>\{(.|\n)*\})> but was:<(.|\n)*># ' , $ exception ->getMessage (), $ matches );
94+ if (empty ($ matches )) {
95+ throw $ exception ;
96+ }
97+ $ expected = json_decode ($ matches ['expected ' ], true );
98+ $ notMatchedRequests = $ this ->mockserver ->getNotMatchedRequests ();
99+ $ currentSimilityRatio = 0 ;
100+ $ expectedFormatted = $ this ->formatMockServerRequest ($ expected );
101+ foreach ($ notMatchedRequests as $ notMatchedRequest ) {
102+ $ diff = DiffHelper::calculate ($ expectedFormatted , $ this ->formatMockServerRequest ($ notMatchedRequest ));
103+ $ statistics = DiffHelper::getStatistics ();
104+ $ similityRatio = $ statistics ['unmodified ' ] - $ statistics ['inserted ' ] - $ statistics ['deleted ' ];
105+ if ($ currentSimilityRatio < $ similityRatio ) {
106+ $ currentSimilityRatio = $ similityRatio ;
107+ $ bestDiff = $ diff ;
108+ }
109+ }
110+ throw new AssertionFailedError ('Impossible match request: ' . PHP_EOL . $ bestDiff );
111+ }
88112 }
89113
90114 public function seeMockRequestWasNotCalled (string $ expectationId ): void
@@ -138,4 +162,10 @@ public function createMockRequestFromJsonFile(string $expectationFile): void
138162 Assert::assertIsString ($ expectationJson );
139163 $ this ->createMockRequest ($ expectationJson );
140164 }
165+
166+ public function formatMockServerRequest (array $ mockServerRequest ): string
167+ {
168+ ksort ($ mockServerRequest );
169+ return json_encode ($ mockServerRequest , JSON_PRETTY_PRINT );
170+ }
141171}
0 commit comments