Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yzen-dev committed Jun 15, 2023
1 parent 2b865a4 commit c073a55
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 42 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -33,7 +33,7 @@
}
},
"require": {
"php": "^8.1"
"php": "^8.0"
},
"require-dev": {
"mockery/mockery": "1.5.1",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions src/Reflection/CacheReflectionProperty.php
Expand Up @@ -42,15 +42,7 @@ public function notTransform(): bool
{
return $this->notTransform;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}


/**
* @param string $name
*
Expand Down
9 changes: 3 additions & 6 deletions src/TransformUtils.php
Expand Up @@ -74,12 +74,9 @@ public static function mutationSetterToCamelCase(string $key): string
*
* @return string|null
*/
public static function getClassFromPhpDoc($phpDoc): ?string
public static function getClassFromPhpDoc(string $phpDoc): ?string
{
if (is_string($phpDoc)) {
preg_match('/array<([a-zA-Z\d\\\]+)>/', $phpDoc, $arrayType);
return $arrayType[1] ?? null;
}
return null;
preg_match('/array<([a-zA-Z\d\\\]+)>/', $phpDoc, $arrayType);
return $arrayType[1] ?? null;
}
}
8 changes: 2 additions & 6 deletions src/ValueCasting.php
Expand Up @@ -59,12 +59,8 @@ public function castAttribute(mixed $value): mixed
return $this->castEnum($value);
}

if ($this->property->type instanceof TransformableType) {
return (new Hydrator($this->config))
->create($this->property->type->name, $value);
}

return $value;
return (new Hydrator($this->config))
->create($this->property->type->name, $value);
}


Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/ClassTransformerFromCacheTest.php
Expand Up @@ -8,6 +8,7 @@
use ClassTransformer\HydratorConfig;
use PHPUnit\Framework\TestCase;
use Tests\ClearCache;
use Tests\Integration\DTO\PurchaseForCacheDto;
use Tests\Integration\DTO\UserDTO;
use Tests\Integration\DTO\ProductDTO;
use Tests\Integration\DTO\PurchaseDTO;
Expand All @@ -31,9 +32,9 @@ public function testRecursiveObject(): void
$data->orders = $this->getArrayUsers();

$purchaseDTO = (new Hydrator(new HydratorConfig(true)))
->create(PurchaseDto::class, $data);
->create(PurchaseForCacheDto::class, $data);

self::assertInstanceOf(PurchaseDTO::class, $purchaseDTO);
self::assertInstanceOf(PurchaseForCacheDto::class, $purchaseDTO);
self::assertInstanceOf(UserDTO::class, $purchaseDTO->user);
self::assertEquals($data->user->id, $purchaseDTO->user->id);
self::assertEquals($data->user->email, $purchaseDTO->user->email);
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/DTO/PurchaseForCacheDto.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Integration\DTO;

use ClassTransformer\Attributes\ConvertArray;

class PurchaseForCacheDto
{
#[ConvertArray(ProductDTO::class)]
public array $products;

/** @var UserDTO $user */
public UserDTO $user;

/** @var array<UserDTO> $orders Order list */
public array $clients;
}
26 changes: 18 additions & 8 deletions tests/Units/CacheGeneratorTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Units;

use ClassTransformer\Enums\TypeEnums;
use ClassTransformer\Exceptions\ClassNotFoundException;
use RuntimeException;
use Tests\ClearCache;
use Tests\Units\DTO\ColorEnum;
Expand Down Expand Up @@ -43,13 +44,20 @@ protected function setUp(): void
/**
* @throws \ReflectionException
*/
public function testGenerateException(): void
public function testGenerateRuntimeException(): void
{
$this->expectException(RuntimeException::class);
$dir = __DIR__ . '/CacheGeneratorTest.php';
$generator = new CacheGenerator(UserCacheableDTO::class, new HydratorConfig(true, $dir));
$dto = $generator->generate();
}

public function testGenerateClassNotFoundException(): void
{
$this->expectException(ClassNotFoundException::class);
$generator = new CacheGenerator('FakeTestClass');
$dto = $generator->generate();
}

public function testGenerateCache(): void
{
Expand All @@ -72,31 +80,33 @@ public function testGenerateCache(): void
$this->assertEquals(UserCacheableDTO::class, $property->class);
$this->assertEquals('id', $property->name);
$this->assertEquals('int', $property->type->name);
$this->assertFalse($property->hasSetMutator);
$this->assertFalse($property->notTransform);
$this->assertEmpty($property->docComment);
$this->assertIsArray($property->attributes);
$this->assertEmpty($property->attributes);
$this->assertFalse($property->hasSetMutator());
$this->assertFalse($property->notTransform());
$this->assertEmpty($property->getDocComment());

$property = $cache['properties'][2];

$this->assertEquals(UserCacheableDTO::class, $property->class);
$this->assertEquals('phone', $property->name);
$this->assertEquals(TypeEnums::TYPE_MIXED, $property->type->name);
$this->assertCount(1, $property->aliases);
$this->assertCount(1, $property->getAliases());

$property = $cache['properties'][5];
$this->assertIsArray($property->attributes);
$this->assertCount(1, $property->attributes);
$this->assertIsArray($property->attributes[WritingStyle::class]);
$this->assertEquals(WritingStyle::STYLE_CAMEL_CASE, $property->attributes[WritingStyle::class][0]);
$this->assertIsArray($property->getAttribute(WritingStyle::class));
$this->assertIsArray($property->getAttributeArguments(WritingStyle::class));

/** @var CacheReflectionProperty $property */
$property = $cache['properties'][8];
$this->assertInstanceOf(EnumType::class, $property->type);
$this->assertEquals(ColorEnum::class, $property->type->name);

$cacheGenerator->generate();
$cacheClass = $cacheGenerator->getClass();
$this->assertEquals(UserCacheableDTO::class, $cacheClass->getClass());

}

protected function tearDown(): void
Expand Down
20 changes: 20 additions & 0 deletions tests/Units/ClassExistsValidatorTest.php
@@ -0,0 +1,20 @@
<?php

namespace Tests\Units;

use ClassTransformer\Exceptions\ClassNotFoundException;
use ClassTransformer\Validators\ClassExistsValidator;
use ClassTransformer\ValueCasting;
use PHPUnit\Framework\TestCase;
use Tests\Units\DTO\ExtendedDto;
use ClassTransformer\Reflection\RuntimeReflectionProperty;

class ClassExistsValidatorTest extends TestCase
{

public function testCreateProperty(): void
{
$this->expectException(ClassNotFoundException::class);
new ClassExistsValidator('TestClass');
}
}
15 changes: 15 additions & 0 deletions tests/Units/RuntimeReflectionClassTest.php
Expand Up @@ -2,7 +2,9 @@

namespace Tests\Units;

use ClassTransformer\Exceptions\ClassNotFoundException;
use PHPUnit\Framework\TestCase;
use Tests\Integration\DTO\PurchaseDTO;
use Tests\Units\DTO\AbstractClass;
use ClassTransformer\Reflection\RuntimeReflectionClass;
use ClassTransformer\Exceptions\InstantiableClassException;
Expand All @@ -16,4 +18,17 @@ public function testInstantiable(): void
$instance = new RuntimeReflectionClass(AbstractClass::class);
$instance->getProperties();
}

public function testGetClass(): void
{
$instance = new RuntimeReflectionClass(PurchaseDTO::class);
$this->assertEquals(PurchaseDTO::class, $instance->getClass());
}

public function testGenerateClassNotFoundException(): void
{
$this->expectException(ClassNotFoundException::class);
$instance = new RuntimeReflectionClass('FakeTestClass');
$instance->getProperties();
}
}
10 changes: 8 additions & 2 deletions tests/Units/ValueCastingTest.php
Expand Up @@ -50,7 +50,13 @@ public function testCreateProperty(): void

public function testCreateArrayProperty(): void
{
// Array check

$caster = new ValueCasting(
new RuntimeReflectionProperty(new \ReflectionProperty(ExtendedDto::class, 'intItems'))
);
$value = $caster->castAttribute('1');
$this->assertIsString($value);
$this->assertEquals('1', $value);

$caster = new ValueCasting(
new RuntimeReflectionProperty(new \ReflectionProperty(ExtendedDto::class, 'intItems'))
Expand All @@ -72,7 +78,7 @@ public function testCreateArrayProperty(): void
$value = $caster->castAttribute([10]);
$this->assertIsString($value[0]);
$this->assertEquals('10', $value[0]);

$caster = new ValueCasting(
new RuntimeReflectionProperty(new \ReflectionProperty(ExtendedDto::class, 'boolItems'))
);
Expand Down

0 comments on commit c073a55

Please sign in to comment.