Completed
Push — master ( 09eb6c...75b4b4 )
by Tamas
01:44
created

FirebaseTest::_parsePushResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    private function parsePushResponse($response)
108
    {
109
        $responseObj = json_decode($response);
110
        return $responseObj->name;
111
    }
112
}
113