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 d245633 commit 3b40454
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 67 deletions.
14 changes: 0 additions & 14 deletions src/Model/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,4 @@ public function __construct(
public ?string $postCode
) {
}

public static function fromArray(array $data = []): self
{
/*$id = $data['id'] ?? null;
$name = $data['name'] ?? null;
$postCode = $data['postCode'] ?? null;
return new self($id, $name, $postCode); */
}

public function toArray(): array
{
return get_object_vars($this);
}
}
48 changes: 18 additions & 30 deletions src/Model/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,28 @@

namespace VasilDakov\Econt\Model;

final class Country
{
/*
id int [0..1]
code2 string [0..1] ISO 3166-1 alpha-2 code (e.g. BG, GB, GR)
code3 string [0..1] ISO 3166-1 alpha-3 code (e.g. BGR ,GBR, GRC)
name string [0..1] The bulgarian name of the country
nameEn string [0..1] The international name of the country
isEU boolean [0..1] True if country is a member of the EU
*/
use JMS\Serializer\Annotation as Serializer;

readonly class Country
{
public function __construct(
public readonly ?int $id,
public readonly ?string $code2,
public readonly ?string $code3,
public readonly ?string $name,
public readonly ?string $nameEn,
public readonly ?bool $isEU
) {
}
#[Serializer\Type('int')]
public ?int $id,

public static function fromArray(array $data): Country
{
list($id, $code2, $code3, $name, $nameEn, $isEU) = array_values($data);
#[Serializer\Type('string')]
public ?string $code2,

return new Country($id, $code2, $code3, $name, $nameEn, $isEU);
}
#[Serializer\Type('string')]
public ?string $code3,

#[Serializer\Type('string')]
public ?string $name,

public static function fromArray2(array $data = []): Country
{
foreach (get_object_vars($obj = new self) as $property => $default) {
if (!array_key_exists($property, $data)) continue;
$obj->{$property} = $data[$property]; // assign value to object
}
return $obj;
#[Serializer\Type('string')]
public ?string $nameEn,

#[Serializer\Type('bool')]
public ?bool $isEU
) {
}
}
28 changes: 26 additions & 2 deletions test/EcontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,39 @@
namespace VasilDakov\EcontTest;

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('username', 'password');
$this->client = $this->createMock(ClientInterface::class);
$this->factory = $this->createMock(RequestFactoryInterface::class);

parent::setUp();
}

/**
* @test
*/
public function itCanBeCreated(): void
public function itCanBeCreatedWithValidArguments(): void
{
self::assertInstanceOf(Econt::class, new Econt());
self::assertInstanceOf(
expected: Econt::class,
actual: new Econt(
configuration: $this->configuration,
client: $this->client,
factory: $this->factory
)
);
}
}
27 changes: 8 additions & 19 deletions test/Model/CityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use PHPUnit\Framework\TestCase;
use VasilDakov\Econt\Model\City;
use VasilDakov\Econt\Serializer\SerializerFactory;

class CityTest extends TestCase
{
/**
* @test
*/
public function itCanBeCreatedFromArray(): void
public function itCanBeCreatedDeserialized(): void
{
$city = City::fromArray($this->getData());
$json = json_encode($this->getData());
$serializer = (new SerializerFactory())();
$city = $serializer->deserialize($json, City::class, 'json');

self::assertInstanceOf(City::class, $city);
}
Expand All @@ -22,26 +25,12 @@ public function itCanBeCreatedFromArray(): void
*/
public function itCanBeCreatedFromEmptyArray(): void
{
$city = City::fromArray([]);

self::assertInstanceOf(City::class, $city);
self::assertInstanceOf(
City::class,
new City(null, null, null, null, null, null, null));
}


/**
* @test
*/
public function itCanBeConvertedToArray(): void
{
$city = City::fromArray([]);
$array = $city->toArray();

self::assertIsArray($array);
self::assertArrayHasKey('id', $array);
self::assertArrayHasKey('name', $array);
self::assertArrayHasKey('postCode', $array);
}


private function getData(): array
{
Expand Down
15 changes: 13 additions & 2 deletions test/Model/CountryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,26 @@ final class CountryTest extends TestCase
*/
public function itCanCreateFromArray(): void
{
self::assertInstanceOf(Country::class, Country::fromArray($this->getData()));
self::assertInstanceOf(
Country::class,
new Country(null, null, null, null, null, null)
);
}

/**
* @test
*/
public function itCanReadPublicReadonly(): void
{
$country = Country::fromArray($this->getData());
$data = $this->getData();
$country = new Country(
id: $data['id'],
code2: $data['code2'],
code3: $data['code3'],
name: $data['name'],
nameEn: $data['nameEn'],
isEU: $data['isEU']
);

self::assertEquals("AT", $country->code2);
self::assertEquals("AUT", $country->code3);
Expand Down

0 comments on commit 3b40454

Please sign in to comment.