Skip to content

Commit

Permalink
updated phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasildakov committed Dec 27, 2023
1 parent 3b40454 commit a8b0f98
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 48 deletions.
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
</include>
</coverage>
<testsuites>
<testsuite name="Econt Test Suite">
<directory>./test</directory>
<testsuite name="Unit">
<directory>./test/Unit</directory>
</testsuite>
<testsuite name="Integration">
<directory>./test/Integration</directory>
</testsuite>
</testsuites>
<logging>
Expand Down
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
$object = (new GetCitiesResponseFactory())($json);

dump($object->findById(39));
dump($object->findByName('Сливен'));
dump($object->findByName('Бургас'));
43 changes: 0 additions & 43 deletions test/EcontTest.php

This file was deleted.

64 changes: 64 additions & 0 deletions test/Integration/EcontTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace VasilDakov\EcontTest\Integration;

use GuzzleHttp\Client;
use Laminas\Diactoros\RequestFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use VasilDakov\Econt\Configuration;
use VasilDakov\Econt\Econt;

final class EcontTest extends TestCase
{
private Configuration $configuration;
private ClientInterface $client;
private RequestFactoryInterface $factory;


protected function setUp(): void
{
$this->configuration = new Configuration('iasp-dev','1Asp-dev');
//$this->client = $this->createMock(ClientInterface::class);
//$this->factory = $this->createMock(RequestFactoryInterface::class);

$this->client = new Client();
$this->factory = new RequestFactory();

parent::setUp();
}

/**
* @test
*/
public function itCanGetClientProfiles(): void
{
$econt = $this->getEcontClient();

$array = json_decode($econt->getClientProfiles(), true);
self::assertArrayHasKey(key: 'profiles', array: $array);
self::assertArrayHasKey(key: 'client', array: $array['profiles'][0]);
}

/**
* @test
*/
public function itCanGetCountries(): void
{
$econt = $this->getEcontClient();
$array = json_decode($econt->getCountries(), true);
self::assertArrayHasKey(key:'countries', array: $array);
}

private function getEcontClient(): Econt
{
return new Econt(
configuration: $this->configuration,
client: $this->client,
factory: $this->factory
);
}
}
151 changes: 151 additions & 0 deletions test/Unit/EcontTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace VasilDakov\EcontTest\Unit;

use Fig\Http\Message\RequestMethodInterface;
use GuzzleHttp\Client;
use Laminas\Diactoros\RequestFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use VasilDakov\Econt\Configuration;
use VasilDakov\Econt\Econt;
use VasilDakov\Econt\Request\GetCitiesRequest;
use function PHPUnit\Framework\once;

class EcontTest extends TestCase
{
private Configuration $configuration;
private ClientInterface $client;
private RequestFactoryInterface $factory;
private RequestInterface $request;
private ResponseInterface $response;
private StreamInterface $stream;

protected function setUp(): void
{
$this->configuration = new Configuration('iasp-dev','1Asp-dev');
$this->client = $this->createMock(ClientInterface::class);
$this->factory = $this->createMock(RequestFactoryInterface::class);
$this->request = $this->createMock(RequestInterface::class);
$this->response = $this->createMock(ResponseInterface::class);
$this->stream = $this->createMock(StreamInterface::class);

parent::setUp();
}

/**
* @test
*/
public function itCanBeCreatedWithValidArguments(): void
{
self::assertInstanceOf(
expected: Econt::class,
actual: new Econt(
configuration: $this->configuration,
client: $this->client,
factory: $this->factory
)
);
}

/**
* @test
*/
public function itCanGetCountries(): void
{
$json = file_get_contents('./data/GetCountriesResponse.json');

$econt = new Econt(
configuration: $this->configuration,
client: $this->client,
factory: $this->factory
);

$this->factory
->expects(self::once())
->method('createRequest')
->with(RequestMethodInterface::METHOD_POST)
->willReturn($this->request)
;

$this->request
->expects(self::exactly(2))
->method('withAddedHeader')
->willReturnOnConsecutiveCalls($this->request, $this->request)
;

$this->client
->expects(self::once())
->method('sendRequest')
->with($this->request)
->willReturn($this->response)
;

$this->response
->expects(self::once())
->method('getBody')
->willReturn($this->stream)
;

$this->stream
->expects(self::once())
->method('getContents')
->willReturn($json)
;

self::assertJson($econt->getCountries());
}


/**
* @test
*/
public function itCanGetCities(): void
{
$json = file_get_contents('./data/GetCitiesResponse.json');

$econt = new Econt(
configuration: $this->configuration,
client: $this->client,
factory: $this->factory
);

$this->factory
->expects(self::once())
->method('createRequest')
->with(RequestMethodInterface::METHOD_POST)
->willReturn($this->request)
;

$this->request
->expects(self::exactly(2))
->method('withAddedHeader')
->willReturnOnConsecutiveCalls($this->request, $this->request)
;

$this->client
->expects(self::once())
->method('sendRequest')
->with($this->request)
->willReturn($this->response)
;

$this->response
->expects(self::once())
->method('getBody')
->willReturn($this->stream)
;

$this->stream
->expects(self::once())
->method('getContents')
->willReturn($json)
;

self::assertJson($econt->getCities(new GetCitiesRequest('BGR')));
}
}
2 changes: 1 addition & 1 deletion test/Model/CityTest.php → test/Unit/Model/CityTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace VasilDakov\EcontTest\Model;
namespace VasilDakov\EcontTest\Unit\Model;

use PHPUnit\Framework\TestCase;
use VasilDakov\Econt\Model\City;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace VasilDakov\EcontTest\Model;
namespace VasilDakov\EcontTest\Unit\Model;

use PHPUnit\Framework\TestCase;
use VasilDakov\Econt\Model\Country;
Expand Down

0 comments on commit a8b0f98

Please sign in to comment.