Skip to content

Commit

Permalink
[PHPUnit] Fix test suite locally
Browse files Browse the repository at this point in the history
  • Loading branch information
camillebaronnet committed Sep 1, 2020
1 parent 4e09b5c commit 171d3ad
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 77 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.phpunit.result.cache
/composer.lock
/vendor/
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand All @@ -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"
}
}
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Cristal Team - PHP API Wrapper Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/Transports/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
41 changes: 16 additions & 25 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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);
}
}
}
Loading

0 comments on commit 171d3ad

Please sign in to comment.