From 171d3adbc0af951979d549a2419bb68840a97088 Mon Sep 17 00:00:00 2001 From: Camille Baronnet Date: Mon, 31 Aug 2020 21:36:57 +0200 Subject: [PATCH] [PHPUnit] Fix test suite locally --- .gitignore | 3 + composer.json | 8 ++- phpunit.xml.dist | 23 ++++++++ src/Transports/Transport.php | 2 +- tests/ApiTest.php | 41 ++++++-------- tests/TransportCurlTest.php | 104 ++++++++++++++++++----------------- 6 files changed, 104 insertions(+), 77 deletions(-) create mode 100644 .gitignore create mode 100644 phpunit.xml.dist diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c9d0d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.phpunit.result.cache +/composer.lock +/vendor/ diff --git a/composer.json b/composer.json index 8631e55..3d5aed6 100755 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "name": "cristal/php-api-wrapper", + "description": "Work with APIs like with Laravel Eloquent or Doctrine (no longer a dream)", "type": "package", "authors": [ { @@ -23,5 +24,10 @@ "Cristal\\ApiWrapper\\": "src" } }, - "license": "MIT" + "license": "MIT", + "require-dev": { + "phpunit/phpunit": "^9.3", + "mockery/mockery": "^1.4", + "symfony/var-dumper": "^5.1" + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..61fdf36 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,23 @@ + + + + + ./src + + + + + + + + ./tests/ + + + diff --git a/src/Transports/Transport.php b/src/Transports/Transport.php index 7ce4323..d14e055 100755 --- a/src/Transports/Transport.php +++ b/src/Transports/Transport.php @@ -213,7 +213,7 @@ public function encodeBody($data) return $data; } if ($value instanceof MultipartParam) { - $delimiter = '----WebKitFormBoundary'.uniqid(); + $delimiter = '----WebKitFormBoundary'.uniqid('', true); $this->getClient()->setHeader('Content-Type', 'multipart/form-data; boundary=' . $delimiter); return join(array_map(function ($param, $name) use ($delimiter) { diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 177f9d6..d207355 100755 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -6,16 +6,17 @@ use Cristal\ApiWrapper\Transports\TransportInterface; use Mockery; use PHPUnit\Framework\TestCase; +use TypeError; class ApiTest extends TestCase { - const ENDPOINTS = ['client', 'catalogue', 'materiel', 'fabricant', 'type', 'tarif', 'caracteristique']; + protected const ENDPOINTS = ['client', 'catalogue', 'materiel', 'fabricant', 'type', 'tarif', 'caracteristique']; protected $token; protected $entrypoint; - const WITH_FILTER = ['with_filters']; - const WITHOUT_FILTER = ['without_filters']; - const ID_ENTITY = 123; + protected const WITH_FILTER = ['with_filters']; + protected const WITHOUT_FILTER = ['without_filters']; + protected const ID_ENTITY = 123; protected function createFakeTransport() { @@ -27,60 +28,50 @@ protected function createFakeTransport() return $transport; } - public function setUp() + public function setUp(): void { $this->token = 'token_jwt'; $this->entrypoint = 'https://exemple/api/'; } - public function testApiWithoutTransport() + public function testApiWithoutTransport(): void { - $this->expectException(\TypeError::class); + $this->expectException(TypeError::class); new Api(null); } - public function testApiWithTransport() + public function testApiWithTransport(): void { $transport = Mockery::mock(TransportInterface::class); $api = new Api($transport); - $this->assertInstanceOf(TransportInterface::class, $api->getTransport()); + self::assertInstanceOf(TransportInterface::class, $api->getTransport()); } - public function testGetWithoutFilters() + public function testGetWithoutFilters(): void { $transport = $this->createFakeTransport(); $api = new Api($transport); foreach (self::ENDPOINTS as $endpoint) { - $this->assertEquals(self::WITHOUT_FILTER, $api->{'get'.ucfirst($endpoint).'s'}()); + self::assertEquals(self::WITHOUT_FILTER, $api->{'get'.ucfirst($endpoint).'s'}()); } } - public function testGetWithFilters() + public function testGetWithFilters(): void { $transport = $this->createFakeTransport(); $api = new Api($transport); foreach (self::ENDPOINTS as $endpoint) { - $this->assertEquals(self::WITH_FILTER, $api->{'get'.ucfirst($endpoint).'s'}(self::WITH_FILTER)); + self::assertEquals(self::WITH_FILTER, $api->{'get'.ucfirst($endpoint).'s'}(self::WITH_FILTER)); } } - public function testTryToGetSpecificEntityWithoutIdAsArgument() - { - $this->expectException(\TypeError::class); - $transport = $this->createFakeTransport(); - $api = new Api($transport); - foreach (self::ENDPOINTS as $endpoint) { - $api->{'get'.ucfirst($endpoint)}(); - } - } - - public function testTryToGetSpecificEntity() + public function testTryToGetSpecificEntity(): void { $transport = $this->createFakeTransport(); $api = new Api($transport); foreach (self::ENDPOINTS as $endpoint) { $entity = $api->{'get'.ucfirst($endpoint)}(self::ID_ENTITY); - $this->assertInternalType('array', $entity); + self::assertIsArray($entity); } } } diff --git a/tests/TransportCurlTest.php b/tests/TransportCurlTest.php index 71e7235..2cc13d9 100755 --- a/tests/TransportCurlTest.php +++ b/tests/TransportCurlTest.php @@ -2,7 +2,10 @@ namespace Cristal\ApiWrapper\Tests; +use Curl\Curl; +use Exception; use Mockery; +use Mockery\MockInterface; use PHPUnit\Framework\TestCase; use Cristal\ApiWrapper\Transports\Bearer; @@ -19,191 +22,192 @@ class TransportCurlTest extends TestCase protected $entrypoint; /** - * @var \Mockery\MockInterface|\Curl\Curl + * @var MockInterface|Curl */ protected $client; - protected function setUp() + protected function setUp(): void { $this->jwt = 'jwt'; $this->entrypoint = 'http://entrypoint/'; - $this->client = Mockery::mock(\Curl\Curl::class); + $this->client = Mockery::mock(Curl::class); $this->client->shouldReceive('setHeader')->andReturn(null); } - public function testCreateCurlTranportClass() + public function testCreateCurlTranportClassWorks(): void { - new Bearer($this->jwt, $this->entrypoint, $this->client); + $transport = new Bearer($this->jwt, $this->entrypoint, $this->client); + self::assertInstanceOf(Bearer::class, $transport); } /** * @param $httpCode - * @throws \Exception + * @throws Exception */ - protected function httpStatusCode($httpCode) + protected function httpStatusCode($httpCode): void { $curl = new Bearer($this->jwt, $this->entrypoint, $this->client); $this->client->shouldReceive('get')->andReturn(null); - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->client->httpStatusCode = $httpCode; $curl->request('/some-endpoint'); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode404ThrowsAnException() + public function testRequestWithHttpStatusCode404ThrowsAnException(): void { $this->httpStatusCode(404); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode301ThrowsAnException() + public function testRequestWithHttpStatusCode301ThrowsAnException(): void { $this->httpStatusCode(301); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode500ThrowsAnException() + public function testRequestWithHttpStatusCode500ThrowsAnException(): void { $this->httpStatusCode(500); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode300ThrowsAnException() + public function testRequestWithHttpStatusCode300ThrowsAnException(): void { $this->httpStatusCode(300); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode418ThrowsAnException() + public function testRequestWithHttpStatusCode418ThrowsAnException(): void { $this->httpStatusCode(418); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode422ThrowsAnException() + public function testRequestWithHttpStatusCode422ThrowsAnException(): void { $this->httpStatusCode(422); } /** * @param $httpCode - * @throws \Exception + * @throws Exception */ - protected function httpStatusCodeValidParseAndReturnJsonReponse($httpCode) + protected function httpStatusCodeValidParseAndReturnJsonReponse($httpCode): void { $curl = new Bearer($this->jwt, $this->entrypoint, $this->client); $this->client->shouldReceive('get')->andReturn(null); $this->client->httpStatusCode = $httpCode; $this->client->rawResponse = '{"success":true}'; - $this->assertArrayHasKey('success', $curl->request('/some-endpoint')); + self::assertArrayHasKey('success', $curl->request('/some-endpoint')); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode200ParseAndReturnJsonResponse() + public function testRequestWithHttpStatusCode200ParseAndReturnJsonResponse(): void { $this->httpStatusCodeValidParseAndReturnJsonReponse(200); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode201ParseAndReturnJsonResponse() + public function testRequestWithHttpStatusCode201ParseAndReturnJsonResponse(): void { $this->httpStatusCodeValidParseAndReturnJsonReponse(201); } /** - * @throws \Exception + * @throws Exception */ - public function testRequestWithHttpStatusCode204ParseAndReturnJsonResponse() + public function testRequestWithHttpStatusCode204ParseAndReturnJsonResponse(): void { $this->httpStatusCodeValidParseAndReturnJsonReponse(204); } /** - * @throws \Exception + * @throws Exception */ - public function testGetMethodRequest() + public function testGetMethodRequest(): void { $curl = new Bearer($this->jwt, $this->entrypoint, $this->client); $client = $this->client; - $this->client->shouldReceive('get')->andReturnUsing(function () use ($client) { + $this->client->shouldReceive('get')->andReturnUsing(static function () use ($client) { $client->httpStatusCode = 200; $client->rawResponse = '{"success":true}'; }); - $this->assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'get')); + self::assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'get')); } /** - * @throws \Exception + * @throws Exception */ - public function testPostMethodRequest() + public function testPostMethodRequest(): void { $curl = new Bearer($this->jwt, $this->entrypoint, $this->client); $client = $this->client; - $this->client->shouldReceive('post')->andReturnUsing(function () use ($client) { + $this->client->shouldReceive('post')->andReturnUsing(static function () use ($client) { $client->httpStatusCode = 200; $client->rawResponse = '{"success":true}'; }); - $this->assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'post')); + self::assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'post')); } /** - * @throws \Exception + * @throws Exception */ - public function testPutMethodRequest() + public function testPutMethodRequest(): void { $curl = new Bearer($this->jwt, $this->entrypoint, $this->client); $client = $this->client; - $this->client->shouldReceive('put')->andReturnUsing(function () use ($client) { + $this->client->shouldReceive('put')->andReturnUsing(static function () use ($client) { $client->httpStatusCode = 200; $client->rawResponse = '{"success":true}'; }); - $this->assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'put')); + self::assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'put')); } /** - * @throws \Exception + * @throws Exception */ - public function testDeleteMethodRequest() + public function testDeleteMethodRequest(): void { $curl = new Bearer($this->jwt, $this->entrypoint, $this->client); $client = $this->client; - $this->client->shouldReceive('delete')->andReturnUsing(function () use ($client) { + $this->client->shouldReceive('delete')->andReturnUsing(static function () use ($client) { $client->httpStatusCode = 200; $client->rawResponse = '{"success":true}'; }); - $this->assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'delete')); + self::assertArrayHasKey('success', $curl->request('/some-endpoint', [], 'delete')); } /** - * @throws \Exception + * @throws Exception */ - public function testUnknownMethodRequestThrowsAnException() + public function testUnknownMethodRequestThrowsAnException(): void { - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $curl = new Bearer($this->jwt, $this->entrypoint, $this->client); $client = $this->client; - $this->client->shouldReceive('azerty')->andReturnUsing(function () use ($client) { + $this->client->shouldReceive('foo')->andReturnUsing(static function () use ($client) { $client->httpStatusCode = 200; }); - $curl->request('/some-endpoint', [], 'azerty'); + $curl->request('/some-endpoint', [], 'foo'); } }