Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add behat test data transformer initializer
- Loading branch information
Showing
10 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/Fixtures/TestBundle/DataTransformer/InitializeInputDataTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\DataTransformer; | ||
|
||
use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface; | ||
use ApiPlatform\Core\Serializer\AbstractItemNormalizer; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\InitializeInput as InitializeInputDocument; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\InitializeInputDto; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\InitializeInput; | ||
|
||
final class InitializeInputDataTransformer implements DataTransformerInitializerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transform($object, string $to, array $context = []) | ||
{ | ||
/** @var InitializeInputDto */ | ||
$data = $object; | ||
|
||
/** @var InitializeInput|InitializeInputDocument */ | ||
$resourceObject = $context[AbstractItemNormalizer::OBJECT_TO_POPULATE] ?? new $context['resource_class'](); | ||
$resourceObject->name = $data->name; | ||
|
||
return $resourceObject; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function initialize(string $inputClass, array $context = []) | ||
{ | ||
$currentResource = $context[AbstractItemNormalizer::OBJECT_TO_POPULATE] ?? null; | ||
if (!$currentResource) { | ||
return new InitializeInputDto(); | ||
} | ||
|
||
$dto = new InitializeInputDto(); | ||
$dto->manager = $currentResource->manager; | ||
|
||
return $dto; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsTransformation($data, string $to, array $context = []): bool | ||
{ | ||
return (InitializeInput::class === $to || InitializeInputDocument::class === $to) && InitializeInputDto::class === ($context['input']['class'] ?? null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\InitializeInputDto; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
/** | ||
* @ApiResource(input=InitializeInputDto::class) | ||
* @ODM\Document | ||
*/ | ||
class InitializeInput | ||
{ | ||
/** | ||
* @ODM\Id(strategy="NONE", type="integer") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ODM\Field | ||
*/ | ||
public $manager; | ||
|
||
/** | ||
* @ODM\Field | ||
*/ | ||
public $name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto; | ||
|
||
class InitializeInputDto | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $manager; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\InitializeInputDto; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ApiResource(input=InitializeInputDto::class) | ||
* @ORM\Entity | ||
*/ | ||
class InitializeInput | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\Column(type="integer") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ORM\Column | ||
*/ | ||
public $manager; | ||
|
||
/** | ||
* @ORM\Column | ||
*/ | ||
public $name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters