Completed
Push — develop ( 4811e5...9083e6 )
by Michael
01:49
created

test_handleWebhookPayload_initial_double()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
3
namespace dokuwiki\plugin\swarmwebhook\test;
4
5
/**
6
 * General tests for the swarmwebhook plugin
7
 *
8
 * @group plugin_swarmwebhook
9
 * @group plugins
10
 */
11
class ZapierHandlePayload extends \DokuWikiTest
12
{
13
    /** @var array alway enable the needed plugins */
14
    protected $pluginsEnabled = ['swarmwebhook', 'struct', 'sqlite'];
15
16
17
    /**
18
     * Do not refactor these with a DataProvider -> struct database state would depend on order of execution
19
     */
20
    public function test_handleWebhookPayload_initial_single()
21
    {
22
        $inputJSON = '{"createdAt": "1525849118", "like": "False", "isMayor": "True", "editableUntil": "1525935518000", "posts": {"count": "0", "textCount": "0"}, "comments": {"count": "0"}, "photos": {"count": "0", "items": ""}, "likes": {"count": "0", "groups": ""}, "venue": {"stats": {"tipCount": "2", "checkinsCount": "1552", "usersCount": "24"}, "name": "CosmoCode", "venueRatingBlacklisted": "True", "url": "http://www.cosmocode.de", "contact": {"twitter": "cosmocode"}, "location": {"city": "Berlin", "labeledLatLngs": "label: display\nlat: 52.5341728565\nlng: 13.4235969339", "cc": "DE", "country": "Germany", "postalCode": "10405", "state": "Berlin", "formattedAddress": "Prenzlauer Allee 36 (Marienburger Strasse),10405 Berlin", "crossStreet": "Marienburger Strasse", "address": "Prenzlauer Allee 36", "lat": "52.5341728565", "lng": "13.4235969339"}, "beenHere": {"lastCheckinExpiredAt": "0"}, "verified": "False", "id": "4b4ca6c8f964a520f8b826e3", "categories": "icon: {u\'prefix\': u\'https://ss3.4sqi.net/img/categories_v2/building/default_\', u\'suffix\': u\'.png\'}\nid: 4bf58dd8d48988d124941735\nname: Office\npluralName: Offices\nprimary: True\nshortName: Office"}, "type": "checkin", "id": "5af29c1e6fd626002c38730b", "timeZoneOffset": "120", "source": {"url": "https://www.swarmapp.com", "name": "Swarm for Android"}}';
23
        $webhook = new mock\Zapier();
24
25
        $webhook->run($inputJSON);
26
27
        /** @var \remote_plugin_struct $remote */
28
        $remote = plugin_load('remote', 'struct');
29
        $rows = $remote->getAggregationData(
30
            ['swarm'],
31
            ['*']
32
        );
33
34
        $expectedRows = [
35
            'swarm.date' => '2018-05-09',
36
            'swarm.json' => $inputJSON,
37
            'swarm.locname' => 'CosmoCode',
38
            'swarm.checkinid' => '5af29c1e6fd626002c38730b',
39
            'swarm.shout' => '',
40
            'swarm.time' => '2018-05-09 08:58',
41
            'swarm.service' => 'Zapier',
42
        ];
43
44
        $this->assertEquals($rows[0], $expectedRows, 'single event, initially creating the schema');
45
    }
46
47
    /**
48
     * Do not refactor these with a DataProvider -> struct database state would depend on order of execution
49
     */
50
    public function test_handleWebhookPayload_initial_double()
51
    {
52
        $inputJSON = '{"createdAt": "1525849118", "like": "False", "isMayor": "True", "editableUntil": "1525935518000", "posts": {"count": "0", "textCount": "0"}, "comments": {"count": "0"}, "photos": {"count": "0", "items": ""}, "likes": {"count": "0", "groups": ""}, "venue": {"stats": {"tipCount": "2", "checkinsCount": "1552", "usersCount": "24"}, "name": "CosmoCode", "venueRatingBlacklisted": "True", "url": "http://www.cosmocode.de", "contact": {"twitter": "cosmocode"}, "location": {"city": "Berlin", "labeledLatLngs": "label: display\nlat: 52.5341728565\nlng: 13.4235969339", "cc": "DE", "country": "Germany", "postalCode": "10405", "state": "Berlin", "formattedAddress": "Prenzlauer Allee 36 (Marienburger Strasse),10405 Berlin", "crossStreet": "Marienburger Strasse", "address": "Prenzlauer Allee 36", "lat": "52.5341728565", "lng": "13.4235969339"}, "beenHere": {"lastCheckinExpiredAt": "0"}, "verified": "False", "id": "4b4ca6c8f964a520f8b826e3", "categories": "icon: {u\'prefix\': u\'https://ss3.4sqi.net/img/categories_v2/building/default_\', u\'suffix\': u\'.png\'}\nid: 4bf58dd8d48988d124941735\nname: Office\npluralName: Offices\nprimary: True\nshortName: Office"}, "type": "checkin", "id": "5af29c1e6fd626002c38730b", "timeZoneOffset": "120", "source": {"url": "https://www.swarmapp.com", "name": "Swarm for Android"}}';
53
        $webhook = new mock\Zapier();
54
55
        $actualOK = $webhook->handleWebhookPayload($inputJSON);
56
        $actualOK = $actualOK && $webhook->handleWebhookPayload($inputJSON);
57
58
        /** @var \remote_plugin_struct $remote */
59
        $remote = plugin_load('remote', 'struct');
60
        $rows = $remote->getAggregationData(
61
            ['swarm'],
62
            ['*']
63
        );
64
65
        $expectedRows = [
66
            'swarm.date' => '2018-05-09',
67
            'swarm.json' => $inputJSON,
68
            'swarm.locname' => 'CosmoCode',
69
            'swarm.checkinid' => '5af29c1e6fd626002c38730b',
70
            'swarm.shout' => '',
71
            'swarm.time' => '2018-05-09 08:58',
72
            'swarm.service' => 'Zapier',
73
        ];
74
75
        $this->assertTrue($actualOK, 'single event, initially creating the schema');
76
        $this->assertCount(1, $rows, 'saving a payload twice should only create one row');
77
        $this->assertEquals($rows[0], $expectedRows);
78
    }
79
}