|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
|
7
|
|
|
* @package yii2-hiart |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hiqdev\hiart\tests\unit; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Psr7\Response; |
|
15
|
|
|
use hiqdev\hiart\Connection; |
|
16
|
|
|
use hiqdev\hiart\tests\Mock; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Connection test class. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ConnectionTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Connection |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $object; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Mock |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $mock; |
|
33
|
|
|
|
|
34
|
|
|
protected $site = 'http://test.api.site/'; |
|
35
|
|
|
protected $url = 'test/url'; |
|
36
|
|
|
protected $query = ['a' => 'b']; |
|
37
|
|
|
protected $body = ['c' => 'd']; |
|
38
|
|
|
protected $result = 'xyz result string'; |
|
39
|
|
|
|
|
40
|
|
|
protected function setUp() |
|
41
|
|
|
{ |
|
42
|
|
|
$response = new Response(200, [], $this->result); |
|
43
|
|
|
$this->mock = new Mock($response); |
|
44
|
|
|
$this->object = Yii::createObject([ |
|
45
|
|
|
'class' => Connection::className(), |
|
46
|
|
|
'handler' => $this->mock, |
|
47
|
|
|
'config' => [ |
|
48
|
|
|
'base_uri' => $this->site, |
|
49
|
|
|
], |
|
50
|
|
|
'errorChecker' => function ($res) { |
|
|
|
|
|
|
51
|
|
|
return null; |
|
52
|
|
|
}, |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function tearDown() |
|
57
|
|
|
{ |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testGet() |
|
61
|
|
|
{ |
|
62
|
|
|
$result = $this->object->get($this->url, [], $this->body, false); |
|
|
|
|
|
|
63
|
|
|
$this->assertSame($this->result, $result); |
|
64
|
|
|
$this->assertSame('request', $this->mock->name); |
|
65
|
|
|
$this->assertSame('GET', $this->mock->args[0]); |
|
66
|
|
|
$this->assertSame($this->url, $this->mock->args[1]); |
|
67
|
|
|
$this->assertSame($this->body, $this->mock->args[2]['form_params']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testErrorChecker() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->object->setErrorChecker(function ($res) { return $res; }); |
|
73
|
|
|
$this->setExpectedException('hiqdev\hiart\ErrorResponseException', $this->result); |
|
74
|
|
|
$this->object->get($this->url, [], $this->body, false); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.