forked from alienzin/php-eos-rpc-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathControllerTest.php
122 lines (101 loc) · 3.43 KB
/
ControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
use BlockMatrix\EosRpc\Adapter\Http\CurlAdapter;
use BlockMatrix\EosRpc\Adapter\Http\GuzzleAdapter;
use BlockMatrix\EosRpc\Adapter\Settings\DotenvAdapter;
use BlockMatrix\EosRpc\ChainController;
use BlockMatrix\EosRpc\Exception\HttpException;
use Curl\Curl;
use Dotenv\Dotenv;
use GuzzleHttp\Client;
use Mockery as m;
class ControllerTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
protected function getSettingsMock()
{
return m::mock(Dotenv::class)
->shouldReceive('load')
->shouldReceive('rpcNode')
->andReturn('https://eosapi.blockmatrix.network/')
->getMock();
}
protected function getPsrValidResponse()
{
return m::mock(\GuzzleHttp\Psr7\Response::class)
->shouldReceive('getBody')
->andReturn('{"success":true}')
->getMock();
}
public function testFetchApi()
{
$settings = m::mock(Dotenv::class)
->shouldReceive('load')
->getMock();
$http = m::mock(Client::class);
$api = new ChainController(new DotenvAdapter($settings), new GuzzleAdapter($http));
$this->assertInstanceOf(ChainController::class, $api);
}
public function testGetWorks()
{
$settings = $this->getSettingsMock();
$http = m::mock(Client::class)
->shouldReceive('get')
->andReturn($this->getPsrValidResponse())
->getMock();
$api = new ChainController(new DotenvAdapter($settings), new GuzzleAdapter($http));
$result = $api->getInfo();
$this->assertJson($result);
}
/**
* @expectedException BlockMatrix\EosRpc\Exception\HttpException
*/
public function testHttpGetException()
{
$settings = $this->getSettingsMock();
$http = m::mock(Client::class)
->shouldReceive('get')
->andThrow(new HttpException('cant connect to mothership'))
->getMock();
$api = new ChainController(new DotenvAdapter($settings), new GuzzleAdapter($http));
$response = $api->getInfo();
}
/**
* @expectedException BlockMatrix\EosRpc\Exception\SettingsNotFoundException
*/
public function testSettingsPathException()
{
$settings = m::mock(Dotenv::class)
->shouldReceive('load')
->andThrow(new \Dotenv\Exception\InvalidPathException)
->getMock();
$http = m::mock(Client::class);
$api = new ChainController(new DotenvAdapter($settings), new GuzzleAdapter($http));
}
/**
* @expectedException BlockMatrix\EosRpc\Exception\SettingsException
*/
public function testSettingsException()
{
$settings = m::mock(Dotenv::class)
->shouldReceive('load')
->andThrow(new \Exception)
->getMock();
$http = m::mock(Client::class);
$api = new ChainController(new DotenvAdapter($settings), new GuzzleAdapter($http));
}
public function testPostWorks()
{
$settings = $this->getSettingsMock();
$http = m::mock(Curl::class)
->shouldReceive('post')
->andReturn(['valid' => 'response'])
->shouldReceive('close')
->getMock();
$api = new ChainController(new DotenvAdapter($settings), new CurlAdapter($http));
$result = $api->getBlock('1337');
$this->assertJson($result);
}
}