Issues (10)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/unit/FractalServiceTest.php (7 issues)

Upgrade to new PHP Analysis Engine

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 Nord\Lumen\Fractal\Tests;
4
5
use League\Fractal\Pagination\Cursor;
6
use League\Fractal\Serializer\ArraySerializer;
7
use League\Fractal\Serializer\DataArraySerializer;
8
use Nord\Lumen\Fractal\FractalService;
9
use Nord\Lumen\Tests\Files\Book;
10
use Nord\Lumen\Tests\Files\BookPaginator;
11
use Nord\Lumen\Tests\Files\BookTransformer;
12
use Nord\Lumen\Tests\Files\Author;
13
14
class FractalServiceTest extends \Codeception\Test\Unit
15
{
16
    use \Codeception\Specify;
17
18
    /**
19
     * @var \Nord\Lumen\Fractal\FractalService
20
     */
21
    protected $service;
22
23
    /**
24
     * @var Author
25
     */
26
    private $author;
27
28
    /**
29
     * @var Book
30
     */
31
    private $book;
32
33
    /**
34
     * @var BookTransformer
35
     */
36
    private $transformer;
37
38
    /**
39
     * @inheritdoc
40
     */
41
    protected function setup()
42
    {
43
        $this->service = new FractalService();
44
        $this->service->setDefaultSerializer(new DataArraySerializer());
45
46
        $this->author = new Author('Test', 'Author');
47
        $this->book = new Book('Test Book', 'Test Publisher', $this->author);
48
        $this->transformer = new BookTransformer();
49
    }
50
51
    /**
52
     * Tests the fractal item.
53
     */
54
    public function testItem()
55
    {
56
        $this->specify('The key "data" is required.', function () {
57
            $item = $this->service->item($this->book, $this->transformer)->toArray();
58
            verify($item)->hasKey('data');
59
        });
60
61 View Code Duplication
        $this->specify('The book title must equal "Test Book".', function () {
0 ignored issues
show
This code seems to be duplicated across 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...
62
            $item = $this->service->item($this->book, $this->transformer)->toArray();
63
            verify($item['data']['title'])->equals('Test Book');
64
        });
65
    }
66
67
    /**
68
     * Tests the fractal collection.
69
     */
70
    public function testCollection()
71
    {
72
        $this->specify('Array does not contain specified amount of items', function () {
73
            $collection = $this->service->collection([$this->book], $this->transformer)->toArray();
74
            verify(count($collection['data']))->equals(1);
75
            verify(count($collection['data']))->equals(1);
76
        });
77
    }
78
79
    /**
80
     * Tests the Author include.
81
     */
82
    public function testAuthorInclude()
83
    {
84
        $this->specify('The key "data" is required.', function () {
85
            $item = $this->service->item($this->book, $this->transformer)->toArray();
86
            verify($item)->hasKey('data');
87
        });
88
89
        $this->specify('Author last name must be "Author".', function () {
90
            $item = $this->service->item($this->book, $this->transformer)->toArray();
91
            verify($item['data']['author'])->hasKey('data');
92
            verify($item['data']['author']['data'])->hasKey('lastName');
93
            verify($item['data']['author']['data']['lastName'])->equals('Author');
94
        });
95
    }
96
97
    /**
98
     * Tests the item&collection meta support.
99
     */
100
    public function testAssertHasMeta()
101
    {
102
        $meta = [
103
            'foo' => 'bar'
104
        ];
105
106 View Code Duplication
        $this->specify('The key "meta" is required for item.', function () use ($meta) {
0 ignored issues
show
This code seems to be duplicated across 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...
107
            $item = $this->service->item($this->book, $this->transformer)
108
                ->setMeta($meta)
109
                ->toArray();
110
            verify($item)->hasKey('meta');
111
            verify($item['meta'])->equals($meta);
112
        });
113
114 View Code Duplication
        $this->specify('The key "meta" is required for collection.', function () use ($meta) {
0 ignored issues
show
This code seems to be duplicated across 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...
115
            $collection = $this->service->collection([$this->book], $this->transformer)
116
                ->setMeta($meta)
117
                ->toArray();
118
            verify($collection)->hasKey('meta');
119
            verify($collection['meta'])->equals($meta);
120
        });
121
    }
122
123
    /**
124
     *
125
     */
126
    public function testAssertJson()
127
    {
128 View Code Duplication
        $this->specify('The item has to be in JSON format.', function () {
0 ignored issues
show
This code seems to be duplicated across 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...
129
            $item = $this->service->item($this->book, $this->transformer)->toJson();
130
            verify($item)->equals('{"data":{"title":"Test Book","publisher":"Test Publisher","author":{"data":{"firstName":"Test","lastName":"Author"}}}}');
131
        });
132
133 View Code Duplication
        $this->specify('The collection has to be in JSON format.', function () {
0 ignored issues
show
This code seems to be duplicated across 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...
134
            $collection = $this->service->collection([$this->book], $this->transformer)->toJson();
135
            verify($collection)->equals('{"data":[{"title":"Test Book","publisher":"Test Publisher","author":{"data":{"firstName":"Test","lastName":"Author"}}}]}');
136
        });
137
    }
138
139
    /**
140
     *
141
     */
142
    public function testAssertSerializer()
143
    {
144
        $this->specify('The serializer is array serializer', function () {
145
            $builder = $this->service->item($this->book, $this->transformer);
146
            $builder->setSerializer(new ArraySerializer());
147
            $item = $builder->toArray();
148
            verify($item)->equals([
149
                'title' => 'Test Book',
150
                'publisher' => 'Test Publisher',
151
                'author' => [
152
                    'firstName' => 'Test',
153
                    'lastName'  => 'Author',
154
                ]
155
            ]);
156
        });
157
158
        $this->specify('The serializer is data array serializer', function () {
159
            $builder = $this->service->item($this->book, $this->transformer);
160
            $builder->setSerializer(new DataArraySerializer());
161
            $item = $builder->toArray();
162
            verify($item)->equals([
163
                'data' =>  [
164
                    'title' => 'Test Book',
165
                    'publisher' => 'Test Publisher',
166
                    'author' => [
167
                        'data' => [
168
                            'firstName' => 'Test',
169
                            'lastName'  => 'Author',
170
                        ]
171
                    ]
172
                ]
173
            ]);
174
        });
175
    }
176
177
    /**
178
     *
179
     */
180
    public function testAssertResourceKey()
181
    {
182 View Code Duplication
        $this->specify('The fractal builder resource key can be set.', function () {
0 ignored issues
show
This code seems to be duplicated across 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...
183
            $item = $this->service->item($this->book, $this->transformer);
184
            $item->setResourceKey('book');
185
            $array = $item->toArray();
186
            verify($array)->hasKey('data');
187
        });
188
189
        $this->specify('The fractal service can set the builder resource key.', function () {
190
            $item = $this->service->item($this->book, $this->transformer, 'book')->toArray();
191
            verify($item)->hasKey('data');
192
        });
193
194
        $this->specify('The fractal builder resource key has to be valid.', function () {
195
            $item = $this->service->item($this->book, $this->transformer);
196
            $item->setResourceKey(123);
197
        }, ['throws' => 'Nord\Lumen\Fractal\Exceptions\InvalidArgument']);
198
    }
199
200
    /**
201
     *
202
     */
203
    public function testAssertPaginator()
204
    {
205 View Code Duplication
        $this->specify('The collection can have a paginator', function () {
0 ignored issues
show
This code seems to be duplicated across 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...
206
            $books = [$this->book];
207
            $collection = $this->service->collection($books, $this->transformer)
208
                ->setPaginator(new BookPaginator($books))
209
                ->toArray();
210
            verify($collection)->hasKey('meta');
211
            verify($collection['meta'])->hasKey('pagination');
212
        });
213
214
        $this->specify('The item cannot have a paginator', function () {
215
            $this->service->item($this->book, $this->transformer)
216
                ->setPaginator(new BookPaginator([]))
217
                ->toArray();
218
        }, ['throws' => 'Nord\Lumen\Fractal\Exceptions\NotApplicable']);
219
    }
220
221
    /**
222
     *
223
     */
224
    public function testAssertCursor()
225
    {
226
        $this->specify('The collection can have a cursor', function () {
227
            $collection = $this->service->collection([$this->book], $this->transformer)
228
                ->setCursor(new Cursor())
229
                ->toArray();
230
            verify(count($collection['data']))->equals(1);
231
        });
232
233
        $this->specify('The item cannot have a cursor', function () {
234
            $this->service->item($this->book, $this->transformer)
235
                ->setCursor(new Cursor())
236
                ->toArray();
237
        }, ['throws' => 'Nord\Lumen\Fractal\Exceptions\NotApplicable']);
238
    }
239
}
240