aboutsummaryrefslogtreecommitdiff
path: root/tests/Actions/ListActionTest.php
blob: 5056050e2d6f448b849ce0db3e45a9a0e9712cd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

namespace RssBridge\Tests\Actions;

use ActionFactory;
use BridgeFactory;
use PHPUnit\Framework\TestCase;

class ListActionTest extends TestCase
{
    private $data;

    /**
     * @runInSeparateProcess
     * @requires function xdebug_get_headers
     */
    public function testHeaders()
    {
        $this->initAction();

        $this->assertContains(
            'Content-Type: application/json',
            xdebug_get_headers()
        );
    }

    /**
     * @runInSeparateProcess
     */
    public function testOutput()
    {
        $this->initAction();

        $items = json_decode($this->data, true);

        $this->assertNotNull($items, 'invalid JSON output: ' . json_last_error_msg());

        $this->assertArrayHasKey('total', $items, 'Missing "total" parameter');
        $this->assertIsInt($items['total'], 'Invalid type');

        $this->assertArrayHasKey('bridges', $items, 'Missing "bridges" array');

        $this->assertEquals(
            $items['total'],
            count($items['bridges']),
            'Item count doesn\'t match'
        );

        $bridgeFactory = new BridgeFactory();

        $this->assertEquals(
            count($bridgeFactory->getBridgeClassNames()),
            count($items['bridges']),
            'Number of bridges doesn\'t match'
        );

        $expectedKeys = [
            'status',
            'uri',
            'name',
            'icon',
            'parameters',
            'maintainer',
            'description'
        ];

        $allowedStatus = [
            'active',
            'inactive'
        ];

        foreach ($items['bridges'] as $bridge) {
            foreach ($expectedKeys as $key) {
                $this->assertArrayHasKey($key, $bridge, 'Missing key "' . $key . '"');
            }

            $this->assertContains($bridge['status'], $allowedStatus, 'Invalid status value');
        }
    }

    private function initAction()
    {
        $actionFactory = new ActionFactory();

        $action = $actionFactory->create('list');

        ob_start();
        $action->execute();
        $this->data = ob_get_contents();
        ob_clean();
        ob_end_flush();
    }
}