Issues (404)

tests/AcceptApiTest.php (3 issues)

1
<?php
2
3
/**
4
 * Provides acceptance(ish) tests for API functions.
5
 */
6
class AcceptApiTest extends FetchPageTestCase {
7
    /**
8
     * Loads the api testing fixture.
9
     */
10
    public function getDataSet() {
11
        return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/_fixtures/api.xml');
0 ignored issues
show
Are you sure the usage of $this->createMySQLXMLDat.... '/_fixtures/api.xml') targeting TWFY_Database_TestCase::createMySQLXMLDataSet() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
12
    }
13
14
    private function fetch_page($method, $vars = []) {
15
        $vars['method'] = $method;
16
        return $this->base_fetch_page($vars, 'api');
17
    }
18
19
    private function get_page($page, $vars = []) {
20
        return $this->base_fetch_page_user($vars, '1.fbb689a0c092f5534b929d302db2c8a9', 'api', "$page.php");
21
    }
22
23
    private function post_page($page, $vars = []) {
24
        return $this->base_post_page_user($vars, '1.fbb689a0c092f5534b929d302db2c8a9', 'api', "$page.php");
25
    }
26
27
    /**
28
     * Ensure that not providing a key throws the right error
29
     */
30
    public function testMissingKeyFailure() {
31
        $page = $this->fetch_page('getConstituencies');
32
        $this->assertEquals('{"error":"No API key provided. Please see https://www.theyworkforyou.com/api/key for more information."}', $page);
33
    }
34
35
    /**
36
     * Ensure that providing an incorrect key throws the right error
37
     */
38
    public function testIncorrectKeyFailure() {
39
        $page = $this->fetch_page('getConstituencies', [
40
            'key' => 'invalid_key',
41
        ]);
42
        $this->assertEquals('{"error":"Invalid API key."}', $page);
43
    }
44
45
    private function _testGetJSON($page, $key, $value, $result) {
46
        $page = $this->fetch_page($page, [
47
            'key' => 'test_key',
48
            $key => $value,
49
        ]);
50
        $this->assertNotNull(json_decode($page));
51
        $this->assertNotNull(json_decode($result));
52
        $this->assertEquals(json_decode($result), json_decode($page));
53
    }
54
55
    public function testGetConstituencies() {
56
        $this->_testGetJSON('getConstituencies', 'dummy', 1, '[
57
            {"name":"Alyn and Deeside"},
58
            {"name":"Amber Valley"},
59
            {"name":"Belfast West"},
60
            {"name":"Cities of London and Westminster"},
61
            {"name":"Cities of \\"London\\" and Westminster"}
62
            ]');
63
    }
64
65
    public function testGetConstituencyByName() {
66
        $this->_testGetJSON(
67
            'getConstituency',
68
            'name',
69
            'Amber Valley',
70
            '{"name":"Amber Valley"}'
71
        );
72
    }
73
74
    public function testGetConstituencyByPostcode() {
75
        $this->_testGetJSON(
76
            'getConstituency',
77
            'postcode',
78
            'SW1A 1AA',
79
            '{"name":"Cities of London and Westminster"}'
80
        );
81
    }
82
83
    public function testGetConstituencyByAlternateName() {
84
        $this->_testGetJSON(
85
            'getConstituency',
86
            'name',
87
            'Alyn & Deeside',
88
            '{"name":"Alyn and Deeside"}'
89
        );
90
    }
91
92
    public function testGetConstituencyByIncorrectName() {
93
        $this->_testGetJSON(
94
            'getConstituency',
95
            'name',
96
            'No Such Constituency',
97
            '{"error":"Could not find anything with that name"}'
98
        );
99
    }
100
101
    public function testGetMpByPostcode() {
102
        $this->_testGetJSON(
103
            'getMP',
104
            'postcode',
105
            'SW1A 1AA',
106
            '{"member_id":"2","house":"1","given_name":"Test","family_name":"Current-City-MP","constituency":"Cities of London and Westminster","party":"Labour","entered_house":"2000-01-01","left_house":"9999-12-31","entered_reason":"general_election","left_reason":"still_in_office","person_id":"3","title":"Mr","lastupdate":"2013-08-07 15:06:19","full_name":"Mr Test Current-City-MP","url":"/mp/3/mr_test_current-city-mp/cities_of_london_and_westminster"}'
107
        );
108
    }
109
110
    public function testGetMpByConstituency() {
111
        $this->_testGetJSON(
112
            'getMP',
113
            'constituency',
114
            'Amber Valley',
115
            '{"member_id":"1","house":"1","given_name":"Test","family_name":"Current-MP","constituency":"Amber Valley","party":"Labour","entered_house":"2000-01-01","left_house":"9999-12-31","entered_reason":"general_election","left_reason":"still_in_office","person_id":"2","title":"Mrs","lastupdate":"2013-08-07 15:06:19","full_name":"Mrs Test Current-MP","url":"/mp/2/mrs_test_current-mp/amber_valley"}'
116
        );
117
    }
118
119
    public function testGetMpById() {
120
        $this->_testGetJSON(
121
            'getMP',
122
            'id',
123
            '2',
124
            '[{"member_id":"1","house":"1","given_name":"Test","family_name":"Current-MP","constituency":"Amber Valley","party":"Labour","entered_house":"2000-01-01","left_house":"9999-12-31","entered_reason":"general_election","left_reason":"still_in_office","person_id":"2","title":"Mrs","lastupdate":"2013-08-07 15:06:19","full_name":"Mrs Test Current-MP","url":"/mp/2/mrs_test_current-mp/amber_valley"}]'
125
        );
126
    }
127
128
    public function testGetMlasLookup() {
129
        $result = '[
130
{"member_id":"101","house":"3","given_name":"Test1","family_name":"Nimember","constituency":"Belfast West","party":"DUP","entered_house":"2000-01-01","left_house":"9999-12-31","entered_reason":"general_election","left_reason":"still_in_office","person_id":"101","title":"Mr","lastupdate":"2013-08-07 15:06:19","full_name":"Mr Test1 Nimember"},
131
{"member_id":"102","house":"3","given_name":"Test2","family_name":"Nimember","constituency":"Belfast West","party":"DUP","entered_house":"2000-01-01","left_house":"9999-12-31","entered_reason":"general_election","left_reason":"still_in_office","person_id":"102","title":"Ms","lastupdate":"2013-08-07 15:06:19","full_name":"Ms Test2 Nimember"}
132
        ]';
133
        $this->_testGetJSON('getMLA', 'postcode', 'BT17 0XD', $result);
134
        $this->_testGetJSON('getMLA', 'constituency', 'Belfast West', $result);
135
        $this->_testGetJSON(
136
            'getMLA',
137
            'id',
138
            '101',
139
            '[{"member_id":"101","house":"3","given_name":"Test1","family_name":"Nimember","constituency":"Belfast West","party":"DUP","entered_house":"2000-01-01","left_house":"9999-12-31","entered_reason":"general_election","left_reason":"still_in_office","person_id":"101","title":"Mr","lastupdate":"2013-08-07 15:06:19","full_name":"Mr Test1 Nimember"}]'
140
        );
141
    }
142
143
    public function testApiKeySignup() {
144
        $page = $this->post_page('key');
145
        $this->assertStringContainsString('Subscribe to a plan', $page);
146
        $page = $this->post_page('update-plan', [
147
            'plan' => 'twfy-1k',
148
            'charitable_tick' => 'on',
149
            'charitable' => 'c',
150
            'charity_number' => '123456',
151
            'tandcs_tick' => 'on',
152
        ]);
153
        $this->assertEquals('Location: /api/key?updated=1', $page);
154
        $page = $this->get_page('key', ['updated' => 1]);
155
        $this->assertStringContainsString('Your current plan is <strong>Some calls per month</strong>.', $page);
156
        $this->assertStringContainsString('It costs you £0/month.', $page);
157
        $this->assertStringContainsString('100% discount applied.', $page);
158
    }
159
160
    public function testApiKeyDowngrade() {
161
        $page = $this->post_page('update-plan', [
162
            'stripeToken' => 'TOKEN',
163
            'plan' => 'twfy-5k',
164
            'charitable_tick' => 'on',
165
            'charitable' => 'c',
166
            'charity_number' => '123456',
167
            'tandcs_tick' => 'on',
168
        ]);
169
        $this->assertEquals('Location: /api/key?updated=1', $page);
170
        $page = $this->get_page('key', ['updated' => 1]);
171
        $this->assertStringContainsString('Your current plan is <strong>Many calls per month</strong>.', $page);
172
        $this->assertStringContainsString('It costs you £50/month.', $page);
173
174
        $page = $this->post_page('update-plan', [
0 ignored issues
show
The assignment to $page is dead and can be removed.
Loading history...
175
            'plan' => 'twfy-1k',
176
            'charitable_tick' => 'on',
177
            'charitable' => 'c',
178
            'charity_number' => '123456',
179
            'tandcs_tick' => 'on',
180
        ]);
181
        $page = $this->get_page('key', ['updated' => 1]);
182
    }
183
184
    public function testApiKeyUpgrade() {
185
        $page = $this->post_page('update-plan', [
186
            'stripeToken' => 'TOKEN',
187
            'plan' => 'twfy-1k',
188
            'charitable_tick' => 'on',
189
            'charitable' => 'c',
190
            'charity_number' => 'up-test',
191
            'tandcs_tick' => 'on',
192
        ]);
193
        $this->assertEquals('Location: /api/key?updated=1', $page);
194
        $page = $this->get_page('key', ['updated' => 1]);
0 ignored issues
show
The assignment to $page is dead and can be removed.
Loading history...
195
        $page = $this->post_page('update-plan', [
196
            'stripeToken' => 'TOKEN',
197
            'plan' => 'twfy-5k',
198
            'tandcs_tick' => 'on',
199
        ]);
200
        $page = $this->get_page('key', ['updated' => 1]);
201
        $this->assertStringContainsString('Your current plan is <strong>Many calls per month</strong>.', $page);
202
        $this->assertStringContainsString('It costs you £50/month.', $page);
203
    }
204
}
205