Добавил тестов
This commit is contained in:
parent
dabeee4a72
commit
642dd026f1
51
tests/unit/Converter/ClassTransformersTest.php
Normal file
51
tests/unit/Converter/ClassTransformersTest.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\Converter;
|
||||
|
||||
use Rinsvent\Data2DTO\Data2DtoConverter;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloTagsRequest;
|
||||
|
||||
class ClassTransformersTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
protected $tester;
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
}
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
}
|
||||
|
||||
// tests
|
||||
public function testSuccessWithReturnData()
|
||||
{
|
||||
$data2DtoConverter = new Data2DtoConverter();
|
||||
|
||||
$data = [
|
||||
'surname' => 'Surname1234',
|
||||
'age' => 3,
|
||||
];
|
||||
$dto = $data2DtoConverter->convert($data, new HelloClassTransformersRequest);
|
||||
$this->assertInstanceOf(HelloClassTransformersRequest::class, $dto);
|
||||
$this->assertEquals(123454321, $dto->surname);
|
||||
}
|
||||
|
||||
public function testSuccessWithReturnObject()
|
||||
{
|
||||
$data2DtoConverter = new Data2DtoConverter();
|
||||
|
||||
$data = [
|
||||
'surname' => 'Surname1234',
|
||||
'age' => 3,
|
||||
];
|
||||
$dto = $data2DtoConverter->convert($data, new HelloClassTransformersRequest2());
|
||||
$this->assertInstanceOf(HelloClassTransformersRequest2::class, $dto);
|
||||
$this->assertEquals(98789, $dto->surname);
|
||||
}
|
||||
}
|
43
tests/unit/Converter/DataObjectTest.php
Normal file
43
tests/unit/Converter/DataObjectTest.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\Converter;
|
||||
|
||||
use Rinsvent\Data2DTO\Data2DtoConverter;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Bar;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\BuyRequest;
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloRequest;
|
||||
|
||||
class DataObjectTest extends \Codeception\Test\Unit
|
||||
{
|
||||
/**
|
||||
* @var \UnitTester
|
||||
*/
|
||||
protected $tester;
|
||||
|
||||
protected function _before()
|
||||
{
|
||||
}
|
||||
|
||||
protected function _after()
|
||||
{
|
||||
}
|
||||
|
||||
// tests
|
||||
public function testSuccessFillRequestData()
|
||||
{
|
||||
$data2DtoConverter = new Data2DtoConverter();
|
||||
|
||||
$buy = new BuyRequest();
|
||||
$buy->phrase = 'Buy buy!!!';
|
||||
$buy->length = 10;
|
||||
$buy->isFirst = true;
|
||||
|
||||
$dto = $data2DtoConverter->convert([
|
||||
'surname' => ' asdf',
|
||||
'buy' => $buy
|
||||
], new HelloRequest);
|
||||
|
||||
$this->assertInstanceOf(HelloRequest::class, $dto);
|
||||
$this->assertEquals($buy, $dto->buy);
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer\ClassData;
|
||||
|
||||
#[ClassData]
|
||||
class HelloClassTransformersRequest
|
||||
{
|
||||
public string $surname;
|
||||
public int $age;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest;
|
||||
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer\ClassObject;
|
||||
|
||||
#[ClassObject]
|
||||
class HelloClassTransformersRequest2
|
||||
{
|
||||
public string $surname;
|
||||
public int $age;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Data2DTO\Transformer\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class ClassData extends Meta
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Data2DTO\Transformer\Meta;
|
||||
use Rinsvent\Data2DTO\Transformer\TransformerInterface;
|
||||
|
||||
class ClassDataTransformer implements TransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param array|null $data
|
||||
* @param ClassData $meta
|
||||
*/
|
||||
public function transform(&$data, Meta $meta): void
|
||||
{
|
||||
if ($data === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($data['surname'])) {
|
||||
$data['surname'] = '123454321';
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Data2DTO\Transformer\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class ClassObject extends Meta
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\Transformer;
|
||||
|
||||
use Rinsvent\Data2DTO\Tests\unit\Converter\fixtures\FillTest\HelloClassTransformersRequest2;
|
||||
use Rinsvent\Data2DTO\Transformer\Meta;
|
||||
use Rinsvent\Data2DTO\Transformer\TransformerInterface;
|
||||
|
||||
class ClassObjectTransformer implements TransformerInterface
|
||||
{
|
||||
/**
|
||||
* @param array|null $data
|
||||
* @param ClassData $meta
|
||||
*/
|
||||
public function transform(&$data, Meta $meta): void
|
||||
{
|
||||
if ($data === null) {
|
||||
return;
|
||||
}
|
||||
$object = new HelloClassTransformersRequest2();
|
||||
$object->surname = '98789';
|
||||
$data = $object;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user