QuandlTest::cacheHandler()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.2
cc 4
eloc 9
nc 3
nop 3
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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)) 
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
The property $cache_file was declared of type boolean, but $cache_file is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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