ServiceBaseUnitTest::testSetTransport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 14
rs 10
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\ServiceHttpTransport\ServiceHttpTransport;
8
use Mezon\Service\ServiceRestTransport\ServiceRestTransport;
9
use Mezon\Service\ServiceConsoleTransport\ServiceConsoleTransport;
10
use Mezon\Service\Tests\Mocks\TestingTransport;
11
12
/**
13
 *
14
 * @psalm-suppress PropertyNotSetInConstructor
15
 */
16
class ServiceBaseUnitTest extends TestCase
17
{
18
19
    /**
20
     * Testing getTransport method
21
     */
22
    public function testGetTransport(): void
23
    {
24
        // setup
25
        $provider = new MockProvider();
26
        $service = new ServiceBase(new ServiceHttpTransport($provider));
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(): void
36
    {
37
        // setup
38
        $provider = new MockProvider();
39
        $service = new ServiceBase(new ServiceHttpTransport($provider));
40
41
        // assertions
42
        $this->assertInstanceOf(ServiceHttpTransport::class, $service->getTransport());
43
44
        // test body
45
        $service->setTransport(new ServiceRestTransport($provider));
46
47
        // assertions
48
        $this->assertInstanceOf(ServiceRestTransport::class, $service->getTransport());
49
    }
50
51
    /**
52
     * Testing fetchActions call
53
     */
54
    public function testFetchActionsGet(): void
55
    {
56
        // setup
57
        $provider = new MockProvider();
58
        $service = new TestingBaseService(new ServiceConsoleTransport($provider));
59
60
        // test body
61
        $_GET['r'] = 'test';
62
        $_SERVER['REQUEST_METHOD'] = 'GET';
63
        $service->run();
64
65
        // assertions
66
        $this->assertEquals('Action!', ServiceConsoleTransport::$result);
67
    }
68
69
    /**
70
     * Testing fetchActions call
71
     */
72
    public function testFetchActionsPost(): void
73
    {
74
        // setup
75
        $provider = new MockProvider();
76
        $service = new TestingBaseService(new ServiceConsoleTransport($provider));
77
78
        // test body
79
        $_GET['r'] = 'test';
80
        $_SERVER['REQUEST_METHOD'] = 'POST';
81
        $service->run();
82
83
        // assertions
84
        $this->assertEquals('Action!', ServiceConsoleTransport::$result);
85
    }
86
87
    /**
88
     * Testing exception handling while constructor call
89
     */
90
    public function testExceptionWhileConstruction(): void
91
    {
92
        // setup and assertions
93
        ob_start();
94
        $provider = new MockProvider();
95
        new ExceptionTestingBaseService(new TestingTransport($provider));
96
        $content = ob_get_contents();
97
        ob_end_clean();
98
99
        // assertions
100
        $this->assertStringContainsString('"message"', $content);
101
        $this->assertStringContainsString('"code"', $content);
102
        $this->assertTrue(is_array(json_decode($content, true)));
103
    }
104
}
105