Passed
Push — master ( a58ef6...fc3cb3 )
by Alex
10:58 queued 01:08
created

ServiceBaseUnitTest::testFetchActionsPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 16
rs 9.9666
c 1
b 0
f 0
1
<?php
2
namespace Mezon\Service\Tests;
3
4
use Mezon\Security\MockProvider;
5
use PHPUnit\Framework\TestCase;
6
use Mezon\Service\ServiceBase;
7
use Mezon\Service\ServiceBaseLogic;
8
use Mezon\Service\ServiceModel;
9
use Mezon\Service\ServiceHttpTransport\ServiceHttpTransport;
10
use Mezon\Service\ServiceRestTransport\ServiceRestTransport;
11
use Mezon\Service\ServiceConsoleTransport\ServiceConsoleTransport;
12
13
class ServiceBaseUnitTest extends TestCase
14
{
15
16
    /**
17
     * Testing getTransport method
18
     */
19
    public function testGetTransport()
20
    {
21
        // setup
22
        $service = new ServiceBase(
23
            ServiceBaseLogic::class,
24
            ServiceModel::class,
25
            MockProvider::class,
26
            ServiceHttpTransport::class);
27
28
        // test body and assertions
29
        $this->assertInstanceOf(ServiceHttpTransport::class, $service->getTransport());
30
    }
31
32
    /**
33
     * Testing getTransport method
34
     */
35
    public function testSetTransport()
36
    {
37
        // setup
38
        $service = new ServiceBase(
39
            ServiceBaseLogic::class,
40
            ServiceModel::class,
41
            MockProvider::class,
42
            ServiceHttpTransport::class);
43
44
        // assertions
45
        $this->assertInstanceOf(ServiceHttpTransport::class, $service->getTransport());
46
47
        // test body
48
        $service->setTransport(new ServiceRestTransport());
49
50
        // assertions
51
        $this->assertInstanceOf(ServiceRestTransport::class, $service->getTransport());
52
    }
53
54
    /**
55
     * Testing fetchActions call
56
     */
57
    public function testFetchActionsGet(): void
58
    {
59
        // setup
60
        $service = new TestingBaseService(
61
            ServiceBaseLogic::class,
62
            ServiceModel::class,
63
            MockProvider::class,
64
            ServiceConsoleTransport::class);
65
66
        // test body
67
        $_GET['r'] = 'test';
68
        $_SERVER['REQUEST_METHOD'] = 'GET';
69
        $service->run();
70
71
        // assertions
72
        $this->assertEquals('Action!', $service->getTransport()->result);
73
    }
74
75
    /**
76
     * Testing fetchActions call
77
     */
78
    public function testFetchActionsPost(): void
79
    {
80
        // setup
81
        $service = new TestingBaseService(
82
            ServiceBaseLogic::class,
83
            ServiceModel::class,
84
            MockProvider::class,
85
            ServiceConsoleTransport::class);
86
87
        // test body
88
        $_GET['r'] = 'test';
89
        $_SERVER['REQUEST_METHOD'] = 'POST';
90
        $service->run();
91
92
        // assertions
93
        $this->assertEquals('Action!', $service->getTransport()->result);
94
    }
95
96
    /**
97
     * Testing exception handling while constructor call
98
     */
99
    public function testExceptionWhileConstruction(): void
100
    {
101
        // setup and assertions
102
        ob_start();
103
        new ExceptionTestingBaseService(
104
            ServiceBaseLogic::class,
105
            ServiceModel::class,
106
            MockProvider::class,
107
            TestingTransport::class);
108
        $content = ob_get_contents();
109
        ob_end_clean();
110
111
        // assertions
112
        $this->assertStringContainsString('"message"', $content);
113
        $this->assertStringContainsString('"code"', $content);
114
        $this->assertTrue(is_array(json_decode($content, true)));
115
    }
116
}
117