Completed
Push — master ( e0017c...6b1304 )
by Tom
14s queued 11s
created

Authentication/Adapter/ObjectRepositoryTest.php (7 issues)

Check that return values of null are not assigned.

Bug Minor

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
declare(strict_types=1);
4
5
namespace DoctrineModuleTest\Authentication\Adapter;
6
7
use DoctrineModule\Authentication\Adapter\ObjectRepository as ObjectRepositoryAdapter;
8
use DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject;
9
use DoctrineModuleTest\Authentication\Adapter\TestAsset\PublicPropertiesIdentityObject;
10
use PHPUnit\Framework\TestCase as BaseTestCase;
11
use stdClass;
12
use function crypt;
13
14
/**
15
 * Tests for the ObjectRepository based authentication adapter
16
 *
17
 * @link    http://www.doctrine-project.org/
18
 */
19
class ObjectRepositoryTest extends BaseTestCase
20
{
21
    public function testWillRejectInvalidIdentityProperty() : void
22
    {
23
        $this->expectException(
24
            'Laminas\Authentication\Adapter\Exception\InvalidArgumentException'
25
        );
26
        $this->expectExceptionMessage(
27
            'Provided $identityProperty is invalid, string given'
28
        );
29
30
        new ObjectRepositoryAdapter(['identity_property' => false]);
31
    }
32
33
    public function testWillRejectInvalidCredentialProperty() : void
34
    {
35
        $this->expectException(
36
            'Laminas\Authentication\Adapter\Exception\InvalidArgumentException'
37
        );
38
        $this->expectExceptionMessage(
39
            'Provided $credentialProperty is invalid, string given'
40
        );
41
        new ObjectRepositoryAdapter(['credential_property' => false]);
42
    }
43
44
    public function testWillRequireIdentityValue() : void
45
    {
46
        $this->expectException(
47
            'Laminas\Authentication\Adapter\Exception\RuntimeException'
48
        );
49
        $this->expectExceptionMessage(
50
            'A value for the identity was not provided prior to authentication with ObjectRepository authentication '
51
            . 'adapter'
52
        );
53
        $adapter = new ObjectRepositoryAdapter();
54
        $adapter->setOptions([
55
            'object_manager' => $this->createMock('Doctrine\Persistence\ObjectManager'),
56
            'identity_class' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject',
57
        ]);
58
        $adapter->setCredential('a credential');
59
        $adapter->authenticate();
60
    }
61
62
    public function testWillRequireCredentialValue() : void
63
    {
64
        $this->expectException(
65
            'Laminas\Authentication\Adapter\Exception\RuntimeException'
66
        );
67
        $this->expectExceptionMessage(
68
            'A credential value was not provided prior to authentication with ObjectRepository authentication adapter'
69
        );
70
        $adapter = new ObjectRepositoryAdapter();
71
        $adapter->setOptions([
72
            'object_manager' => $this->createMock('Doctrine\Persistence\ObjectManager'),
73
            'identity_class' => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject',
74
        ]);
75
76
        $adapter->setIdentity('an identity');
77
        $adapter->authenticate();
78
    }
79
80
    public function testWillRejectInvalidCredentialCallable() : void
81
    {
82
        $this->expectException(
83
            'Laminas\Authentication\Adapter\Exception\InvalidArgumentException'
84
        );
85
        $this->expectExceptionMessage(
86
            '"array" is not a callable'
87
        );
88
        $adapter = new ObjectRepositoryAdapter();
89
        $adapter->setOptions([
90
            'object_manager'      => $this->createMock('Doctrine\Persistence\ObjectManager'),
91
            'identity_class'      => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject',
92
            'credential_callable' => [],
93
        ]);
94
95
        $adapter->authenticate();
96
    }
97
98
    public function testAuthentication() : void
99
    {
100
        $entity = new IdentityObject();
101
        $entity->setUsername('a username');
102
        $entity->setPassword('a password');
103
104
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
0 ignored issues
show
Are you sure the assignment to $objectRepository is correct as $this->createMock('Doctr...nce\\ObjectRepository') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
105
        $method           = $objectRepository
106
            ->expects($this->exactly(2))
107
            ->method('findOneBy')
108
            ->with($this->equalTo(['username' => 'a username']))
109
            ->will($this->returnValue($entity));
110
111
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
0 ignored issues
show
Are you sure the assignment to $objectManager is correct as $this->createMock('Doctr...stence\\ObjectManager') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
112
        $objectManager->expects($this->exactly(2))
113
                      ->method('getRepository')
114
                      ->with($this->equalTo('DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject'))
115
                      ->will($this->returnValue($objectRepository));
116
117
        $adapter = new ObjectRepositoryAdapter();
118
        $adapter->setOptions([
119
            'object_manager'      => $objectManager,
120
            'identity_class'      => 'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject',
121
            'credential_property' => 'password',
122
            'identity_property'   => 'username',
123
        ]);
124
125
        $adapter->setIdentity('a username');
126
        $adapter->setCredential('a password');
127
128
        $result = $adapter->authenticate();
129
130
        $this->assertTrue($result->isValid());
131
        $this->assertInstanceOf(
132
            'DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject',
133
            $result->getIdentity()
134
        );
135
136
        $method->will($this->returnValue(null));
137
138
        $result = $adapter->authenticate();
139
140
        $this->assertFalse($result->isValid());
141
    }
142
143
    public function testAuthenticationWithPublicProperties() : void
144
    {
145
        $entity           = new PublicPropertiesIdentityObject();
146
        $entity->username = 'a username';
147
        $entity->password = 'a password';
148
149
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
0 ignored issues
show
Are you sure the assignment to $objectRepository is correct as $this->createMock('Doctr...nce\\ObjectRepository') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
150
        $method           = $objectRepository
151
            ->expects($this->exactly(2))
152
            ->method('findOneBy')
153
            ->with($this->equalTo(['username' => 'a username']))
154
            ->will($this->returnValue($entity));
155
156
        $adapter = new ObjectRepositoryAdapter();
157
        $adapter->setOptions([
158
            'object_repository' => $objectRepository,
159
            'credential_property' => 'password',
160
            'identity_property' => 'username',
161
        ]);
162
163
        $adapter->setIdentity('a username');
164
        $adapter->setCredential('a password');
165
166
        $result = $adapter->authenticate();
167
168
        $this->assertTrue($result->isValid());
169
170
        $method->will($this->returnValue(null));
171
172
        $result = $adapter->authenticate();
173
174
        $this->assertFalse($result->isValid());
175
    }
176
177
    public function testWillRefuseToAuthenticateWithoutGettersOrPublicMethods() : void
178
    {
179
        $this->expectException('Laminas\Authentication\Adapter\Exception\UnexpectedValueException');
180
181
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
0 ignored issues
show
Are you sure the assignment to $objectRepository is correct as $this->createMock('Doctr...nce\\ObjectRepository') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
182
        $objectRepository
183
            ->expects($this->once())
184
            ->method('findOneBy')
185
            ->with($this->equalTo(['username' => 'a username']))
186
            ->will($this->returnValue(new stdClass()));
187
188
        $adapter = new ObjectRepositoryAdapter();
189
        $adapter->setOptions([
190
            'object_repository' => $objectRepository,
191
            'credential_property' => 'password',
192
            'identity_property' => 'username',
193
        ]);
194
195
        $adapter->setIdentity('a username');
196
        $adapter->setCredential('a password');
197
        $adapter->authenticate();
198
    }
199
200
    public function testCanValidateWithSpecialCrypt() : void
201
    {
202
        $hash   = '$2y$07$usesomesillystringforsalt$';
203
        $entity = new IdentityObject();
204
        $entity->setUsername('username');
205
        // Crypt password using Blowfish
206
        $entity->setPassword(crypt('password', $hash));
207
208
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
0 ignored issues
show
Are you sure the assignment to $objectRepository is correct as $this->createMock('Doctr...nce\\ObjectRepository') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
209
        $objectRepository
210
            ->expects($this->exactly(2))
211
            ->method('findOneBy')
212
            ->with($this->equalTo(['username' => 'username']))
213
            ->will($this->returnValue($entity));
214
215
        $adapter = new ObjectRepositoryAdapter();
216
        $adapter->setOptions([
217
            'object_repository' => $objectRepository,
218
            'credential_property' => 'password',
219
            'identity_property' => 'username',
220
            // enforced type hinting to verify that closure is invoked correctly
221
            'credential_callable' => static function (IdentityObject $identity, $credentialValue) use ($hash) {
222
                return $identity->getPassword() === crypt($credentialValue, $hash);
223
            },
224
        ]);
225
226
        $adapter->setIdentity('username');
227
        $adapter->setCredential('password');
228
229
        $result = $adapter->authenticate();
230
231
        $this->assertTrue($result->isValid());
232
233
        $adapter->setCredential('wrong password');
234
        $result = $adapter->authenticate();
235
236
        $this->assertFalse($result->isValid());
237
    }
238
239
    public function testWillRefuseToAuthenticateWhenInvalidInstanceIsFound() : void
240
    {
241
        $this->expectException('Laminas\Authentication\Adapter\Exception\UnexpectedValueException');
242
243
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
0 ignored issues
show
Are you sure the assignment to $objectRepository is correct as $this->createMock('Doctr...nce\\ObjectRepository') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
244
        $objectRepository
245
            ->expects($this->once())
246
            ->method('findOneBy')
247
            ->with($this->equalTo(['username' => 'a username']))
248
            ->will($this->returnValue(new stdClass()));
249
250
        $adapter = new ObjectRepositoryAdapter();
251
        $adapter->setOptions([
252
            'object_repository'   => $objectRepository,
253
            'credential_property' => 'password',
254
            'identity_property'   => 'username',
255
        ]);
256
257
        $adapter->setIdentity('a username');
258
        $adapter->setCredential('a password');
259
260
        $adapter->authenticate();
261
    }
262
263
    public function testWillNotCastAuthCredentialValue() : void
264
    {
265
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
0 ignored issues
show
Are you sure the assignment to $objectRepository is correct as $this->createMock('Doctr...nce\\ObjectRepository') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
266
        $adapter          = new ObjectRepositoryAdapter();
267
        $entity           = new IdentityObject();
268
269
        $entity->setPassword(0);
270
        $adapter->setOptions([
271
            'object_repository'   => $objectRepository,
272
            'credential_property' => 'password',
273
            'identity_property'   => 'username',
274
        ]);
275
        $adapter->setIdentity('a username');
276
        $adapter->setCredential('00000');
277
        $objectRepository
278
            ->expects($this->once())
279
            ->method('findOneBy')
280
            ->with($this->equalTo(['username' => 'a username']))
281
            ->will($this->returnValue($entity));
282
283
        $this->assertFalse($adapter->authenticate()->isValid());
284
    }
285
}
286