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 |
||
| 14 | class DoctrineORMTokenRepositoryTest extends \PHPUnit_Framework_TestCase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var EntityManager|ObjectProphecy |
||
| 18 | */ |
||
| 19 | private $manager; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var EntityRepository|ObjectProphecy |
||
| 23 | */ |
||
| 24 | private $repository; |
||
| 25 | |||
| 26 | protected function setUp() |
||
| 27 | { |
||
| 28 | $this->manager = $this->prophesize(EntityManager::class); |
||
| 29 | $this->repository = $this->prophesize(EntityRepository::class); |
||
| 30 | } |
||
| 31 | |||
| 32 | protected function tearDown() |
||
| 33 | { |
||
| 34 | unset( |
||
| 35 | $this->manager, |
||
| 36 | $this->repository |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function repository() |
||
| 41 | { |
||
| 42 | return new DoctrineORMTokenRepository($this->manager->reveal(), $this->repository->reveal()); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @test |
||
| 47 | * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenNotFoundException |
||
| 48 | */ |
||
| 49 | public function it_throw_exception_if_token_not_found() |
||
| 50 | { |
||
| 51 | $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
||
| 52 | ->shouldBeCalledTimes(1) |
||
| 53 | ->willReturn(null); |
||
| 54 | |||
| 55 | $this->repository()->get('unique', 'init_password'); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @test |
||
| 60 | * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenExpiredException |
||
| 61 | */ |
||
| 62 | public function it_throw_exception_if_token_expired() |
||
| 63 | { |
||
| 64 | $token = new Token('string', 'jdoe','unique', 'init_password', '-1 day', []); |
||
| 65 | |||
| 66 | $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
||
| 67 | ->shouldBeCalledTimes(1) |
||
| 68 | ->willReturn($token); |
||
| 69 | |||
| 70 | $this->repository()->get('unique', 'init_password'); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @test |
||
| 75 | * @expectedException \Yokai\SecurityTokenBundle\Exception\TokenUsedException |
||
| 76 | */ |
||
| 77 | public function it_throw_exception_if_token_used() |
||
| 78 | { |
||
| 79 | $token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
||
| 80 | $token->setUsedAt(new \DateTime()); |
||
| 81 | $token->setUsedInformation(['info']); |
||
| 82 | |||
| 83 | $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
||
| 84 | ->shouldBeCalledTimes(1) |
||
| 85 | ->willReturn($token); |
||
| 86 | |||
| 87 | $this->repository()->get('unique', 'init_password'); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @test |
||
| 92 | */ |
||
| 93 | public function it_get_valid_token() |
||
| 94 | { |
||
| 95 | $token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
||
| 96 | |||
| 97 | $this->repository->findOneBy(['value' => 'unique', 'purpose' => 'init_password']) |
||
| 98 | ->shouldBeCalledTimes(1) |
||
| 99 | ->willReturn($token); |
||
| 100 | |||
| 101 | $got = $this->repository()->get('unique', 'init_password'); |
||
| 102 | |||
| 103 | self::assertSame($token, $got); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @test |
||
| 108 | */ |
||
| 109 | public function it_create_token() |
||
| 110 | { |
||
| 111 | $token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
||
| 112 | |||
| 113 | $this->manager->persist($token) |
||
| 114 | ->shouldBeCalledTimes(1); |
||
| 115 | $this->manager->flush($token) |
||
| 116 | ->shouldBeCalledTimes(1); |
||
| 117 | |||
| 118 | $this->repository()->create($token); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @test |
||
| 123 | */ |
||
| 124 | public function it_update_token() |
||
| 125 | { |
||
| 126 | $token = new Token('string', 'jdoe','unique', 'init_password', '+1 day', []); |
||
| 127 | |||
| 128 | $this->manager->persist($token) |
||
| 129 | ->shouldBeCalledTimes(1); |
||
| 130 | $this->manager->flush($token) |
||
| 131 | ->shouldBeCalledTimes(1); |
||
| 132 | |||
| 133 | $this->repository()->update($token); |
||
| 134 | } |
||
| 135 | } |
||
| 136 |