Issues (27)

Security Analysis    no request data  

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/Array2ObjectTest.php (1 issue)

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
/*
4
 * This file is part of the rafrsr/lib-array2object package.
5
 *
6
 * (c) Rafael SR <https://github.com/rafrsr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Rafrsr\LibArray2Object\Tests;
12
13
use Rafrsr\LibArray2Object\Array2ObjectBuilder;
14
use Rafrsr\LibArray2Object\Matcher\MapMatcher;
15
use Rafrsr\LibArray2Object\Parser\CallableParser;
16
use Rafrsr\LibArray2Object\Tests\Fixtures\Team;
17
18
class Array2ObjectTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testArray2Object()
21
    {
22
        $teamArray = [
23
            'id' => 1, //read-only
24
            'name' => 'Dream Team',
25
            'Manager' => [
26
                'name' => 'Big Manager',
27
                'salary' => '$10000',
28
            ],
29
            'createdAt' => '2016-01-01',
30
            'points' => '25',
31
            'players' => [
32
                [
33
                    'name' => 'Player 1',
34
                    'number' => '1',
35
                    'height' => '1.80',
36
                    'regular' => 'true',
37
                ],
38
                [
39
                    'name' => 'Player 2',
40
                    'number' => '2',
41
                    'height' => '1.85',
42
                    'regular' => 0,
43
                ],
44
                [
45
                    'name' => 'Player 3',
46
                    'number' => '3',
47
                    'height' => '1.78',
48
                    'regular' => 'yes',
49
                ],
50
            ],
51
            'scores' => [
52
                '2016' => '29',
53
                '2015' => '28',
54
                '2014' => '30',
55
            ],
56
        ];
57
58
        //register custom parser
59
        $array2Object = Array2ObjectBuilder::create()->addParser(
60
            new CallableParser(
61
                function ($value, $type, \ReflectionProperty $property, $object) {
0 ignored issues
show
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
                    if ($property->getName() === 'salary') {
63
                        $value = str_replace('$', null, $value);
64
                    }
65
66
                    return $value;
67
                }
68
            )
69
        )->build();
70
71
        /** @var Team $team */
72
        $team = $array2Object->createObject(Team::class, $teamArray);
73
        static::assertNull($team->getId());
74
        static::assertEquals('Dream Team', $team->getName());
75
        static::assertEquals(25, $team->getPoints());
76
        static::assertEquals(29, $team->getScores()[2016]);
77
        static::assertEquals('2016-01-01', $team->getCreatedAt()->format('Y-m-d'));
78
79
        static::assertEquals('Big Manager', $team->getManager()->getName());
80
        static::assertEquals(10000, $team->getManager()->getSalary());
81
82
        static::assertEquals('Player 1', $team->getPlayers()[0]->getName());
83
        static::assertEquals(1, $team->getPlayers()[0]->getNumber());
84
        static::assertEquals(1.80, $team->getPlayers()[0]->getHeight());
85
        static::assertTrue($team->getPlayers()[0]->isRegular());
86
87
        static::assertEquals('Player 2', $team->getPlayers()[1]->getName());
88
        static::assertEquals(2, $team->getPlayers()[1]->getNumber());
89
        static::assertEquals(1.85, $team->getPlayers()[1]->getHeight());
90
        static::assertFalse($team->getPlayers()[1]->isRegular());
91
92
        static::assertTrue($team->getPlayers()[2]->isRegular());
93
94
        static::assertInternalType('string', $team->getName());
95
        static::assertInternalType('integer', $team->getPoints());
96
        static::assertInternalType('float', $team->getManager()->getSalary());
97
        static::assertInternalType('boolean', $team->getPlayers()[0]->isRegular());
98
        static::assertInternalType('float', $team->getPlayers()[0]->getHeight());
99
        static::assertInternalType('boolean', $team->getPlayers()[2]->isRegular());
100
        static::assertInternalType('integer', $team->getScores()[2016]);
101
102
        $teamArray = [
103
            'name' => 'New Name',
104
        ];
105
106
        $array2Object->populate($team, $teamArray);
107
        static::assertEquals('New Name', $team->getName());
108
    }
109
110
    /**
111
     * @see https://github.com/rafrsr/lib-array2object/issues/1
112
     */
113
    public function testArray2ObjectWithNestingChildren()
114
    {
115
        $teamArray = [
116
            'name' => 'Dream Team',
117
            'players' => [
118
                'player' => [
119
                    [
120
                        'name' => 'Player 1',
121
                        'number' => '1',
122
                        'height' => '1.80',
123
                        'regular' => 'true',
124
                    ],
125
                    [
126
                        'name' => 'Player 2',
127
                        'number' => '2',
128
                        'height' => '1.85',
129
                        'regular' => 0,
130
                    ],
131
                ],
132
            ],
133
        ];
134
135
        $array2Object = Array2ObjectBuilder::create()->build();
136
137
        /** @var Team $team */
138
        $team = $array2Object->createObject(Team::class, $teamArray);
139
        static::assertEquals('Dream Team', $team->getName());
140
141
        static::assertEquals('Player 1', $team->getPlayers()[0]->getName());
142
        static::assertEquals(1, $team->getPlayers()[0]->getNumber());
143
        static::assertEquals(1.80, $team->getPlayers()[0]->getHeight());
144
        static::assertTrue($team->getPlayers()[0]->isRegular());
145
146
        static::assertEquals('Player 2', $team->getPlayers()[1]->getName());
147
        static::assertEquals(2, $team->getPlayers()[1]->getNumber());
148
        static::assertEquals(1.85, $team->getPlayers()[1]->getHeight());
149
        static::assertFalse($team->getPlayers()[1]->isRegular());
150
    }
151
152
    public function testArray2ObjectWithNestingChildrenOnlyOne()
153
    {
154
        //with only one children
155
        //https://github.com/rafrsr/lib-array2object/issues/1#issuecomment-228155603
156
        $teamArray = [
157
            'name' => 'Dream Team',
158
            'players' => [
159
                'player' => [
160
                    'name' => 'Player 1',
161
                    'number' => '1',
162
                    'height' => '1.80',
163
                    'regular' => 'true',
164
                ],
165
            ],
166
        ];
167
168
        $array2Object = Array2ObjectBuilder::create()->build();
169
170
        /** @var Team $team */
171
        $team = $array2Object->createObject(Team::class, $teamArray);
172
        static::assertEquals('Dream Team', $team->getName());
173
        static::assertEquals('Player 1', $team->getPlayers()[0]->getName());
174
        static::assertEquals(1, $team->getPlayers()[0]->getNumber());
175
        static::assertEquals(1.80, $team->getPlayers()[0]->getHeight());
176
        static::assertTrue($team->getPlayers()[0]->isRegular());
177
    }
178
179
    public function testPopulateObjectError()
180
    {
181
        static::setExpectedException('\InvalidArgumentException');
182
        Array2ObjectBuilder::create()->build()->populate(Team::class, []);
183
    }
184
185
    public function testCreateObjectError()
186
    {
187
        static::setExpectedException('\InvalidArgumentException');
188
        Array2ObjectBuilder::create()->build()->createObject(new Team(), []);
189
    }
190
191
    public function testSettingPropertyMatcher()
192
    {
193
        $array2Object = Array2ObjectBuilder::create()->setMatcher(
194
            new MapMatcher(
195
                [
196
                    'name' => 'nombre',
197
                ]
198
            )
199
        )->build();
200
        /** @var Team $team */
201
        $team = $array2Object->createObject(Team::class, ['nombre' => 'Team']);
202
        static::assertEquals('Team', $team->getName());
203
    }
204
205
    public function testDeserializeJson()
206
    {
207
        $json = '{"name":"Big Team","players":[{"name":"Player 1"}]}';
208
        /** @var Team $team */
209
        $team = Array2ObjectBuilder::create()->build()->createObject(Team::class, json_decode($json, true));
210
        static::assertEquals('Big Team', $team->getName());
211
        static::assertEquals('Player 1', $team->getPlayers()[0]->getName());
212
    }
213
}
214