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 |
||
| 9 | class ProxyTestAbstract extends AbstractEntityManagerTest |
||
| 10 | { |
||
| 11 | 1 | public function testLazyProxy() |
|
| 12 | { |
||
| 13 | 1 | $manager = $this->getManager(); |
|
| 14 | |||
| 15 | 1 | $this->getResponseMock()->append( |
|
| 16 | 1 | new Response( |
|
| 17 | 1 | 200, |
|
| 18 | 1 | [], |
|
| 19 | 1 | json_encode( |
|
| 20 | [ |
||
| 21 | 1 | 'jsonrpc' => '2.0', |
|
| 22 | 1 | 'id' => 'test', |
|
| 23 | 'result' => [ |
||
| 24 | 1 | 'id' => 2, |
|
| 25 | 1 | 'payload' => 'test-payload', |
|
| 26 | 1 | 'sub-payload' => 'sub-payload', |
|
| 27 | 1 | ], |
|
| 28 | ] |
||
| 29 | 1 | ) |
|
| 30 | 1 | ) |
|
| 31 | 1 | ); |
|
| 32 | |||
| 33 | /** @var SubEntity|Proxy $entity */ |
||
| 34 | 1 | $entity = $manager->getReference(SubEntity::class, 2); |
|
| 35 | |||
| 36 | //Test that entity is a proxy and request was not send |
||
| 37 | 1 | self::assertInstanceOf(Proxy::class, $entity); |
|
| 38 | 1 | self::assertFalse($entity->__isInitialized()); |
|
| 39 | 1 | self::assertGreaterThan(0, $this->getResponseMock()->count()); |
|
| 40 | |||
| 41 | //Test that we can obtain ID and request was still not sent |
||
| 42 | 1 | self::assertEquals(2, $entity->getId()); |
|
| 43 | 1 | self::assertInstanceOf(Proxy::class, $entity); |
|
| 44 | 1 | self::assertFalse($entity->__isInitialized()); |
|
| 45 | 1 | self::assertGreaterThan(0, $this->getResponseMock()->count()); |
|
| 46 | |||
| 47 | //Test that we can obtain data and request was sent |
||
| 48 | 1 | self::assertInstanceOf(SubEntity::class, $entity); |
|
| 49 | 1 | self::assertEquals('test-payload', $entity->getPayload()); |
|
| 50 | 1 | self::assertEquals('sub-payload', $entity->getSubPayload()); |
|
| 51 | |||
| 52 | //Test that we are still a Proxy object |
||
| 53 | 1 | self::assertInstanceOf(Proxy::class, $entity); |
|
| 54 | 1 | self::assertTrue($entity->__isInitialized()); |
|
| 55 | 1 | self::assertEquals(0, $this->getResponseMock()->count()); |
|
| 56 | 1 | } |
|
| 57 | |||
| 58 | 1 | public function testSimpleProxy() |
|
| 59 | { |
||
| 60 | 1 | $repository = $this->getManager()->getRepository(SubEntity::class); |
|
| 61 | |||
| 62 | 1 | $this->getResponseMock()->append( |
|
| 63 | 1 | new Response( |
|
| 64 | 1 | 200, |
|
| 65 | 1 | [], |
|
| 66 | 1 | json_encode( |
|
| 67 | [ |
||
| 68 | 1 | 'jsonrpc' => '2.0', |
|
| 69 | 1 | 'id' => 'test', |
|
| 70 | 'result' => [ |
||
| 71 | 1 | 'id' => 2, |
|
| 72 | 1 | 'payload' => 'test-payload', |
|
| 73 | 1 | 'sub-payload' => 'sub-payload', |
|
| 74 | 1 | ], |
|
| 75 | ] |
||
| 76 | 1 | ) |
|
| 77 | 1 | ) |
|
| 78 | 1 | ); |
|
| 79 | |||
| 80 | /** @var SubEntity|Proxy $entity */ |
||
| 81 | 1 | $entity = $repository->find(2); |
|
| 82 | |||
| 83 | //Test that we can obtain data and request was sent |
||
| 84 | 1 | self::assertInstanceOf(SubEntity::class, $entity); |
|
| 85 | 1 | self::assertEquals(2, $entity->getId()); |
|
| 86 | 1 | self::assertEquals('test-payload', $entity->getPayload()); |
|
| 87 | 1 | self::assertEquals('sub-payload', $entity->getSubPayload()); |
|
| 88 | 1 | } |
|
| 89 | |||
| 90 | 2 | protected function getClientNames() |
|
| 91 | { |
||
| 92 | 2 | return [self::DEFAULT_CLIENT, 'test-reference-client']; |
|
| 93 | } |
||
| 94 | } |
||
| 95 |