Completed
Push — master ( 6d78ae...fd9c92 )
by Maxim
04:03
created

AmoCRMClientTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 40.48 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 34
loc 84
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validSessionStorage() 0 5 1
A invalidSessionStorage() 0 7 1
A setUp() 0 16 1
A testExecAbstractMethod() 17 17 1
A testSessionException() 17 17 1

How to fix   Duplicated Code   

Duplicated Code

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
2
use GuzzleHttp\ClientInterface;
3
use GuzzleHttp\Psr7\Request;
4
use GuzzleHttp\Psr7\Response;
5
use mb24dev\AmoCRM\Exception;
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, Exception.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use mb24dev\AmoCRM\Method\MethodInterface;
7
use mb24dev\AmoCRM\ResponseTransformer\ResponseTransformerInterface;
8
use mb24dev\AmoCRM\Session\Session;
9
use mb24dev\AmoCRM\Session\SessionDoesNotExistException;
10
use mb24dev\AmoCRM\Session\SessionDoNotSavedException;
11
use mb24dev\AmoCRM\SessionStorage\SessionStorageInterface;
12
use mb24dev\AmoCRM\User\User;
13
14
/**
15
 * Class AmoCRMClientTest
16
 */
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 View Code Duplication
    public function testExecAbstractMethod()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testSessionException()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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