55use Codeception \Lib \ModuleContainer ;
66use Codeception \Module ;
77use Codeception \TestInterface ;
8+ use DEVizzent \CodeceptionMockServerHelper \Client \MockServer ;
89use DEVizzent \CodeceptionMockServerHelper \Config \CleanUpBefore ;
910use DEVizzent \CodeceptionMockServerHelper \Config \NotMatchedRequest ;
1011use GuzzleHttp \Client ;
@@ -18,7 +19,7 @@ class MockServerHelper extends Module
1819 private const CONFIG_URL = 'url ' ;
1920 private const CONFIG_CLEANUP_BEFORE = 'cleanupBefore ' ;
2021 public const NOT_MATCHED_REQUEST_ID = 'not-matched-request ' ;
21- private Client $ mockserverClient ;
22+ private MockServer $ mockserver ;
2223 private CleanUpBefore $ cleanUpBefore ;
2324 private NotMatchedRequest $ notMatchedRequest ;
2425 /** @param array<string, string>|null $config */
@@ -40,53 +41,43 @@ public function _initialize(): void
4041 if (is_string ($ this ->config [self ::CONFIG_CLEANUP_BEFORE ] ?? null )) {
4142 $ this ->cleanUpBefore = new CleanUpBefore ($ this ->config [self ::CONFIG_CLEANUP_BEFORE ]);
4243 }
43- $ this ->mockserverClient = new Client ([
44+ $ this ->mockserver = new MockServer ( new Client ([
4445 'base_uri ' => $ this ->config [self ::CONFIG_URL ]
45- ]);
46+ ])) ;
4647 if ($ this ->notMatchedRequest ->isEnabled ()) {
4748 $ expectationJson = file_get_contents (__DIR__ . '/not-matched-request.json ' );
4849 Assert::assertIsString ($ expectationJson );
4950 $ this ->createMockRequest ($ expectationJson );
50- } else {
51- $ this ->deactivateNotMatchedRequest ();
51+ return ;
5252 }
53+
54+ $ this ->deactivateNotMatchedRequest ();
5355 }
5456
5557 public function _beforeSuite ($ settings = []): void
5658 {
5759 parent ::_beforeSuite ($ settings );
5860 if ($ this ->cleanUpBefore ->isSuite ()) {
59- $ this ->clearMockServerLogs ();
61+ $ this ->mockserver -> clearLogs ();
6062 }
6163 }
6264
6365 public function _before (TestInterface $ test ): void
6466 {
6567 parent ::_before ($ test );
6668 if ($ this ->cleanUpBefore ->isTest ()) {
67- $ this ->clearMockServerLogs ();
69+ $ this ->mockserver -> clearLogs ();
6870 }
6971 }
7072
7173 public function seeMockRequestWasCalled (string $ expectationId , ?int $ times = null ): void
7274 {
73- $ body = json_encode ([
74- 'expectationId ' => ['id ' => $ expectationId ],
75- 'times ' => ['atLeast ' => $ times ?? 1 , 'atMost ' => $ times ?? 1000 ]
76- ]);
77- Assert::assertNotFalse ($ body );
78- $ request = new Request ('PUT ' , '/mockserver/verify ' , [], $ body );
79- $ response = $ this ->mockserverClient ->sendRequest ($ request );
80- Assert::assertEquals (
81- 202 ,
82- $ response ->getStatusCode (),
83- $ response ->getBody ()->getContents ()
84- );
75+ $ this ->mockserver ->verify ($ expectationId , $ times );
8576 }
8677
8778 public function seeMockRequestWasNotCalled (string $ expectationId ): void
8879 {
89- $ this ->seeMockRequestWasCalled ($ expectationId , 0 );
80+ $ this ->mockserver -> verify ($ expectationId , 0 );
9081 }
9182
9283 public function seeAllRequestWereMatched (): void
@@ -97,7 +88,7 @@ public function seeAllRequestWereMatched(): void
9788 );
9889 }
9990 try {
100- $ this ->seeMockRequestWasCalled (self ::NOT_MATCHED_REQUEST_ID , 0 );
91+ $ this ->mockserver -> verify (self ::NOT_MATCHED_REQUEST_ID , 0 );
10192 } catch (ExpectationFailedException $ exception ) {
10293 $ message = 'REQUEST NOT MATCHED ' . strstr ($ exception ->getMessage (), ' was: ' );
10394 throw new ExpectationFailedException ($ message );
@@ -106,48 +97,21 @@ public function seeAllRequestWereMatched(): void
10697
10798 public function createMockRequest (string $ json ): void
10899 {
109- $ request = new Request (
110- 'PUT ' ,
111- '/mockserver/expectation ' ,
112- ['Content-Type ' => 'application/json ' ],
113- $ json
114- );
115- $ response = $ this ->mockserverClient ->sendRequest ($ request );
116- Assert::assertEquals (
117- 201 ,
118- $ response ->getStatusCode (),
119- $ response ->getBody ()->getContents ()
120- );
100+ $ this ->mockserver ->create ($ json );
121101 }
122102
123103 public function removeMockRequest (string $ mockRequestId ): void
124104 {
125- $ body = json_encode ([
126- 'id ' => $ mockRequestId
127- ]);
128- Assert::assertIsString ($ body );
129- $ request = new Request ('PUT ' , '/mockserver/clear?type=expectations ' , [], $ body );
130- $ response = $ this ->mockserverClient ->sendRequest ($ request );
131- Assert::assertEquals (
132- 200 ,
133- $ response ->getStatusCode (),
134- $ response ->getBody ()->getContents ()
135- );
105+ $ this ->mockserver ->removeById ($ mockRequestId );
136106 }
137107
138108 public function clearMockServerLogs (): void
139109 {
140- $ request = new Request ('PUT ' , '/mockserver/clear?type=log ' );
141- $ response = $ this ->mockserverClient ->sendRequest ($ request );
142- Assert::assertEquals (
143- 200 ,
144- $ response ->getStatusCode (),
145- $ response ->getBody ()->getContents ()
146- );
110+ $ this ->mockserver ->clearLogs ();
147111 }
148112
149113 public function deactivateNotMatchedRequest (): void
150114 {
151- $ this ->removeMockRequest (self ::NOT_MATCHED_REQUEST_ID );
115+ $ this ->mockserver -> removeById (self ::NOT_MATCHED_REQUEST_ID );
152116 }
153117}
0 commit comments