-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathUserHydratorTest.php
More file actions
143 lines (121 loc) · 4.01 KB
/
Copy pathUserHydratorTest.php
File metadata and controls
143 lines (121 loc) · 4.01 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace ZfcUserTest\Mapper;
use ZfcUser\Entity\User as UserEntity;
use ZfcUser\Mapper\UserHydrator as Hydrator;
class UserHydratorTest extends \PHPUnit_Framework_TestCase
{
const ENCRYPTED_PASSWORD = 'c4zyP455w0rd!';
protected $hydrator;
public function setUp()
{
$hydrator = new Hydrator($this->buildCrypto());
$this->hydrator = $hydrator;
}
/**
* @covers ZfcUser\Mapper\UserHydrator::getCryptoService
*/
public function testGetCryptoServiceReturnsPasswordInterface()
{
$this->assertInstanceOf(
'Zend\Crypt\Password\PasswordInterface',
$this->hydrator->getCryptoService()
);
}
/**
* @covers ZfcUser\Mapper\UserHydrator::extract
* @expectedException ZfcUser\Mapper\Exception\InvalidArgumentException
*/
public function testExtractWithInvalidUserObject()
{
$user = new \StdClass;
$this->hydrator->extract($user);
}
/**
* @covers ZfcUser\Mapper\UserHydrator::extract
* @covers ZfcUser\Mapper\UserHydrator::mapField
* @dataProvider provideValidUserObjects
* @see https://github.com/ZF-Commons/ZfcUser/pull/421
*/
public function testExtractWithValidUserObject($object, $expectArray)
{
$result = $this->hydrator->extract($object);
$this->assertEquals($expectArray, $result);
}
/**
* @covers ZfcUser\Mapper\UserHydrator::hydrate
* @expectedException ZfcUser\Mapper\Exception\InvalidArgumentException
*/
public function testHydrateWithInvalidUserObject()
{
$user = new \StdClass;
$this->hydrator->hydrate(array(), $user);
}
/**
* @covers ZfcUser\Mapper\UserHydrator::hydrate
* @covers ZfcUser\Mapper\UserHydrator::mapField
*/
public function testHydrateWithValidUserObject()
{
$user = new \ZfcUser\Entity\User;
$expectArray = array(
'username' => 'zfcuser',
'email' => 'Zfc User',
'display_name' => 'ZfcUser',
'password' => 'c4zyP455w0rd!',
'user_id' => 1
);
$result = $this->hydrator->hydrate($expectArray, $user);
$this->assertEquals($expectArray['username'], $result->getUsername());
$this->assertEquals($expectArray['email'], $result->getEmail());
$this->assertEquals($expectArray['display_name'], $result->getDisplayName());
$this->assertEquals(static::ENCRYPTED_PASSWORD, $result->getPassword());
$this->assertEquals($expectArray['user_id'], $result->getId());
}
public function provideValidUserObjects()
{
$return = array();
$expectArray = array();
$buffer = array(
'username' => 'zfcuser',
'email' => 'Zfc User',
'display_name' => 'ZfcUser',
'password' => 'ZfcUserPassword',
'user_id' => 1
);
$return[]=array($this->buildUser($buffer), $buffer);
/**
* @see https://github.com/ZF-Commons/ZfcUser/pull/421
*/
$buffer = array(
'username' => 'zfcuser',
'email' => 'Zfc User',
'display_name' => 'ZfcUser',
'password' => 'ZfcUserPassword'
);
$return[]=array($this->buildUser($buffer), $buffer);
return $return;
}
private function buildUser(array $data)
{
$user = new UserEntity;
foreach ($data as $key => $value) {
if ($key == 'user_id') {
$key = 'id';
}
$method = 'set' . str_replace(" ", "", ucwords(str_replace("_", " ", $key)));
call_user_func(array($user, $method), $value);
}
return $user;
}
private function buildCrypto()
{
$crypto = $this->getMockForAbstractClass(
'Zend\Crypt\Password\PasswordInterface'
);
$crypto
->expects($this->any())
->method('create')
->will($this->returnValue(static::ENCRYPTED_PASSWORD));
return $crypto;
}
}