Skip to content

Commit a542105

Browse files
authored
Merge pull request #12 from danitome24/3-pwd-vo
#3 - Added pwd vo
2 parents 91123b7 + f98f9e2 commit a542105

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <[email protected]>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\User;
9+
10+
use PhpValueObject\ValueObject;
11+
12+
final class Password implements ValueObject
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $pwd;
18+
19+
/**
20+
* Password constructor.
21+
* @param string $pwd
22+
*/
23+
private function __construct(string $pwd)
24+
{
25+
$this->pwd = $pwd;
26+
}
27+
28+
/**
29+
* Named constructor
30+
*
31+
* @param string $pwd
32+
* @return static
33+
*/
34+
public static function build(string $pwd)
35+
{
36+
return new static($pwd);
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function pwd(): string
43+
{
44+
return $this->pwd;
45+
}
46+
47+
/**
48+
* Compare a value object with another one.
49+
*
50+
* @param static|ValueObject $valueObjectToCompare
51+
* @return bool
52+
*/
53+
public function equals(ValueObject $valueObjectToCompare): bool
54+
{
55+
return ($this->pwd() === $valueObjectToCompare->pwd());
56+
}
57+
}

tests/User/PasswordTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <[email protected]>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Test\User;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use PhpValueObject\User\Password;
12+
13+
class PasswordTest extends TestCase
14+
{
15+
16+
public function testPasswordIsValid()
17+
{
18+
$passwd = 'somepassword';
19+
$pwd = Password::build($passwd);
20+
$this->assertEquals($passwd, $pwd->pwd());
21+
}
22+
23+
/**
24+
* @dataProvider passwords
25+
* @param Password $pwd
26+
* @param Password $pwdToCompare
27+
* @param bool $isEquals
28+
*/
29+
public function testPasswordIsEqualsMethod(Password $pwd, Password $pwdToCompare, bool $isEquals)
30+
{
31+
$this->assertEquals($pwd->equals($pwdToCompare), $isEquals);
32+
}
33+
34+
public function passwords()
35+
{
36+
return [
37+
[Password::build('somepwd'), Password::build('somepwd'), true],
38+
[Password::build('somepwd'), Password::build('somepwddif'), false]
39+
];
40+
}
41+
}

0 commit comments

Comments
 (0)