|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace spec\Sylius\Bundle\CoreBundle\Form\DataTransformer; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
|
17
|
|
|
use Sylius\Bundle\CoreBundle\Form\DataTransformer\TaxonsToCodesTransformer; |
|
18
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
|
19
|
|
|
use Sylius\Component\Resource\Exception\UnexpectedTypeException; |
|
20
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
|
21
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class TaxonsToCodesTransformerSpec extends ObjectBehavior |
|
27
|
|
|
{ |
|
28
|
|
|
function let(TaxonRepositoryInterface $taxonRepository) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->beConstructedWith($taxonRepository); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
function it_is_initializable() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->shouldHaveType(TaxonsToCodesTransformer::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
function it_implements_data_transformer_interface() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->shouldImplement(DataTransformerInterface::class); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function it_transforms_array_of_taxons_codes_to_taxons_collection( |
|
44
|
|
|
TaxonRepositoryInterface $taxonRepository, |
|
45
|
|
|
TaxonInterface $bows, |
|
46
|
|
|
TaxonInterface $swords |
|
47
|
|
|
) { |
|
48
|
|
|
$taxonRepository->findBy(['code' => ['bows', 'swords']])->willReturn([$bows, $swords]); |
|
49
|
|
|
|
|
50
|
|
|
$taxons = new ArrayCollection([$bows->getWrappedObject(), $swords->getWrappedObject()]); |
|
51
|
|
|
|
|
52
|
|
|
$this->transform(['bows', 'swords'])->shouldBeCollection($taxons); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
function it_transforms_only_existing_taxons( |
|
56
|
|
|
TaxonRepositoryInterface $taxonRepository, |
|
57
|
|
|
TaxonInterface $bows |
|
58
|
|
|
) { |
|
59
|
|
|
$taxonRepository->findBy(['code' => ['bows', 'swords']])->willReturn([$bows]); |
|
60
|
|
|
|
|
61
|
|
|
$taxons = new ArrayCollection([$bows->getWrappedObject()]); |
|
62
|
|
|
|
|
63
|
|
|
$this->transform(['bows', 'swords'])->shouldBeCollection($taxons); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
function it_transforms_empty_array_into_empty_collection() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->transform([])->shouldBeCollection(new ArrayCollection([])); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function it_throws_exception_if_value_to_transform_is_not_array() |
|
72
|
|
|
{ |
|
73
|
|
|
$this |
|
74
|
|
|
->shouldThrow(new UnexpectedTypeException('badObject', 'array')) |
|
75
|
|
|
->during('transform', ['badObject']) |
|
76
|
|
|
; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
function it_reverse_transforms_into_array_of_taxons_codes( |
|
80
|
|
|
TaxonInterface $axes, |
|
81
|
|
|
TaxonInterface $shields |
|
82
|
|
|
) { |
|
83
|
|
|
$axes->getCode()->willReturn('axes'); |
|
84
|
|
|
$shields->getCode()->willReturn('shields'); |
|
85
|
|
|
|
|
86
|
|
|
$this |
|
87
|
|
|
->reverseTransform(new ArrayCollection([$axes->getWrappedObject(), $shields->getWrappedObject()])) |
|
88
|
|
|
->shouldReturn(['axes', 'shields']) |
|
89
|
|
|
; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function it_throws_exception_if_reverse_transformed_object_is_not_collection() |
|
93
|
|
|
{ |
|
94
|
|
|
$this |
|
95
|
|
|
->shouldThrow(new UnexpectedTypeException('badObject', Collection::class)) |
|
96
|
|
|
->during('reverseTransform', ['badObject']) |
|
97
|
|
|
; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function it_returns_empty_array_if_passed_collection_is_empty() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->reverseTransform(new ArrayCollection())->shouldReturn([]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getMatchers() |
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
|
|
'beCollection' => function ($subject, $key) { |
|
112
|
|
|
if (!$subject instanceof Collection || !$key instanceof Collection) { |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if ($subject->count() !== $key->count()) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
foreach ($subject as $subjectElement) { |
|
121
|
|
|
if (!$key->contains($subjectElement)) { |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return true; |
|
127
|
|
|
}, |
|
128
|
|
|
]; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|