Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 8 | class UserTest extends TestCase |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var User |
||
| 12 | */ |
||
| 13 | protected $user; |
||
| 14 | |||
| 15 | public function setUp() |
||
| 16 | { |
||
| 17 | $this->user = $this->getMockForAbstractClass(User::class); |
||
| 18 | } |
||
| 19 | |||
| 20 | public function testUsername() |
||
| 21 | { |
||
| 22 | $this->assertNull($this->user->getUsername()); |
||
| 23 | |||
| 24 | $this->user->setUsername('awurth'); |
||
| 25 | $this->assertSame('awurth', $this->user->getUsername()); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function testEmail() |
||
| 29 | { |
||
| 30 | $this->assertNull($this->user->getEmail()); |
||
| 31 | |||
| 32 | $this->user->setEmail('[email protected]'); |
||
| 33 | $this->assertSame('[email protected]', $this->user->getEmail()); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testPassword() |
||
| 37 | { |
||
| 38 | $this->assertNull($this->user->getPassword()); |
||
| 39 | |||
| 40 | $this->user->setPassword('my_password'); |
||
| 41 | $this->assertSame('my_password', $this->user->getPassword()); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testPlainPassword() |
||
| 45 | { |
||
| 46 | $this->assertNull($this->user->getPlainPassword()); |
||
| 47 | |||
| 48 | $this->user->setPlainPassword('my_plain_password'); |
||
| 49 | $this->assertSame('my_plain_password', $this->user->getPlainPassword()); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testSalt() |
||
| 53 | { |
||
| 54 | $this->assertNull($this->user->getSalt()); |
||
| 55 | |||
| 56 | $salt = rtrim(str_replace('+', '.', base64_encode(random_bytes(32))), '='); |
||
| 57 | |||
| 58 | $this->user->setSalt($salt); |
||
| 59 | $this->assertSame($salt, $this->user->getSalt()); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testEnabled() |
||
| 63 | { |
||
| 64 | $this->assertFalse($this->user->isEnabled()); |
||
| 65 | |||
| 66 | $this->user->setEnabled(true); |
||
| 67 | $this->assertTrue($this->user->isEnabled()); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testConfirmationToken() |
||
| 71 | { |
||
| 72 | $this->assertNull($this->user->getConfirmationToken()); |
||
| 73 | |||
| 74 | $token = rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '='); |
||
| 75 | |||
| 76 | $this->user->setConfirmationToken($token); |
||
| 77 | $this->assertSame($token, $this->user->getConfirmationToken()); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testRoles() |
||
| 81 | { |
||
| 82 | $this->assertSame([User::ROLE_DEFAULT], $this->user->getRoles()); |
||
| 83 | |||
| 84 | $this->user->setRoles(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN']); |
||
| 85 | $this->assertSame(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN', User::ROLE_DEFAULT], $this->user->getRoles()); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testHasRole() |
||
| 89 | { |
||
| 90 | $defaultRole = User::ROLE_DEFAULT; |
||
| 91 | $newRole = 'ROLE_ADMIN'; |
||
| 92 | $this->assertTrue($this->user->hasRole($defaultRole)); |
||
| 93 | $this->assertFalse($this->user->hasRole($newRole)); |
||
| 94 | |||
| 95 | $this->user->addRole($defaultRole); |
||
| 96 | $this->assertTrue($this->user->hasRole($defaultRole)); |
||
| 97 | $this->user->addRole($newRole); |
||
| 98 | $this->assertTrue($this->user->hasRole($newRole)); |
||
| 99 | |||
| 100 | $this->user->removeRole($defaultRole); |
||
| 101 | $this->assertTrue($this->user->hasRole($defaultRole)); |
||
| 102 | $this->user->removeRole($newRole); |
||
| 103 | $this->assertFalse($this->user->hasRole($newRole)); |
||
| 104 | } |
||
| 105 | } |
||
| 106 |