Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Apr 10, 2021
1 parent 45c25e5 commit 4649eb6
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/MainEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types = 1);

namespace Graphpommando\Tests;

final class MainEntity implements \Graphpommando\Entity
{
public int $fieldInt;
public float $fieldFloat;
public bool $fieldBool;
public string $fieldString;
#[\Graphpommando\Attribute\Alias('fieldString')]
public string $fieldAliasedString;
public SubEntity $subfield;
#[\Graphpommando\Attribute\OfType('string', false)]
public array $listOfStrings;
#[\Graphpommando\Attribute\OfType('array', false, ['string', false])]
public array $matrixOfStrings;
#[\Graphpommando\Attribute\OfType(SubEntity::class, false)]
public array $listOfSubfields;
#[\Graphpommando\Attribute\OfType('array', false, [SubEntity::class, false])]
public array $matrixOfSubfields;
#[\Graphpommando\Attribute\Argument('arg1', \Graphpommando\Value\VariableValue::class, ['var1'])]
public int $fieldWithArgs;
#[\Graphpommando\Attribute\ConstructorPass]
public \DateTime $fieldDateTime;
public ?int $fieldNullable;
}
10 changes: 10 additions & 0 deletions tests/SubEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types = 1);

namespace Graphpommando\Tests;

final class SubEntity implements \Graphpommando\Entity
{
public int $fieldInt;
}
75 changes: 75 additions & 0 deletions tests/Unit/EntityHydratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types = 1);

namespace Graphpommando\Tests\Unit;

final class EntityHydratorTest extends \PHPUnit\Framework\TestCase
{
public function testSimple() : void
{
$hydrator = new \Graphpommando\EntityHydrator();
$result = $hydrator->hydrate(\Graphpommando\Tests\MainEntity::class, \Infinityloop\Utils\Json::fromNative((object) [
'data' => [
'fieldInt' => 1,
'fieldFloat' => 2.0,
'fieldBool' => true,
'fieldString' => '3',
'fieldAliasedString' => '4',
'subfield' => ['fieldInt' => 5],
'listOfStrings' => ['6', '7'],
'matrixOfStrings' => [['8'], [], ['9', '10']],
'listOfSubfields' => [['fieldInt' => 11], ['fieldInt' => 12]],
'matrixOfSubfields' => [[['fieldInt' => 13], ['fieldInt' => 14]], [['fieldInt' => 15]]],
'fieldWithArgs' => 16,
'fieldDateTime' => '2021-01-01',
'fieldNullable' => null,
],
])->toString());

self::assertInstanceOf(\Graphpommando\Tests\MainEntity::class, $result);
\assert($result instanceof \Graphpommando\Tests\MainEntity);
self::assertSame(1, $result->fieldInt);
self::assertSame(2.0, $result->fieldFloat);
self::assertTrue($result->fieldBool);
self::assertSame('3', $result->fieldString);
self::assertSame('4', $result->fieldAliasedString);
self::assertInstanceOf(\Graphpommando\Tests\SubEntity::class, $result->subfield);
self::assertSame(5, $result->subfield->fieldInt);
self::assertSame(16, $result->fieldWithArgs);
self::assertInstanceOf(\DateTime::class, $result->fieldDateTime);
self::assertSame($result->fieldDateTime->format('Y-m-d'), '2021-01-01');
self::assertNull($result->fieldNullable);
self::assertIsArray($result->listOfStrings);

foreach ($result->listOfStrings as $field) {
self::assertIsString($field);
}

self::assertIsArray($result->listOfSubfields);

foreach ($result->listOfSubfields as $field) {
self::assertInstanceOf(\Graphpommando\Tests\SubEntity::class, $field);
}

self::assertIsArray($result->matrixOfStrings);

foreach ($result->matrixOfStrings as $field) {
self::assertIsArray($field);

foreach ($field as $subField) {
self::assertIsString($subField);
}
}

self::assertIsArray($result->matrixOfSubfields);

foreach ($result->matrixOfSubfields as $field) {
self::assertIsArray($field);

foreach ($field as $subField) {
self::assertInstanceOf(\Graphpommando\Tests\SubEntity::class, $subField);
}
}
}
}
19 changes: 19 additions & 0 deletions tests/Unit/QueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types = 1);

namespace Graphpommando\Tests\Unit;

final class QueryBuilderTest extends \PHPUnit\Framework\TestCase
{
public function testSimple() : void
{
$builder = new \Graphpommando\QueryBuilder();
$operation = new \Graphpommando\DefaultOperation('query', [], [], \Graphpommando\Tests\MainEntity::class);

self::assertSame(
'query{fieldInt fieldFloat fieldBool fieldString fieldAliasedString: fieldString subfield{fieldInt} listOfStrings matrixOfStrings listOfSubfields{fieldInt} matrixOfSubfields{fieldInt} fieldWithArgs(arg1: $var1) fieldDateTime fieldNullable}',
$builder->build($operation),
);
}
}

0 comments on commit 4649eb6

Please sign in to comment.