Skip to content

Commit

Permalink
Merge pull request #5 from signnow/1.x-test-coverage
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
falgonua authored Jul 2, 2021
2 parents e910f7e + 1b85c09 commit f173722
Show file tree
Hide file tree
Showing 16 changed files with 751 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v1.3.0] - 2021-07-02

### Changed

- Increase code coverage

## [v1.2.0] - 2021-06-23

### Added
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.2/phpunit.xsd"
bootstrap="tests/autoload.php"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/Entity/StatusResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types = 1);

namespace Tests\Fixtures\Entity;

use JMS\Serializer\Annotation as Serializer;

/**
* Class StatusResponse
*
* @package Tests\Fixtures\Entity
*/
class StatusResponse
{
/**
* @Serializer\Type("string")
* @var string
*/
private $status;

/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
}
38 changes: 38 additions & 0 deletions tests/fixtures/Entity/TemplateEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types = 1);

namespace Tests\Fixtures\Entity;

use SignNow\Rest\Entity\Entity;
use SignNow\Rest\EntityManager\Annotation\HttpEntity;
use SignNow\Rest\EntityManager\Annotation\GuzzleRequestBody;
use SignNow\Rest\EntityManager\Annotation\ResponseType;
use JMS\Serializer\Annotation as Serializer;

/**
* Class TemplateEntity
*
* @HttpEntity("template", idProperty="")
* @GuzzleRequestBody("form_params")
* @ResponseType("Tests\Fixtures\Entity\StatusResponse")
*
* @package Tests\Fixtures\Entity
*/
class TemplateEntity extends Entity
{
/**
* @Serializer\Type("string")
* @var string
*/
private $id;

/**
* TemplateEntity constructor.
*
* @param string $id
*/
public function __construct(string $id)
{
$this->id = $id;
}
}
39 changes: 39 additions & 0 deletions tests/fixtures/Entity/Upload/FileLinkUpload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types = 1);

namespace Tests\Fixtures\Entity\Upload;

use SignNow\Rest\Entity\Entity;
use SignNow\Rest\EntityManager\Annotation\HttpEntity;
use SignNow\Rest\EntityManager\Annotation\GuzzleRequestBody;
use SignNow\Rest\EntityManager\Annotation\ResponseType;
use JMS\Serializer\Annotation as Serializer;
use SignNow\Rest\Service\Serializer\Type\FileLink;

/**
* Class FileLinkUpload
*
* @HttpEntity("document", idProperty="")
* @GuzzleRequestBody("multipart")
* @ResponseType("Tests\Fixtures\Entity\StatusResponse")
*
* @package Tests\Fixtures\Entity\Upload
*/
class FileLinkUpload extends Entity
{
/**
* @Serializer\Type("SignNow\Rest\Service\Serializer\Type\FileLink")
* @var FileLink
*/
private $fileLink;

/**
* FileLinkUpload constructor.
*
* @param FileLink $fileLink
*/
public function __construct(FileLink $fileLink)
{
$this->fileLink = $fileLink;
}
}
39 changes: 39 additions & 0 deletions tests/fixtures/Entity/Upload/FileUpload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types = 1);

namespace Tests\Fixtures\Entity\Upload;

use SignNow\Rest\Entity\Entity;
use SignNow\Rest\EntityManager\Annotation\HttpEntity;
use SignNow\Rest\EntityManager\Annotation\GuzzleRequestBody;
use SignNow\Rest\EntityManager\Annotation\ResponseType;
use JMS\Serializer\Annotation as Serializer;
use SignNow\Rest\Service\Serializer\Type\File;

/**
* Class FileUpload
*
* @HttpEntity("document", idProperty="")
* @GuzzleRequestBody("multipart")
* @ResponseType("Tests\Fixtures\Entity\StatusResponse")
*
* @package Tests\Fixtures\Entity\Upload
*/
class FileUpload extends Entity
{
/**
* @Serializer\Type("SignNow\Rest\Service\Serializer\Type\File")
* @var File
*/
private $file;

/**
* FileUpload constructor.
*
* @param File $file
*/
public function __construct(File $file)
{
$this->file = $file;
}
}
47 changes: 47 additions & 0 deletions tests/fixtures/Entity/Upload/SplFileInfoUpload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types = 1);

namespace Tests\Fixtures\Entity\Upload;

use SignNow\Rest\Entity\Entity;
use SignNow\Rest\EntityManager\Annotation\HttpEntity;
use SignNow\Rest\EntityManager\Annotation\GuzzleRequestBody;
use SignNow\Rest\EntityManager\Annotation\ResponseType;
use JMS\Serializer\Annotation as Serializer;
use SplFileInfo;

/**
* Class SplFileInfoUpload
*
* @HttpEntity("document", idProperty="")
* @GuzzleRequestBody("multipart")
* @ResponseType("Tests\Fixtures\Entity\StatusResponse")
*
* @package Tests\Fixtures\Entity\Upload
*/
class SplFileInfoUpload extends Entity
{
/**
* @Serializer\Type("SplFileInfo")
* @var SplFileInfo
*/
private $file;

/**
* @Serializer\Type("array")
* @var array
*/
private $tags;

/**
* SplFileInfoUpload constructor.
*
* @param SplFileInfo $file
* @param array $tags
*/
public function __construct(SplFileInfo $file, array $tags = [])
{
$this->file = $file;
$this->tags = $tags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types = 1);

namespace Tests\Unit\EntityManager\Annotation\GuzzleRequestBody\Multipart;

use PHPUnit\Framework\TestCase;
use SignNow\Rest\EntityManager\Annotation\GuzzleRequestBody\Multipart\Item;

/**
* Class ItemTest
*
* @package Tests\Unit\EntityManager\Annotation\GuzzleRequestBody\Multipart
*/
class ItemTest extends TestCase
{
/**
* @return void
*/
public function testToArray(): void
{
$item = new Item('name', 'content', ['header' => 'value'], 'filename');
$toArray = $item->toArray();

$this->assertArrayHasKey('name', $toArray);
$this->assertArrayHasKey('contents', $toArray);
$this->assertArrayHasKey('filename', $toArray);
$this->assertArrayHasKey('headers', $toArray);
}
}
107 changes: 107 additions & 0 deletions tests/unit/EntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
use InvalidArgumentException;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
use LogicException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use ReflectionException;
use SignNow\Rest\Entity\Binary;
use SignNow\Rest\Entity\Collection\Errors;
use SignNow\Rest\Entity\Error;
use SignNow\Rest\EntityManager;
use SignNow\Rest\EntityManager\AnnotationFactory;
use SignNow\Rest\EntityManager\AnnotationResolver;
use SignNow\Rest\EntityManager\Exception\EntityManagerException;
use SignNow\Rest\EntityManager\Exception\PoolException;
use SignNow\Rest\Http\Request;
use SignNow\Rest\Service\Request\Pool;
use SignNow\Rest\Service\Request\Pool\Item;
Expand Down Expand Up @@ -321,6 +325,109 @@ public function testExceptionOnInvalidEntity(): void
$entityManager->get(SimpleEntity::class);
}

/**
* @throws EntityManagerException
* @throws ReflectionException
*/
public function testRequestPool(): void
{
$entityManager = new EntityManager(
$this->client,
$this->serializer,
$this->resolver,
$this->createPool('{"id": "030b82bb8671c23d5b8bc7808759aa605f48b8e0", "type": "signature"}')
);
$entityManager->openRequestPool();
$entityManager->get(FieldEntity::class, ['id' => '030b82bb8671c23d5b8bc7808759aa605f48b8e0']);
$responses = $entityManager->sendRequestPool();

$this->assertNotEmpty($responses);
}

/**
* @throws EntityManagerException
* @throws ReflectionException
*/
public function testAnErrorOnSendingRequestPoolWithoutRequest(): void
{
$this->expectException(LogicException::class);

$entityManager = new EntityManager($this->client, $this->serializer, $this->resolver, new Pool($this->client));
$entityManager->openRequestPool();
$entityManager->sendRequestPool();
}

/**
* @throws EntityManagerException
* @throws ReflectionException
*/
public function testAnErrorFromRequest(): void
{
$this->expectException(EntityManagerException::class);

$pool = $this->getMockBuilder(Pool::class)
->setConstructorArgs([$this->client])
->setMethods(['send', 'getErrors'])
->getMock();

$error = new Error();
$error->setMessage('some error');

$errorCollection = new Errors();
$errorCollection->push(0, $error);

$pool
->method('getErrors')
->willReturn($errorCollection);
$pool
->method('send')
->willThrowException(new PoolException());

$entityManager = new EntityManager($this->client, $this->serializer, $this->resolver, $pool);
$entityManager->openRequestPool();
$entityManager->get(FileEntity::class);
$entityManager->sendRequestPool();
}

/**
* @throws EntityManagerException
* @throws ReflectionException
*/
public function testRetrievingErrorsFormPool(): void
{
$pool = $this->getMockBuilder(Pool::class)
->setConstructorArgs([$this->client])
->setMethods(['send', 'getErrors'])
->getMock();

$response = $this->getMockForAbstractClass(ResponseInterface::class);
$error = new Error();
$error->setMessage('some error');
$error->setResponse($response);

$errorCollection = new Errors();
$errorCollection->push(0, $error);

$pool
->method('getErrors')
->willReturn($errorCollection);
$pool
->method('send')
->willThrowException(new PoolException());

$entityManager = new EntityManager($this->client, $this->serializer, $this->resolver, $pool);
$entityManager->openRequestPool();
$entityManager->get(FileEntity::class);
try {
$entityManager->sendRequestPool();
} catch (EntityManagerException $e) {
// skip exception to retrieve all errors
}

$this->assertNotEmpty($entityManager->getErrors());
$this->assertSame($response, $entityManager->getErrors()->getError(0)->getResponse());
}

/**
* @return array
*/
Expand Down
Loading

0 comments on commit f173722

Please sign in to comment.