These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Hateoas\Tests\Serializer; |
||
4 | |||
5 | use Hateoas\Configuration\Exclusion; |
||
6 | use Hateoas\Configuration\Relation; |
||
7 | use Hateoas\Serializer\ExclusionManager; |
||
8 | use JMS\Serializer\Context; |
||
9 | use JMS\Serializer\SerializationContext; |
||
10 | use Hateoas\Tests\TestCase; |
||
11 | use Prophecy\Argument; |
||
12 | |||
13 | class ExclusionManagerTest extends TestCase |
||
14 | { |
||
15 | View Code Duplication | public function testDoesNotSkipNonNullEmbedded() |
|
16 | { |
||
17 | $exclusionManager = new ExclusionManager($this->mockExpressionEvaluator()); |
||
18 | |||
19 | $object = new \StdClass(); |
||
20 | $relation = new Relation('foo', 'foo', 'foo'); |
||
21 | $context = SerializationContext::create(); |
||
22 | |||
23 | $this->assertFalse($exclusionManager->shouldSkipEmbedded($object, $relation, $context)); |
||
24 | } |
||
25 | |||
26 | View Code Duplication | public function testSkipNullEmbedded() |
|
27 | { |
||
28 | $exclusionManager = new ExclusionManager($this->mockExpressionEvaluator()); |
||
29 | |||
30 | $object = new \StdClass(); |
||
31 | $relation = new Relation('foo', 'foo'); |
||
32 | $context = SerializationContext::create(); |
||
33 | |||
34 | $this->assertTrue($exclusionManager->shouldSkipEmbedded($object, $relation, $context)); |
||
35 | } |
||
36 | |||
37 | View Code Duplication | public function testDoesNotSkipNonNullLink() |
|
0 ignored issues
–
show
|
|||
38 | { |
||
39 | $exclusionManager = new ExclusionManager($this->mockExpressionEvaluator()); |
||
40 | |||
41 | $object = new \StdClass(); |
||
42 | $relation = new Relation('foo', 'foo'); |
||
43 | $context = SerializationContext::create(); |
||
44 | |||
45 | $this->assertFalse($exclusionManager->shouldSkipLink($object, $relation, $context)); |
||
46 | } |
||
47 | |||
48 | View Code Duplication | public function testSkipNullLink() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
49 | { |
||
50 | $exclusionManager = new ExclusionManager($this->mockExpressionEvaluator()); |
||
51 | |||
52 | $object = new \StdClass(); |
||
53 | $relation = new Relation('foo', null, 'foo'); |
||
54 | $context = SerializationContext::create(); |
||
55 | |||
56 | $this->assertTrue($exclusionManager->shouldSkipLink($object, $relation, $context)); |
||
57 | } |
||
58 | |||
59 | public function testSkip() |
||
60 | { |
||
61 | $test = $this; |
||
62 | $exclusionStrategyCallback = function ($args) use ($test) { |
||
63 | $test->assertSame(['foo', 'bar'], $args[0]->groups); |
||
64 | $test->assertSame(1.1, $args[0]->sinceVersion); |
||
65 | $test->assertSame(1.7, $args[0]->untilVersion); |
||
66 | $test->assertSame(77, $args[0]->maxDepth); |
||
67 | }; |
||
68 | |||
69 | $exclusionManager = new ExclusionManager($this->mockExpressionEvaluator()); |
||
70 | $exclusionStrategy = $this->mockExclusionStrategy(true, $exclusionStrategyCallback, 2); |
||
71 | |||
72 | $object = new \StdClass(); |
||
73 | $exclusion = new Exclusion( |
||
74 | array('foo', 'bar'), |
||
75 | 1.1, |
||
76 | 1.7, |
||
77 | 77 |
||
78 | ); |
||
79 | $relation = new Relation('foo', 'foo', 'foo', array(), $exclusion); |
||
80 | $context = SerializationContext::create() |
||
81 | ->addExclusionStrategy($exclusionStrategy) |
||
82 | ; |
||
83 | |||
84 | $this->assertTrue($exclusionManager->shouldSkipLink($object, $relation, $context)); |
||
85 | $this->assertTrue($exclusionManager->shouldSkipEmbedded($object, $relation, $context)); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function testSkipEmbedded() |
|
89 | { |
||
90 | $exclusionManager = new ExclusionManager($this->mockExpressionEvaluator()); |
||
91 | |||
92 | $object = new \StdClass(); |
||
93 | $relation = new Relation('foo', 'foo', 'foo'); |
||
94 | $context = SerializationContext::create() |
||
95 | ->addExclusionStrategy($this->mockExclusionStrategy(true)) |
||
96 | ; |
||
97 | |||
98 | $this->assertTrue($exclusionManager->shouldSkipEmbedded($object, $relation, $context)); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @dataProvider getTestSkipExcludeIfData |
||
103 | */ |
||
104 | public function testSkipExcludeIf($exclude) |
||
105 | { |
||
106 | $object = (object) array('name' => 'adrien'); |
||
107 | $exclusion = new Exclusion(null, null, null, null, 'expr(stuff)'); |
||
108 | $relation = new Relation('foo', 'foo', 'foo', array(), $exclusion); |
||
109 | $context = SerializationContext::create(); |
||
110 | |||
111 | $expressionEvaluatorProphecy = $this->prophesizeExpressionEvaluator(); |
||
112 | $expressionEvaluatorProphecy |
||
113 | ->evaluate('expr(stuff)', $object) |
||
114 | ->willReturn($exclude) |
||
115 | ; |
||
116 | $exclusionManager = new ExclusionManager($expressionEvaluatorProphecy->reveal()); |
||
117 | |||
118 | $this->assertSame($exclude, $exclusionManager->shouldSkipLink($object, $relation, $context)); |
||
119 | $this->assertSame($exclude, $exclusionManager->shouldSkipEmbedded($object, $relation, $context)); |
||
120 | } |
||
121 | |||
122 | public function getTestSkipExcludeIfData() |
||
123 | { |
||
124 | return array( |
||
125 | array(true), |
||
126 | array(false), |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param \Closure $shouldSkipPropertyCallback |
||
132 | * @param integer $calledTimes |
||
133 | */ |
||
134 | private function mockExclusionStrategy($shouldSkipProperty = false, $shouldSkipPropertyCallback = null, $calledTimes = null) |
||
135 | { |
||
136 | $exclusionStrategyProphecy = $this->prophesize('JMS\Serializer\Exclusion\ExclusionStrategyInterface'); |
||
137 | $method = $exclusionStrategyProphecy |
||
138 | ->shouldSkipProperty( |
||
139 | Argument::type('Hateoas\Serializer\Metadata\RelationPropertyMetadata'), |
||
140 | Argument::type('JMS\Serializer\SerializationContext') |
||
141 | ) |
||
142 | ->will(function () use ($shouldSkipProperty, $shouldSkipPropertyCallback) { |
||
143 | if (null !== $shouldSkipPropertyCallback) { |
||
144 | call_user_func_array($shouldSkipPropertyCallback, func_get_args()); |
||
145 | } |
||
146 | |||
147 | return $shouldSkipProperty; |
||
148 | }) |
||
149 | ; |
||
150 | |||
151 | if (null !== $calledTimes) { |
||
152 | $method->shouldBeCalledTimes($calledTimes); |
||
153 | } |
||
154 | |||
155 | return $exclusionStrategyProphecy->reveal(); |
||
156 | } |
||
157 | |||
158 | private function mockExpressionEvaluator() |
||
159 | { |
||
160 | return $this->prophesizeExpressionEvaluator()->reveal(); |
||
161 | } |
||
162 | |||
163 | private function prophesizeExpressionEvaluator() |
||
164 | { |
||
165 | return $this->prophesize('Hateoas\Expression\ExpressionEvaluator'); |
||
166 | } |
||
167 | } |
||
168 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.