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 |
||
17 | class AmoCRMClientTest extends PHPUnit_Framework_TestCase |
||
18 | { |
||
19 | |||
20 | /** @var ClientInterface|PHPUnit_Framework_MockObject_MockObject */ |
||
21 | private $client; |
||
22 | |||
23 | /** @var SessionStorageInterface|PHPUnit_Framework_MockObject_MockObject */ |
||
24 | private $sessionStorage; |
||
25 | |||
26 | /** @var ResponseTransformerInterface|PHPUnit_Framework_MockObject_MockObject */ |
||
27 | private $responseTransformer; |
||
28 | |||
29 | private function validSessionStorage() |
||
30 | { |
||
31 | $this->sessionStorage->method('save')->willReturn(null); |
||
32 | $this->sessionStorage->method('getActive')->willReturn(new Session('session_id')); |
||
33 | } |
||
34 | |||
35 | private function invalidSessionStorage() |
||
36 | { |
||
37 | $this->sessionStorage->method('save')->willThrowException(new SessionDoNotSavedException()); |
||
38 | $this->sessionStorage->method('getActive')->willThrowException( |
||
39 | new SessionDoesNotExistException(new User('domain', 'login', 'hash')) |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Sets up the fixture, for example, open a network connection. |
||
45 | * This method is called before a test is executed. |
||
46 | */ |
||
47 | protected function setUp() |
||
48 | { |
||
49 | $this->client = $this->getMockForAbstractClass(ClientInterface::class); |
||
50 | $this->client->method('send')->willReturn( |
||
51 | new Response(200) |
||
52 | ); |
||
53 | |||
54 | $this->sessionStorage = $this->getMockForAbstractClass( |
||
55 | SessionStorageInterface::class |
||
56 | ); |
||
57 | |||
58 | $this->responseTransformer = $this->getMockForAbstractClass(ResponseTransformerInterface::class); |
||
59 | $this->responseTransformer->method( |
||
60 | 'transform' |
||
61 | )->willReturn([]); |
||
62 | } |
||
63 | |||
64 | public function testExecAbstractMethod() |
||
65 | { |
||
66 | $this->validSessionStorage(); |
||
67 | $amoCRMClient = new \mb24dev\AmoCRM\AmoCRMClient( |
||
68 | $this->client, $this->sessionStorage, $this->responseTransformer |
||
69 | ); |
||
70 | |||
71 | $method = $this->getMockForAbstractClass(MethodInterface::class); |
||
72 | $method->method('getUser')->willReturn( |
||
73 | new User('domain', 'login', 'hash') |
||
74 | ); |
||
75 | |||
76 | $method->method('buildRequest')->willReturn(new Request('post', 'url')); |
||
77 | $method->method('getResponseTransformer')->willReturn(null); |
||
78 | |||
79 | $this->assertEquals([], $amoCRMClient->exec($method)); |
||
80 | } |
||
81 | |||
82 | public function testSessionException() |
||
83 | { |
||
84 | $this->invalidSessionStorage(); |
||
85 | $amoCRMClient = new \mb24dev\AmoCRM\AmoCRMClient( |
||
86 | $this->client, $this->sessionStorage, $this->responseTransformer |
||
87 | ); |
||
88 | |||
89 | $method = $this->getMockForAbstractClass(MethodInterface::class); |
||
90 | $method->method('getUser')->willReturn( |
||
91 | new User('domain', 'login', 'hash') |
||
92 | ); |
||
93 | |||
94 | $method->method('buildRequest')->willReturn(new Request('post', 'url')); |
||
95 | $method->method('getResponseTransformer')->willReturn(null); |
||
96 | $this->expectException(Exception::class); |
||
97 | $amoCRMClient->exec($method); |
||
98 | } |
||
99 | |||
100 | } |
||
101 |