Mocking
1 year ago
ApiCallTest.php
1 year ago
AuthenticationTest.php
1 year ago
ClientTest.php
1 year ago
CoreTestCaseTest.php
1 year ago
EndToEndTest.php
1 year ago
LoggerTest.php
1 year ago
TypesTest.php
1 year ago
UtilsTest.php
1 year ago
bootstrap.php
1 year ago
CoreTestCaseTest.php
399 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Tests; |
| 4 | |
| 5 | use Core\Client; |
| 6 | use Core\Request\Request; |
| 7 | use Core\Response\Context; |
| 8 | use Core\TestCase\BodyMatchers\BodyComparator; |
| 9 | use Core\TestCase\BodyMatchers\KeysAndValuesBodyMatcher; |
| 10 | use Core\TestCase\BodyMatchers\KeysBodyMatcher; |
| 11 | use Core\TestCase\BodyMatchers\NativeBodyMatcher; |
| 12 | use Core\TestCase\BodyMatchers\RawBodyMatcher; |
| 13 | use Core\TestCase\CoreTestCase; |
| 14 | use Core\TestCase\TestParam; |
| 15 | use Core\Tests\Mocking\MockHelper; |
| 16 | use Core\Tests\Mocking\Other\MockChild1; |
| 17 | use Core\Tests\Mocking\Other\MockChild2; |
| 18 | use Core\Tests\Mocking\Other\MockClass; |
| 19 | use Core\Tests\Mocking\Response\MockResponse; |
| 20 | use Core\Utils\CoreHelper; |
| 21 | use Core\Utils\DateHelper; |
| 22 | use PHPUnit\Framework\TestCase; |
| 23 | |
| 24 | class CoreTestCaseTest extends TestCase |
| 25 | { |
| 26 | /** |
| 27 | * @var Client |
| 28 | */ |
| 29 | private static $coreClient; |
| 30 | |
| 31 | public static function setUpBeforeClass(): void |
| 32 | { |
| 33 | self::$coreClient = MockHelper::getClient(); |
| 34 | } |
| 35 | |
| 36 | private static function getResponse(int $status, array $headers, $body): void |
| 37 | { |
| 38 | $response = new MockResponse(); |
| 39 | $response->setStatusCode($status); |
| 40 | $response->setHeaders($headers); |
| 41 | $response->setBody($body); |
| 42 | $context = new Context(new Request('http://my/path'), $response, self::$coreClient); |
| 43 | self::$coreClient->afterResponse($context); |
| 44 | } |
| 45 | |
| 46 | private function newTestCase($result): CoreTestCase |
| 47 | { |
| 48 | return new CoreTestCase($this, MockHelper::getCallbackCatcher(), $result); |
| 49 | } |
| 50 | |
| 51 | public function testScalarParam() |
| 52 | { |
| 53 | $param1 = 'This is string'; |
| 54 | |
| 55 | self::getResponse(202, ['key1' => 'res/header', 'key2' => 'res/2nd'], $param1); |
| 56 | |
| 57 | $this->newTestCase($param1)->assert(); |
| 58 | |
| 59 | $this->newTestCase($param1) |
| 60 | ->expectHeaders(['key1' => ['differentValue', false], 'key2' => ['differentValue', 'not bool']]) |
| 61 | ->assert(); |
| 62 | |
| 63 | $this->newTestCase($param1) |
| 64 | ->expectStatus(202) |
| 65 | ->expectHeaders(['key1' => ['res/header', true]]) |
| 66 | ->allowExtraHeaders() |
| 67 | ->assert(); |
| 68 | |
| 69 | $this->newTestCase($param1) |
| 70 | ->expectStatusRange(200, 208) |
| 71 | ->expectHeaders(['key1' => ['res/header', true], 'key2' => ['res/2nd', true]]) |
| 72 | ->bodyMatcher(RawBodyMatcher::init('This is string')) |
| 73 | ->assert(); |
| 74 | |
| 75 | $this->newTestCase($param1) |
| 76 | ->expectStatusRange(200, 208) |
| 77 | ->expectHeaders(['key1' => ['res/header', true], 'key2' => ['res/2nd', true]]) |
| 78 | ->bodyMatcher(NativeBodyMatcher::init('This is string')) |
| 79 | ->assert(); |
| 80 | } |
| 81 | |
| 82 | public function testFileParam() |
| 83 | { |
| 84 | $file = TestParam::file('https://gist.githubusercontent.com/asadali214/0a64efec5353d351818475f928c50767/' . |
| 85 | 'raw/8ad3533799ecb4e01a753aaf04d248e6702d4947/testFile.txt'); |
| 86 | |
| 87 | self::getResponse(200, [], $file); |
| 88 | |
| 89 | $this->newTestCase($file) |
| 90 | ->expectStatus(200) |
| 91 | ->bodyMatcher(RawBodyMatcher::init(TestParam::file('https://gist.githubusercontent.com/asadali214/' . |
| 92 | '0a64efec5353d351818475f928c50767/raw/8ad3533799ecb4e01a753aaf04d248e6702d4947/testFile.txt'))) |
| 93 | ->assert(); |
| 94 | } |
| 95 | |
| 96 | public function testObjectParamForKeysAndValues() |
| 97 | { |
| 98 | $obj = TestParam::object('{"key1":"value 1","key2":false,"key3":2.3}'); |
| 99 | |
| 100 | self::getResponse(200, [], $obj); |
| 101 | |
| 102 | $this->newTestCase($obj) |
| 103 | ->expectStatus(200) |
| 104 | ->bodyMatcher(RawBodyMatcher::init('{"key1":"value 1","key2":false,"key3":2.3}')) |
| 105 | ->assert(); |
| 106 | |
| 107 | $this->newTestCase($obj) |
| 108 | ->expectStatus(200) |
| 109 | ->bodyMatcher(KeysAndValuesBodyMatcher::init( |
| 110 | TestParam::object('{"key1":"value 1","key2":false,"key3":2.3}'), |
| 111 | true, |
| 112 | true |
| 113 | )) |
| 114 | ->assert(); |
| 115 | |
| 116 | $this->newTestCase($obj) |
| 117 | ->expectStatus(200) |
| 118 | ->bodyMatcher(KeysAndValuesBodyMatcher::init( |
| 119 | TestParam::object('{"key1":"value 1","key2":false}'), |
| 120 | true, |
| 121 | false |
| 122 | )) |
| 123 | ->assert(); |
| 124 | |
| 125 | $this->newTestCase($obj) |
| 126 | ->expectStatus(200) |
| 127 | ->bodyMatcher(KeysAndValuesBodyMatcher::init( |
| 128 | TestParam::object('{"key2":false,"key3":2.3,"key1":"value 1"}'), |
| 129 | false, |
| 130 | true |
| 131 | )) |
| 132 | ->assert(); |
| 133 | |
| 134 | $this->newTestCase($obj) |
| 135 | ->expectStatus(200) |
| 136 | ->bodyMatcher(KeysAndValuesBodyMatcher::init( |
| 137 | TestParam::object('{"key2":false,"key3":2.3}'), |
| 138 | false, |
| 139 | false |
| 140 | )) |
| 141 | ->assert(); |
| 142 | } |
| 143 | |
| 144 | public function testObjectParamForKeys() |
| 145 | { |
| 146 | $obj = TestParam::object('{"key1":"value 1","key2":false,"key3":2.3}'); |
| 147 | |
| 148 | self::getResponse(200, [], $obj); |
| 149 | |
| 150 | $this->newTestCase($obj) |
| 151 | ->expectStatus(200) |
| 152 | ->bodyMatcher(KeysBodyMatcher::init( |
| 153 | TestParam::object('{"key1":"valueB","key2":true,"key3":"myString"}'), |
| 154 | true, |
| 155 | true |
| 156 | )) |
| 157 | ->assert(); |
| 158 | |
| 159 | $this->newTestCase($obj) |
| 160 | ->expectStatus(200) |
| 161 | ->bodyMatcher(KeysBodyMatcher::init( |
| 162 | TestParam::object('{"key1":"value 1","key3":false}'), |
| 163 | true, |
| 164 | false |
| 165 | )) |
| 166 | ->assert(); |
| 167 | |
| 168 | $this->newTestCase($obj) |
| 169 | ->expectStatus(200) |
| 170 | ->bodyMatcher(KeysBodyMatcher::init( |
| 171 | TestParam::object('{"key2":false,"key3":2.3,"key1":"value 1"}'), |
| 172 | false, |
| 173 | true |
| 174 | )) |
| 175 | ->assert(); |
| 176 | |
| 177 | $this->newTestCase($obj) |
| 178 | ->expectStatus(200) |
| 179 | ->bodyMatcher(KeysBodyMatcher::init( |
| 180 | TestParam::object('{"key2":{"key":"val"}}'), |
| 181 | false, |
| 182 | false |
| 183 | )) |
| 184 | ->assert(); |
| 185 | } |
| 186 | |
| 187 | public function testNativeBodyMatcherMessage() |
| 188 | { |
| 189 | $object = CoreHelper::deserialize('{"key":"somevalue"}', false); |
| 190 | $array = ["key" => "somevalue"]; |
| 191 | $scalar = "somevalue"; |
| 192 | |
| 193 | $message = NativeBodyMatcher::init($scalar)->getDefaultMessage(); |
| 194 | $this->assertEquals('Response values does not match', $message); |
| 195 | |
| 196 | $message = NativeBodyMatcher::init($object, true, true)->getDefaultMessage(); |
| 197 | $this->assertEquals('Response object values does not match in order or size', $message); |
| 198 | |
| 199 | $message = NativeBodyMatcher::init($array, true, true)->getDefaultMessage(); |
| 200 | $this->assertEquals('Response array values does not match in order or size', $message); |
| 201 | |
| 202 | $message = NativeBodyMatcher::init($object, false, true)->getDefaultMessage(); |
| 203 | $this->assertEquals('Response object values does not match in size', $message); |
| 204 | |
| 205 | $message = NativeBodyMatcher::init($array, false, true)->getDefaultMessage(); |
| 206 | $this->assertEquals('Response array values does not match in size', $message); |
| 207 | |
| 208 | $message = NativeBodyMatcher::init($object, true)->getDefaultMessage(); |
| 209 | $this->assertEquals('Response object values does not match in order', $message); |
| 210 | |
| 211 | $message = NativeBodyMatcher::init($array, true)->getDefaultMessage(); |
| 212 | $this->assertEquals('Response array values does not match in order', $message); |
| 213 | |
| 214 | $message = NativeBodyMatcher::init($object)->getDefaultMessage(); |
| 215 | $this->assertEquals('Response object values does not match', $message); |
| 216 | |
| 217 | $message = NativeBodyMatcher::init($array)->getDefaultMessage(); |
| 218 | $this->assertEquals('Response array values does not match', $message); |
| 219 | } |
| 220 | |
| 221 | public function testClassParamForNative() |
| 222 | { |
| 223 | $obj = TestParam::object('{"body":{"asad":"item1","ali":"item2"}}', MockClass::class); |
| 224 | self::getResponse(200, [], $obj); |
| 225 | $this->newTestCase($obj) |
| 226 | ->expectStatus(200) |
| 227 | ->bodyMatcher(NativeBodyMatcher::init( |
| 228 | TestParam::object('{"body":{"asad":"item1","ali":"item2"}}', MockClass::class) |
| 229 | )) |
| 230 | ->assert(); |
| 231 | |
| 232 | $obj = TestParam::object('{"key1":{"body":{"asad":"item1","ali":"item2"}},' . |
| 233 | '"key2":{"body":{"asad":"item1","ali":"item2"}}}', MockClass::class, 1); |
| 234 | self::getResponse(200, [], $obj); |
| 235 | $this->newTestCase($obj) |
| 236 | ->expectStatus(200) |
| 237 | ->bodyMatcher(NativeBodyMatcher::init( |
| 238 | TestParam::object('{"key1":{"body":{"asad":"item1","ali":"item2"}},' . |
| 239 | '"key2":{"body":{"asad":"item1","ali":"item2"}}}', MockClass::class, 1), |
| 240 | true, |
| 241 | true |
| 242 | )) |
| 243 | ->assert(); |
| 244 | } |
| 245 | |
| 246 | public function testDateParamForNative() |
| 247 | { |
| 248 | $obj = TestParam::custom('2021-10-01', [DateHelper::class, 'fromSimpleDate']); |
| 249 | self::getResponse(200, [], $obj); |
| 250 | $this->newTestCase($obj) |
| 251 | ->expectStatus(200) |
| 252 | ->bodyMatcher(NativeBodyMatcher::init($obj)) |
| 253 | ->assert(); |
| 254 | |
| 255 | $obj = TestParam::custom( |
| 256 | '{"key1":"2021-10-01","key2":"2021-10-02"}', |
| 257 | [DateHelper::class, 'fromSimpleDateMap'] |
| 258 | ); |
| 259 | self::getResponse(200, [], $obj); |
| 260 | $this->newTestCase($obj) |
| 261 | ->expectStatus(200) |
| 262 | ->bodyMatcher(NativeBodyMatcher::init($obj, true, true)) |
| 263 | ->assert(); |
| 264 | |
| 265 | $obj = TestParam::custom('["2021-10-01","2021-10-02"]', [DateHelper::class, 'fromSimpleDateArray']); |
| 266 | self::getResponse(200, [], $obj); |
| 267 | $this->newTestCase($obj) |
| 268 | ->expectStatus(200) |
| 269 | ->bodyMatcher(NativeBodyMatcher::init($obj, true, true)) |
| 270 | ->assert(); |
| 271 | } |
| 272 | |
| 273 | public function testTypeGroupParamForNative() |
| 274 | { |
| 275 | $obj = TestParam::typeGroup('This is string', 'oneof(string,int)'); |
| 276 | self::getResponse(200, [], $obj); |
| 277 | $this->newTestCase($obj) |
| 278 | ->expectStatus(200) |
| 279 | ->bodyMatcher(NativeBodyMatcher::init( |
| 280 | TestParam::typeGroup('This is string', 'oneof(string,int)') |
| 281 | )) |
| 282 | ->assert(); |
| 283 | } |
| 284 | |
| 285 | public function testTypeGroupParamForCustomTypes() |
| 286 | { |
| 287 | $obj = TestParam::typeGroup( |
| 288 | '{"childBody":"this is mock class","body":[]}', |
| 289 | 'anyOf(MockChild1,MockChild2)' |
| 290 | ); |
| 291 | $this->assertInstanceOf(MockChild1::class, $obj); |
| 292 | } |
| 293 | |
| 294 | public function testTypeGroupParamForCustomTypesWithDiscriminators() |
| 295 | { |
| 296 | $obj = TestParam::typeGroup( |
| 297 | '{"childBody":"this is mock class","body":[],"my field":"This is 2"}', |
| 298 | 'oneOf{disc}(MockChild1{disc1},MockChild2{disc2})' |
| 299 | ); |
| 300 | $this->assertInstanceOf(MockChild2::class, $obj); |
| 301 | } |
| 302 | |
| 303 | public function testClassArrayParamForNative() |
| 304 | { |
| 305 | $obj = TestParam::object('[{"body":{"asad":"item1","ali":"item2"},"0":"other value"},' . |
| 306 | '{"body":{"key1":"item1","key2":"item2","key3":"item3"}}]', MockClass::class, 1); |
| 307 | self::getResponse(200, [], $obj); |
| 308 | |
| 309 | $this->newTestCase($obj) |
| 310 | ->expectStatus(200) |
| 311 | ->bodyMatcher(NativeBodyMatcher::init( |
| 312 | TestParam::object('[{"0":"other value","body":{"ali":"item2","asad":"item1"}}' . |
| 313 | ',{"body":{"key1":"item1","key3":"item3","key2":"item2"}},{"body":{"key1":"item1"' . |
| 314 | ',"key3":"item3"}}]', MockClass::class, 1), |
| 315 | false, |
| 316 | false |
| 317 | )) |
| 318 | ->assert(); |
| 319 | |
| 320 | $this->newTestCase($obj) |
| 321 | ->expectStatus(200) |
| 322 | ->bodyMatcher(NativeBodyMatcher::init( |
| 323 | TestParam::object('[{"body":{"asad":"item1","ali":"item2"},"0":"other value"},' . |
| 324 | '{"body":{"key1":"item1","key2":"item2","key3":"item3"}}]', MockClass::class, 1), |
| 325 | true, |
| 326 | true |
| 327 | )) |
| 328 | ->assert(); |
| 329 | |
| 330 | $this->newTestCase($obj) |
| 331 | ->expectStatus(200) |
| 332 | ->bodyMatcher(NativeBodyMatcher::init( |
| 333 | TestParam::object( |
| 334 | '[{"body":{"asad":"item1","ali":"item2"},"0":"other value"}]', |
| 335 | MockClass::class, |
| 336 | 1 |
| 337 | ), |
| 338 | true, |
| 339 | false |
| 340 | )) |
| 341 | ->assert(); |
| 342 | |
| 343 | $this->newTestCase($obj) |
| 344 | ->expectStatus(200) |
| 345 | ->bodyMatcher(NativeBodyMatcher::init( |
| 346 | TestParam::object('[{"0":"other value","body":{"ali":"item2","asad":"item1"}},' . |
| 347 | '{"body":{"key1":"item1","key3":"item3","key2":"item2"}}]', MockClass::class, 1), |
| 348 | false, |
| 349 | true |
| 350 | )) |
| 351 | ->assert(); |
| 352 | } |
| 353 | |
| 354 | public function testPrimitiveArrayParamForNative() |
| 355 | { |
| 356 | $obj = TestParam::object('["string1","string2"]'); |
| 357 | self::getResponse(200, [], $obj); |
| 358 | |
| 359 | $this->newTestCase($obj) |
| 360 | ->expectStatus(200) |
| 361 | ->bodyMatcher(NativeBodyMatcher::init( |
| 362 | TestParam::object('["string1","string2","string10","string20"]'), |
| 363 | false, |
| 364 | false |
| 365 | )) |
| 366 | ->assert(); |
| 367 | } |
| 368 | |
| 369 | public function testBodyComparator() |
| 370 | { |
| 371 | $obj1 = CoreHelper::deserialize('{"key1":23,"key2":true,"key3":"my string"}', false); |
| 372 | $obj1Copy = CoreHelper::deserialize('{"key1":32,"key2":true,"key3":"my string"}', false); |
| 373 | $obj2 = CoreHelper::deserialize('{"key1":23,"key3":"my string","key2":true,"key4":23.56}', false); |
| 374 | $obj3 = [23, "my string"]; |
| 375 | $obj4 = [$obj3, "my string"]; |
| 376 | |
| 377 | $this->assertFalse((new BodyComparator(false))->compare($obj1, $obj2)); // not allowing extra |
| 378 | $this->assertTrue((new BodyComparator())->compare(null, null)); // both are null |
| 379 | $this->assertFalse((new BodyComparator())->compare(null, $obj2)); // expected is null but actual is not. |
| 380 | $this->assertFalse((new BodyComparator())->compare($obj1, null)); // actual is null but expected is not. |
| 381 | // not equal but not checking for values |
| 382 | $this->assertTrue((new BodyComparator(true, false, false))->compare($obj1, null)); |
| 383 | $this->assertFalse((new BodyComparator())->compare($obj1, 234)); // matching object with primitive |
| 384 | $this->assertFalse((new BodyComparator())->compare($obj2, $obj1)); // actual obj missing a key |
| 385 | // actual obj does not follow same order |
| 386 | $this->assertFalse((new BodyComparator(true, true))->compare($obj1, $obj2)); |
| 387 | // inner actual is not array like inner expected value |
| 388 | $this->assertFalse((new BodyComparator())->compare($obj4, $obj3)); |
| 389 | // inner expected is not array like inner actual value |
| 390 | $this->assertFalse((new BodyComparator())->compare($obj3, $obj4)); |
| 391 | // inner expected value doesn't match actual expected value |
| 392 | $this->assertFalse((new BodyComparator())->compare($obj1, $obj1Copy)); |
| 393 | // left associative array but right not associative |
| 394 | $this->assertFalse((new BodyComparator())->compare($obj1, $obj3)); |
| 395 | // left indexed array but right not indexed |
| 396 | $this->assertFalse((new BodyComparator())->compare($obj3, $obj1)); |
| 397 | } |
| 398 | } |
| 399 |