Completed
Pull Request — master (#273)
by Gonçalo
04:11
created

ManagerTest::testParseIncludes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 0
loc 36
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php namespace League\Fractal\Test;
2
3
use League\Fractal\Manager;
4
use League\Fractal\ParamBag;
5
use League\Fractal\Resource\Collection;
6
use League\Fractal\Resource\Item;
7
use Mockery;
8
9
class ManagerTest extends \PHPUnit_Framework_TestCase
10
{
11
    public function testParseIncludeSelfie()
12
    {
13
        $manager = new Manager();
14
15
        // Test that some includes provided returns self
16
        $this->assertInstanceOf(get_class($manager), $manager->parseIncludes(['foo']));
17
    }
18
19
    /**
20
     * @expectedException InvalidArgumentException
21
     * @expectedExceptionMessage The parseIncludes() method expects a string or an array. NULL given
22
     */
23
    public function testInvalidParseInclude()
24
    {
25
        $manager = new Manager();
26
27
        $manager->parseIncludes(null);
28
    }
29
30
    /**
31
     * @expectedException InvalidArgumentException
32
     * @expectedExceptionMessage The parseIncludes() method expects a string or an array. integer given
33
     */
34
    public function testIceTParseInclude()
35
    {
36
        $manager = new Manager();
37
38
        $manager->parseIncludes(99);
39
    }
40
41
    public function testParseIncludes()
42
    {
43
        $manager = new Manager();
44
45
        // Does a CSV string work
46
        $manager->parseIncludes('foo,bar');
47
48
        $this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes());
49
50
        // Does a big array of stuff work
51
        $manager->parseIncludes(['foo', 'bar', 'bar.baz']);
52
        $this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedIncludes());
53
54
        // Are repeated things stripped
55
        $manager->parseIncludes(['foo', 'foo', 'bar']);
56
        $this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes());
57
58
        // 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...
59
        $manager->parseIncludes(['foo.bar']);
60
        $this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes());
61
62
63
        // See if fancy syntax works
64
        $manager->parseIncludes('foo:limit(5|1):order(-something):anotherparam');
65
66
        $params = $manager->getIncludeParams('foo');
67
68
        $this->assertInstanceOf('League\Fractal\ParamBag', $params);
69
70
71
        $this->assertSame(['5', '1'], $params['limit']);
72
        $this->assertSame(['-something'], $params['order']);
73
        $this->assertSame([''], $params['anotherparam']);
74
75
        $this->assertNull($params['totallymadeup']);
76
    }
77
78
    public function testParseExcludeSelfie()
79
    {
80
        $manager = new Manager();
81
82
        // Test that some excludes provided returns self
83
        $this->assertInstanceOf(get_class($manager), $manager->parseExcludes(['foo']));
84
    }
85
86
    /**
87
     * @expectedException InvalidArgumentException
88
     * @expectedExceptionMessage The parseExcludes() method expects a string or an array. NULL given
89
     */
90
    public function testInvalidParseExclude()
91
    {
92
        $manager = new Manager();
93
94
        $manager->parseExcludes(null);
95
    }
96
97
    /**
98
     * @expectedException InvalidArgumentException
99
     * @expectedExceptionMessage The parseExcludes() method expects a string or an array. integer given
100
     */
101
    public function testIceTParseExclude()
102
    {
103
        $manager = new Manager();
104
105
        $manager->parseExcludes(99);
106
    }
107
108
    public function testParseExcludes()
109
    {
110
        $manager = new Manager();
111
112
        // Does a CSV string work
113
        $manager->parseExcludes('foo,bar');
114
115
        $this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes());
116
117
        // Does a big array of stuff work
118
        $manager->parseExcludes(['foo', 'bar', 'bar.baz']);
119
        $this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedExcludes());
120
121
        // Are repeated things stripped
122
        $manager->parseExcludes(['foo', 'foo', 'bar']);
123
        $this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes());
124
125
        // 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...
126
        $manager->parseExcludes(['foo.bar']);
127
        $this->assertSame(['foo.bar'], $manager->getRequestedExcludes());
128
    }
129
130
    public function testRecursionLimiting()
131
    {
132
        $manager = new Manager();
133
134
        // Should limit to 10 by default
135
        $manager->parseIncludes('a.b.c.d.e.f.g.h.i.j.NEVER');
136
137
        $this->assertSame(
138
            [
139
                'a',
140
                'a.b',
141
                'a.b.c',
142
                'a.b.c.d',
143
                'a.b.c.d.e',
144
                'a.b.c.d.e.f',
145
                'a.b.c.d.e.f.g',
146
                'a.b.c.d.e.f.g.h',
147
                'a.b.c.d.e.f.g.h.i',
148
                'a.b.c.d.e.f.g.h.i.j',
149
            ],
150
            $manager->getRequestedIncludes()
151
        );
152
153
        // Try setting to 3 and see what happens
154
        $manager->setRecursionLimit(3);
155
        $manager->parseIncludes('a.b.c.NEVER');
156
157
        $this->assertSame(
158
            [
159
                'a',
160
                'a.b',
161
                'a.b.c',
162
            ],
163
            $manager->getRequestedIncludes()
164
        );
165
    }
166
167
    public function testCreateDataWithCallback()
168
    {
169
        $manager = new Manager();
170
171
        // Item
172
        $resource = new Item(['foo' => 'bar'], function (array $data) {
173
            return $data;
174
        });
175
176
        $rootScope = $manager->createData($resource);
177
178
        $this->assertInstanceOf('League\Fractal\Scope', $rootScope);
179
180
181
        $this->assertSame(['data' => ['foo' => 'bar']], $rootScope->toArray());
182
        $this->assertSame('{"data":{"foo":"bar"}}', $rootScope->toJson());
183
184
        // Collection
185
        $resource = new Collection([['foo' => 'bar']], function (array $data) {
186
            return $data;
187
        });
188
189
        $rootScope = $manager->createData($resource);
190
191
        $this->assertInstanceOf('League\Fractal\Scope', $rootScope);
192
193
194
        $this->assertSame(['data' => [['foo' => 'bar']]], $rootScope->toArray());
195
        $this->assertSame('{"data":[{"foo":"bar"}]}', $rootScope->toJson());
196
197
    }
198
199
    public function testParseFieldsets()
200
    {
201
        $manager = new Manager();
202
203
        $fields = [
204
            'articles' => 'title,body',
205
            'people' => 'name'
206
        ];
207
208
        $expectedFieldset = [
209
            'articles' => ['title' , 'body'],
210
            'people' => ['name']
211
        ];
212
213
        $manager->parseFieldsets($fields);
214
        $this->assertSame($expectedFieldset, $manager->getRequestedFieldsets());
215
216
        $paramBag = new ParamBag($expectedFieldset['articles']);
217
        $this->assertEquals($paramBag, $manager->getFieldset('articles'));
218
219
        // Are repeated fields stripped
220
        $manager->parseFieldsets(['foo' => 'bar,baz,bar']);
221
        $this->assertSame(['foo' => ['bar', 'baz']], $manager->getRequestedFieldsets());
222
223
        // Are empty fields stripped
224
        $manager->parseFieldsets(['foo' => 'bar,']);
225
        $this->assertSame(['foo' => ['bar']], $manager->getRequestedFieldsets());
226
227
        $this->assertSame(null, $manager->getFieldset('inexistent'));
228
    }
229
230
    public function tearDown()
231
    {
232
        Mockery::close();
233
    }
234
}
235