These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace League\Fractal\Test; |
||
2 | |||
3 | use League\Fractal\Manager; |
||
4 | use League\Fractal\Resource\Collection; |
||
5 | use League\Fractal\Resource\Item; |
||
6 | use Mockery; |
||
7 | |||
8 | class ManagerTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | public function testParseIncludeSelfie() |
||
11 | { |
||
12 | $manager = new Manager(); |
||
13 | |||
14 | // Test that some includes provided returns self |
||
15 | $this->assertInstanceOf(get_class($manager), $manager->parseIncludes(['foo'])); |
||
16 | } |
||
17 | |||
18 | /** |
||
19 | * @expectedException InvalidArgumentException |
||
20 | * @expectedExceptionMessage The parseIncludes() method expects a string or an array. NULL given |
||
21 | */ |
||
22 | public function testInvalidParseInclude() |
||
23 | { |
||
24 | $manager = new Manager(); |
||
25 | |||
26 | $manager->parseIncludes(null); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @expectedException InvalidArgumentException |
||
31 | * @expectedExceptionMessage The parseIncludes() method expects a string or an array. integer given |
||
32 | */ |
||
33 | public function testIceTParseInclude() |
||
34 | { |
||
35 | $manager = new Manager(); |
||
36 | |||
37 | $manager->parseIncludes(99); |
||
38 | } |
||
39 | |||
40 | public function testParseIncludes() |
||
41 | { |
||
42 | $manager = new Manager(); |
||
43 | |||
44 | // Does a CSV string work |
||
45 | $manager->parseIncludes('foo,bar'); |
||
46 | |||
47 | $this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes()); |
||
48 | |||
49 | // Does a big array of stuff work |
||
50 | $manager->parseIncludes(['foo', 'bar', 'bar.baz']); |
||
51 | $this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedIncludes()); |
||
52 | |||
53 | // Are repeated things stripped |
||
54 | $manager->parseIncludes(['foo', 'foo', 'bar']); |
||
55 | $this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes()); |
||
56 | |||
57 | // Do requests for `baz.bart` also request `baz`? |
||
0 ignored issues
–
show
|
|||
58 | $manager->parseIncludes(['foo.bar']); |
||
59 | $this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes()); |
||
60 | |||
61 | |||
62 | // See if fancy syntax works |
||
63 | $manager->parseIncludes('foo:limit(5|1):order(-something):anotherparam'); |
||
64 | |||
65 | $params = $manager->getIncludeParams('foo'); |
||
66 | |||
67 | $this->assertInstanceOf('League\Fractal\ParamBag', $params); |
||
68 | |||
69 | |||
70 | $this->assertSame(['5', '1'], $params['limit']); |
||
71 | $this->assertSame(['-something'], $params['order']); |
||
72 | $this->assertSame([''], $params['anotherparam']); |
||
73 | |||
74 | $this->assertNull($params['totallymadeup']); |
||
75 | } |
||
76 | |||
77 | public function testParseExcludeSelfie() |
||
78 | { |
||
79 | $manager = new Manager(); |
||
80 | |||
81 | // Test that some excludes provided returns self |
||
82 | $this->assertInstanceOf(get_class($manager), $manager->parseExcludes(['foo'])); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @expectedException InvalidArgumentException |
||
87 | * @expectedExceptionMessage The parseExcludes() method expects a string or an array. NULL given |
||
88 | */ |
||
89 | public function testInvalidParseExclude() |
||
90 | { |
||
91 | $manager = new Manager(); |
||
92 | |||
93 | $manager->parseExcludes(null); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @expectedException InvalidArgumentException |
||
98 | * @expectedExceptionMessage The parseExcludes() method expects a string or an array. integer given |
||
99 | */ |
||
100 | public function testIceTParseExclude() |
||
101 | { |
||
102 | $manager = new Manager(); |
||
103 | |||
104 | $manager->parseExcludes(99); |
||
105 | } |
||
106 | |||
107 | public function testParseExcludes() |
||
108 | { |
||
109 | $manager = new Manager(); |
||
110 | |||
111 | // Does a CSV string work |
||
112 | $manager->parseExcludes('foo,bar'); |
||
113 | |||
114 | $this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes()); |
||
115 | |||
116 | // Does a big array of stuff work |
||
117 | $manager->parseExcludes(['foo', 'bar', 'bar.baz']); |
||
118 | $this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedExcludes()); |
||
119 | |||
120 | // Are repeated things stripped |
||
121 | $manager->parseExcludes(['foo', 'foo', 'bar']); |
||
122 | $this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes()); |
||
123 | |||
124 | // Do requests for `baz.bart` also request `baz`? |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
37% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them.
Loading history...
|
|||
125 | $manager->parseExcludes(['foo.bar']); |
||
126 | $this->assertSame(['foo.bar'], $manager->getRequestedExcludes()); |
||
127 | } |
||
128 | |||
129 | public function testRecursionLimiting() |
||
130 | { |
||
131 | $manager = new Manager(); |
||
132 | |||
133 | // Should limit to 10 by default |
||
134 | $manager->parseIncludes('a.b.c.d.e.f.g.h.i.j.NEVER'); |
||
135 | |||
136 | $this->assertSame( |
||
137 | [ |
||
138 | 'a', |
||
139 | 'a.b', |
||
140 | 'a.b.c', |
||
141 | 'a.b.c.d', |
||
142 | 'a.b.c.d.e', |
||
143 | 'a.b.c.d.e.f', |
||
144 | 'a.b.c.d.e.f.g', |
||
145 | 'a.b.c.d.e.f.g.h', |
||
146 | 'a.b.c.d.e.f.g.h.i', |
||
147 | 'a.b.c.d.e.f.g.h.i.j', |
||
148 | ], |
||
149 | $manager->getRequestedIncludes() |
||
150 | ); |
||
151 | |||
152 | // Try setting to 3 and see what happens |
||
153 | $manager->setRecursionLimit(3); |
||
154 | $manager->parseIncludes('a.b.c.NEVER'); |
||
155 | |||
156 | $this->assertSame( |
||
157 | [ |
||
158 | 'a', |
||
159 | 'a.b', |
||
160 | 'a.b.c', |
||
161 | ], |
||
162 | $manager->getRequestedIncludes() |
||
163 | ); |
||
164 | } |
||
165 | |||
166 | public function testCreateDataWithCallback() |
||
167 | { |
||
168 | $manager = new Manager(); |
||
169 | |||
170 | // Item |
||
171 | $resource = new Item(['foo' => 'bar'], function (array $data) { |
||
172 | return $data; |
||
173 | }); |
||
174 | |||
175 | $rootScope = $manager->createData($resource); |
||
176 | |||
177 | $this->assertInstanceOf('League\Fractal\Scope', $rootScope); |
||
178 | |||
179 | |||
180 | $this->assertSame(['data' => ['foo' => 'bar']], $rootScope->toArray()); |
||
181 | $this->assertSame('{"data":{"foo":"bar"}}', $rootScope->toJson()); |
||
182 | |||
183 | // Collection |
||
184 | $resource = new Collection([['foo' => 'bar']], function (array $data) { |
||
185 | return $data; |
||
186 | }); |
||
187 | |||
188 | $rootScope = $manager->createData($resource); |
||
189 | |||
190 | $this->assertInstanceOf('League\Fractal\Scope', $rootScope); |
||
191 | |||
192 | |||
193 | $this->assertSame(['data' => [['foo' => 'bar']]], $rootScope->toArray()); |
||
194 | $this->assertSame('{"data":[{"foo":"bar"}]}', $rootScope->toJson()); |
||
195 | |||
196 | } |
||
197 | |||
198 | public function tearDown() |
||
199 | { |
||
200 | Mockery::close(); |
||
201 | } |
||
202 | } |
||
203 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.