1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Firebase; |
4
|
|
|
|
5
|
|
|
require_once __DIR__ . "/../src/firebaseLib.php"; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class FirebaseTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
protected $_firebase; |
12
|
|
|
protected $_todoMilk = array( |
13
|
|
|
'name' => 'Pick the milk', |
14
|
|
|
'priority' => 1 |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
protected $_todoBeer = array( |
18
|
|
|
'name' => 'Pick the beer', |
19
|
|
|
'priority' => 2 |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
protected $_todoLEGO = array( |
23
|
|
|
'name' => 'Pick the LEGO', |
24
|
|
|
'priority' => 3 |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
// --- set up your own database here |
28
|
|
|
const DEFAULT_URL = 'https://kidsplace.firebaseio.com/'; |
29
|
|
|
const DEFAULT_TOKEN = 'MqL0c8tKCtheLSYcygYNtGhU8Z2hULOFs9OKPdEp'; |
30
|
|
|
const DEFAULT_TODO_PATH = '/sample/todo'; |
31
|
|
|
const DELETE_PATH = '/sample'; |
32
|
|
|
const DEFAULT_SET_RESPONSE = '{"name":"Pick the milk","priority":1}'; |
33
|
|
|
const DEFAULT_UPDATE_RESPONSE = '{"name":"Pick the beer","priority":2}'; |
34
|
|
|
const DEFAULT_PUSH_RESPONSE = '{"name":"Pick the LEGO","priority":3}'; |
35
|
|
|
const DEFAULT_DELETE_RESPONSE = 'null'; |
36
|
|
|
const DEFAULT_URI_ERROR = 'You must provide a baseURI variable.'; |
37
|
|
|
|
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
$this->_firebase = new FirebaseLib(self::DEFAULT_URL, self::DEFAULT_TOKEN); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testNoBaseURI() |
44
|
|
|
{ |
45
|
|
|
$errorMessage = null; |
46
|
|
|
try { |
47
|
|
|
new FirebaseLib(); |
48
|
|
|
} catch (Exception $e) { |
49
|
|
|
$errorMessage = $e->getMessage(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->assertEquals(self::DEFAULT_URI_ERROR, $errorMessage); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testSet() |
56
|
|
|
{ |
57
|
|
|
$response = $this->_firebase->set(self::DEFAULT_TODO_PATH, $this->_todoMilk); |
58
|
|
|
$this->assertEquals(self::DEFAULT_SET_RESPONSE, $response); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetAfterSet() |
62
|
|
|
{ |
63
|
|
|
$response = $this->_firebase->get(self::DEFAULT_TODO_PATH); |
64
|
|
|
$this->assertEquals(self::DEFAULT_SET_RESPONSE, $response); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testUpdate() |
68
|
|
|
{ |
69
|
|
|
$response = $this->_firebase->update(self::DEFAULT_TODO_PATH, $this->_todoBeer); |
70
|
|
|
$this->assertEquals(self::DEFAULT_UPDATE_RESPONSE, $response); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetAfterUpdate() |
74
|
|
|
{ |
75
|
|
|
$response = $this->_firebase->get(self::DEFAULT_TODO_PATH); |
76
|
|
|
$this->assertEquals(self::DEFAULT_UPDATE_RESPONSE, $response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testPush() |
80
|
|
|
{ |
81
|
|
|
$response = $this->_firebase->push(self::DEFAULT_TODO_PATH, $this->_todoLEGO); |
82
|
|
|
$this->assertRegExp('/{"name"\s?:\s?".*?}/', $response); |
83
|
|
|
return $this->_parsePushResponse($response); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @depends testPush |
88
|
|
|
*/ |
89
|
|
|
public function testGetAfterPush($responseName) |
90
|
|
|
{ |
91
|
|
|
$response = $this->_firebase->get(self::DEFAULT_TODO_PATH . '/' . $responseName); |
92
|
|
|
$this->assertEquals(self::DEFAULT_PUSH_RESPONSE, $response); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testDelete() |
96
|
|
|
{ |
97
|
|
|
$response = $this->_firebase->delete(self::DELETE_PATH); |
98
|
|
|
$this->assertEquals(self::DEFAULT_DELETE_RESPONSE, $response); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testGetAfterDELETE() |
102
|
|
|
{ |
103
|
|
|
$response = $this->_firebase->get(self::DEFAULT_TODO_PATH); |
104
|
|
|
$this->assertEquals(self::DEFAULT_DELETE_RESPONSE, $response); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $response |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
private function _parsePushResponse($response) |
112
|
|
|
{ |
113
|
|
|
$responseObj = json_decode($response); |
114
|
|
|
return $responseObj->name; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.