1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Royopa\Quandl\Tests; |
4
|
|
|
|
5
|
|
|
use Royopa\Quandl\Quandl; |
6
|
|
|
|
7
|
|
|
class QuandlTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
private $api_key = "DEBUG_KEY"; |
10
|
|
|
|
11
|
|
|
private $symbol = 'WIKI/AAPL'; |
12
|
|
|
|
13
|
|
|
private $symbols = [ |
14
|
|
|
'WIKI/CSCO', |
15
|
|
|
'WIKI/AAPL' |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
private $dates = [ |
19
|
|
|
'trim_start' => '2014-01-01', |
20
|
|
|
'trim_end' => '2014-02-02' |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
if (! file_exists(__DIR__.'/../.env')) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$dotenv = new \Dotenv\Dotenv(__DIR__.'/../'); |
30
|
|
|
$dotenv->load(); |
31
|
|
|
$this->api_key = getenv('QUANDL_API_KEY'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private $cache_file = false; |
35
|
|
|
|
36
|
|
|
public function tearDown() |
37
|
|
|
{ |
38
|
|
|
$this->cache_file and unlink($this->cache_file); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCsv() |
42
|
|
|
{ |
43
|
|
|
$this->_testGetSymbol("csv", 2800); |
44
|
|
|
$this->_testGetSymbol("csv", 2800, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testXml() |
48
|
|
|
{ |
49
|
|
|
$this->_testGetSymbol("xml", 14000); |
50
|
|
|
$this->_testGetSymbol("xml", 14000, true); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testJson() |
54
|
|
|
{ |
55
|
|
|
$this->_testGetSymbol("json", 4200); |
56
|
|
|
$this->_testGetSymbol("json", 4200, true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testObject() |
60
|
|
|
{ |
61
|
|
|
$this->_testGetSymbol("object", 7400); |
62
|
|
|
$this->_testGetSymbol("object", 7400, true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testInvalidUrl() |
66
|
|
|
{ |
67
|
|
|
$this->_testInvalidUrl(); |
68
|
|
|
$this->_testInvalidUrl(true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testCache() |
72
|
|
|
{ |
73
|
|
|
$this->_testCache(); |
74
|
|
|
$this->cache_file and unlink($this->cache_file); |
|
|
|
|
75
|
|
|
$this->_testCache(true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function cacheHandler($action, $url, $data=null) |
79
|
|
|
{ |
80
|
|
|
$cache_key = md5("quandl:$url"); |
81
|
|
|
$cache_file = __DIR__ . "/$cache_key"; |
82
|
|
|
|
83
|
|
|
if($action == "get" and file_exists($cache_file)) |
|
|
|
|
84
|
|
|
return file_get_contents($cache_file); |
85
|
|
|
else if($action == "set") |
86
|
|
|
file_put_contents($cache_file, $data); |
87
|
|
|
|
88
|
|
|
$this->cache_file = $cache_file; |
|
|
|
|
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function _testInvalidUrl($force_curl = false) |
94
|
|
|
{ |
95
|
|
|
$quandl = new Quandl($this->api_key, "json"); |
96
|
|
|
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl; |
97
|
|
|
$result = $quandl->getSymbol("INVALID/SYMBOL", $this->dates); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->assertEquals( |
100
|
|
|
$quandl->error, |
101
|
|
|
"URL not found or invalid URL", |
102
|
|
|
"TEST invalidUrl response" |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function _testGetList($force_curl = false) |
107
|
|
|
{ |
108
|
|
|
$quandl = new Quandl($this->api_key); |
109
|
|
|
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl; |
110
|
|
|
$result = $quandl->getList("WIKI", 1, 223); |
111
|
|
|
|
112
|
|
|
$this->assertEquals( |
113
|
|
|
223, |
114
|
|
|
count($result->docs), |
115
|
|
|
"TEST getList count" |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function _testGetSearch($force_curl = false) |
120
|
|
|
{ |
121
|
|
|
$quandl = new Quandl($this->api_key); |
122
|
|
|
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl; |
123
|
|
|
$result = $quandl->getSearch("crud oil", 1, 220); |
124
|
|
|
|
125
|
|
|
$this->assertEquals(220, count($result->docs), "TEST getSearch count"); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function _testCache($force_curl = false) |
129
|
|
|
{ |
130
|
|
|
$quandl = new Quandl($this->api_key); |
131
|
|
|
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl; |
132
|
|
|
$quandl->cache_handler = array($this, "cacheHandler"); |
133
|
|
|
$result = $quandl->getSymbol($this->symbol, $this->dates); |
134
|
|
|
$count = count($result->data); |
135
|
|
|
|
136
|
|
|
$this->assertFalse($quandl->was_cached, "TEST was_cache should be false"); |
137
|
|
|
|
138
|
|
|
$result = $quandl->getSymbol($this->symbol, $this->dates); |
139
|
|
|
|
140
|
|
|
$this->assertEquals( |
141
|
|
|
$count, |
142
|
|
|
count($result->data), |
143
|
|
|
"TEST count before and after cache should match" |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$this->assertTrue($quandl->was_cached, "TEST was_cache should be true"); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $format |
151
|
|
|
* @param integer $length |
152
|
|
|
*/ |
153
|
|
|
private function _testGetSymbol($format, $length, $force_curl = false) |
154
|
|
|
{ |
155
|
|
|
$quandl = new Quandl($this->api_key, $format); |
156
|
|
|
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl; |
157
|
|
|
$result = $quandl->getSymbol($this->symbol, $this->dates); |
158
|
|
|
$quandl_format = $format; |
159
|
|
|
|
160
|
|
|
if(is_object($result)) { |
161
|
|
|
$result = serialize($result); |
162
|
|
|
$quandl_format = "json"; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->assertGreaterThan( |
166
|
|
|
$length, |
167
|
|
|
strlen($result), |
168
|
|
|
"TEST $format length"); |
169
|
|
|
|
170
|
|
|
$url_expected = "https://www.quandl.com/api/v1/datasets/{$this->symbol}.{$quandl_format}?trim_start={$this->dates['trim_start']}&trim_end={$this->dates['trim_end']}&auth_token={$this->api_key}"; |
171
|
|
|
|
172
|
|
|
$this->assertEquals( |
173
|
|
|
$url_expected, |
174
|
|
|
$quandl->last_url, |
175
|
|
|
"TEST $format url"); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.