|
1
|
|
|
<?php |
|
2
|
|
|
use GuzzleHttp\ClientInterface; |
|
3
|
|
|
use GuzzleHttp\Psr7\Request; |
|
4
|
|
|
use GuzzleHttp\Psr7\Response; |
|
5
|
|
|
use mb24dev\AmoCRM\Exception; |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: