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,30 @@ 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 (!isset ($ matches ['expected ' ])) {
95+ throw $ exception ;
96+ }
97+ $ expected = json_decode ($ matches ['expected ' ], true );
98+ $ notMatchedRequests = $ this ->mockserver ->getNotMatchedRequests ();
99+ $ currentSimilityRatio = 0 ;
100+ $ bestDiff = '' ;
101+ $ expectedFormatted = $ this ->formatMockServerRequest ($ expected );
102+ foreach ($ notMatchedRequests as $ notMatchedRequest ) {
103+ $ diff = DiffHelper::calculate ($ expectedFormatted , $ this ->formatMockServerRequest ($ notMatchedRequest ));
104+ $ statistics = DiffHelper::getStatistics ();
105+ $ similityRatio = $ statistics ['unmodified ' ] - $ statistics ['inserted ' ] - $ statistics ['deleted ' ];
106+ if ($ currentSimilityRatio < $ similityRatio ) {
107+ $ currentSimilityRatio = $ similityRatio ;
108+ $ bestDiff = $ diff ;
109+ }
110+ }
111+ throw new AssertionFailedError ('Impossible match request: ' . PHP_EOL . $ bestDiff );
112+ }
88113 }
89114
90115 public function seeMockRequestWasNotCalled (string $ expectationId ): void
@@ -138,4 +163,12 @@ public function createMockRequestFromJsonFile(string $expectationFile): void
138163 Assert::assertIsString ($ expectationJson );
139164 $ this ->createMockRequest ($ expectationJson );
140165 }
166+
167+ /** @param array<string, mixed> $mockServerRequest */
168+ public function formatMockServerRequest (array $ mockServerRequest ): string
169+ {
170+ ksort ($ mockServerRequest );
171+ $ requestFormatted = json_encode ($ mockServerRequest , JSON_PRETTY_PRINT );
172+ return is_string ($ requestFormatted ) ? $ requestFormatted : '' ;
173+ }
141174}
0 commit comments