/
EntityUserProviderTest.php
113 lines (87 loc) · 3.72 KB
/
EntityUserProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Bridge\Doctrine\Security\User;
require_once __DIR__.'/../../DoctrineOrmTestCase.php';
require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
use Symfony\Tests\Bridge\Doctrine\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity;
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
use Doctrine\ORM\Tools\SchemaTool;
class EntityUserProviderTest extends DoctrineOrmTestCase
{
public function testRefreshUserGetsUserByPrimaryKey()
{
$em = $this->createTestEntityManager();
$this->createSchema($em);
$user1 = new CompositeIdentEntity(1, 1, 'user1');
$user2 = new CompositeIdentEntity(1, 2, 'user2');
$em->persist($user1);
$em->persist($user2);
$em->flush();
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
// try to change the user identity
$user1->name = 'user2';
$this->assertSame($user1, $provider->refreshUser($user1));
}
public function testRefreshUserRequiresId()
{
$em = $this->createTestEntityManager();
$user1 = new CompositeIdentEntity(null, null, 'user1');
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
$this->setExpectedException(
'InvalidArgumentException',
'You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine'
);
$provider->refreshUser($user1);
}
public function testRefreshInvalidUser()
{
$em = $this->createTestEntityManager();
$this->createSchema($em);
$user1 = new CompositeIdentEntity(1, 1, 'user1');
$em->persist($user1);
$em->flush();
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
$user2 = new CompositeIdentEntity(1, 2, 'user2');
$this->setExpectedException(
'Symfony\Component\Security\Core\Exception\UsernameNotFoundException',
'User with id {"id1":1,"id2":2} not found'
);
$provider->refreshUser($user2);
}
public function testSupportProxy()
{
$em = $this->createTestEntityManager();
$this->createSchema($em);
$user1 = new CompositeIdentEntity(1, 1, 'user1');
$em->persist($user1);
$em->flush();
$em->clear();
$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
$user2 = $em->getReference('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', array('id1' => 1, 'id2' => 1));
$this->assertTrue($provider->supportsClass(get_class($user2)));
}
private function getManager($em, $name = null)
{
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$manager->expects($this->once())
->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($em));
return $manager;
}
private function createSchema($em)
{
$schemaTool = new SchemaTool($em);
$schemaTool->createSchema(array(
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity'),
));
}
}