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
EndToEndTest.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Tests; |
| 4 | |
| 5 | use Core\ApiCall; |
| 6 | use Core\Authentication\Auth; |
| 7 | use Core\Request\Parameters\FormParam; |
| 8 | use Core\Request\Parameters\HeaderParam; |
| 9 | use Core\Request\Parameters\QueryParam; |
| 10 | use Core\Request\Parameters\TemplateParam; |
| 11 | use Core\Request\RequestBuilder; |
| 12 | use Core\Response\ResponseHandler; |
| 13 | use Core\Response\Types\ErrorType; |
| 14 | use Core\TestCase\BodyMatchers\NativeBodyMatcher; |
| 15 | use Core\TestCase\CoreTestCase; |
| 16 | use Core\TestCase\TestParam; |
| 17 | use Core\Tests\Mocking\MockHelper; |
| 18 | use Core\Tests\Mocking\Other\MockClass; |
| 19 | use Core\Tests\Mocking\Other\MockException; |
| 20 | use Core\Tests\Mocking\Other\MockException3; |
| 21 | use Core\Tests\Mocking\Types\MockFileWrapper; |
| 22 | use Core\Utils\DateHelper; |
| 23 | use CoreInterfaces\Http\RetryOption; |
| 24 | use DateTime; |
| 25 | use PHPUnit\Framework\TestCase; |
| 26 | |
| 27 | class EndToEndTest extends TestCase |
| 28 | { |
| 29 | public function newApiCall(): ApiCall |
| 30 | { |
| 31 | return new ApiCall(MockHelper::getClient()); |
| 32 | } |
| 33 | |
| 34 | public function globalResponseHandler(): ResponseHandler |
| 35 | { |
| 36 | return MockHelper::getClient()->getGlobalResponseHandler(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param string|int $template |
| 41 | * @param DateTime[]|null $query |
| 42 | * @param int $header |
| 43 | * @param MockFileWrapper $form1 |
| 44 | * @param array $form2 |
| 45 | * |
| 46 | * @return MockClass Returning some mock class |
| 47 | * @throws MockException |
| 48 | */ |
| 49 | public function callEndpoint($template, ?array $query, int $header, MockFileWrapper $form1, array $form2): MockClass |
| 50 | { |
| 51 | return $this->newApiCall() |
| 52 | ->requestBuilder((new RequestBuilder('POST', '/api/path/{sub-path}')) |
| 53 | ->server('Server2') |
| 54 | ->parameters( |
| 55 | TemplateParam::init('sub-path', $template)->required()->strictType('oneof(string,int)'), |
| 56 | QueryParam::init('date array', $query) |
| 57 | ->commaSeparated() |
| 58 | ->serializeBy([DateHelper::class, 'toRfc1123DateTimeArray']), |
| 59 | HeaderParam::init('header', $header), |
| 60 | FormParam::init('form 1', $form1) |
| 61 | ->encodingHeader('content-type', 'text/plain') |
| 62 | ->unIndexed() |
| 63 | ->required(), |
| 64 | FormParam::init('form 2', $form2)->unIndexed() |
| 65 | ) |
| 66 | ->auth(Auth::and('query', 'header')) |
| 67 | ->retryOption(RetryOption::ENABLE_RETRY)) |
| 68 | ->responseHandler($this->globalResponseHandler() |
| 69 | ->type(MockClass::class) |
| 70 | ->throwErrorOn("405", ErrorType::init('Wrong payload 405', MockException3::class)) |
| 71 | ->nullOn404()) |
| 72 | ->execute(); |
| 73 | } |
| 74 | |
| 75 | private function newTestCase($result): CoreTestCase |
| 76 | { |
| 77 | return new CoreTestCase($this, MockHelper::getCallbackCatcher(), $result); |
| 78 | } |
| 79 | |
| 80 | public function testEndpoint() |
| 81 | { |
| 82 | $template = TestParam::typeGroup('poster', 'oneof(string,int)'); |
| 83 | $query = TestParam::custom( |
| 84 | '["Fri, 01 Oct 2021 00:00:00 GMT","Thu, 30 Sep 2021 00:00:00 GMT"]', |
| 85 | [DateHelper::class, 'fromRfc1123DateTimeArray'] |
| 86 | ); |
| 87 | $header = 1234; |
| 88 | $form1 = TestParam::file('https://gist.githubusercontent.com/asadali214/' . |
| 89 | '0a64efec5353d351818475f928c50767/raw/8ad3533799ecb4e01a753aaf04d248e6702d4947/testFile.txt'); |
| 90 | $form2 = TestParam::object('{"key1":"value 1","key2":false,"key3":2.3}'); |
| 91 | |
| 92 | $result = null; |
| 93 | try { |
| 94 | $result = $this->callEndpoint($template, $query, $header, $form1, $form2); |
| 95 | } catch (MockException $e) { |
| 96 | var_dump($e->getMessage()); |
| 97 | } |
| 98 | $this->newTestCase($result) |
| 99 | ->expectStatusRange(200, 208) |
| 100 | ->expectHeaders(['content-type' => ['application/json', true]]) |
| 101 | ->allowExtraHeaders() |
| 102 | ->bodyMatcher(NativeBodyMatcher::init(TestParam::object('{"body":{"httpMethod":"POST","queryUrl":' . |
| 103 | '"https:\/\/my\/path\/v2\/api\/path\/poster?&date+array=Fri%2C+01+Oct+2021+00%3A00%3A00+GMT%2CThu' . |
| 104 | '%2C+30+Sep+2021+00%3A00%3A00+GMT&token=someAuthToken&authorization=accessToken","headers":{' . |
| 105 | '"additionalHead1":"headVal1","additionalHead2":"headVal2","header":1234,"token":"someAuthToken",' . |
| 106 | '"authorization":"accessToken","Accept":"application\/json"},"parameters":{"form 2":{"key1":' . |
| 107 | '"value 1","key2":"false","key3":2.3}},"parametersEncoded":{' . |
| 108 | '"form 2":"form+2%5Bkey1%5D=value+1&form+2%5Bkey2%5D=false&form+2%5Bkey3%5D=2.3"},' . |
| 109 | '"retryOption":"enableRetries"}}', MockClass::class), true)) |
| 110 | ->assert(); |
| 111 | } |
| 112 | } |
| 113 |