Passed
Push — master ( e82e8d...d1be4d )
by Korotkov
02:49 queued 11s
created

ContainerTraitTest::testPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * Date: 17.02.17
5
 * Time: 13:23
6
 *
7
 * @author    : Korotkov Danila <[email protected]>
8
 * @copyright Copyright (c) 2016, Korotkov Danila
9
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
10
 *
11
 *  phpunit src/tests/ContainerTraitTest --coverage-html src/tests/coverage-html
12
 */
13
14
15
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;
16
use Rudra\ContainerInterface;
17
use Rudra\Container;
18
19
20
/**
21
 * Class ContainerTraitTest
22
 */
23
class ContainerTraitTest extends PHPUnit_Framework_TestCase
24
{
25
26
    /**
27
     * @var ClassWithContainerTrait
28
     */
29
    protected $stub;
30
31
    protected function setUp(): void
32
    {
33
        Container::app()->setBinding(ContainerInterface::class, Container::$app);
34
35
        $app = [
36
            'contracts' => [
37
                ContainerInterface::class => Container::$app
38
            ],
39
40
            'services' => [
41
                'validation' => ['ClassWithoutConstructor'],
42
                'redirect'   => ['ClassWithoutParameters'],
43
                'db'         => ['ClassWithDefaultParameters', ['param' => '123']],
44
            ]
45
        ];
46
47
        Container::$app->setServices($app);
48
49
        $this->stub = new ClassWithContainerTrait(Container::$app);
50
    }
51
52
    public function testValidation(): void
53
    {
54
        $this->assertInstanceOf(ClassWithoutConstructor::class, $this->getStub()->validation());
55
    }
56
57
    public function testRedirect(): void
58
    {
59
        $this->assertInstanceOf(ClassWithoutParameters::class, $this->getStub()->redirect());
60
    }
61
62
    public function testDb(): void
63
    {
64
        $this->assertInstanceOf(ClassWithDefaultParameters::class, $this->getStub()->db());
65
    }
66
67
    public function testNew(): void
68
    {
69
        $newClassWithoutConstructor = $this->getStub()->new('ClassWithoutConstructor');
70
        $this->assertInstanceOf('ClassWithoutConstructor', $newClassWithoutConstructor);
71
    }
72
73
    public function testSetPagination(): void
74
    {
75
        $this->getMockBuilder('Rudra\Pagination')->getMock();
76
        $this->getStub()->setPagination(['id' => 1]);
77
        $this->assertInstanceOf('Rudra\Pagination', $this->getStub()->pagination());
78
    }
79
80
    public function testPost(): void
81
    {
82
        Container::$app->setPost(['key' => 'value']);
83
        $this->assertEquals('value', $this->getStub()->post('key'));
84
    }
85
86
    public function testPut(): void
87
    {
88
        Container::$app->setPut(['key' => 'value']);
89
        $this->assertTrue($this->getStub()->container()->hasPut('key'));
90
        $this->assertEquals('value', $this->getStub()->container()->getPut('key'));
91
    }
92
93
    public function testPatch(): void
94
    {
95
        Container::$app->setPatch(['key' => 'value']);
96
        $this->assertTrue($this->getStub()->container()->hasPatch('key'));
97
        $this->assertEquals('value', $this->getStub()->container()->getPatch('key'));
98
    }
99
100
    public function testDelete(): void
101
    {
102
        Container::$app->setDelete(['key' => 'value']);
103
        $this->assertTrue($this->getStub()->container()->hasDelete('key'));
104
        $this->assertEquals('value', $this->getStub()->container()->getDelete('key'));
105
    }
106
107
    public function testSessionData(): void
108
    {
109
        $this->getStub()->setSession('key', 'value');
110
        $this->getStub()->setSession('subKey', 'value', 'subSet');
111
        $this->getStub()->setSession('increment', 'value', 'increment');
112
        $this->assertEquals('value', Container::$app->getSession('key'));
113
        $this->assertEquals('value', Container::$app->getSession('subKey', 'subSet'));
114
        $this->assertEquals('value', Container::$app->getSession('increment', '0'));
115
        $this->assertNull($this->getStub()->unsetSession('key'));
116
        $this->assertNull($this->getStub()->unsetSession('subKey', 'subSet'));
117
        $this->assertFalse(Container::$app->hasSession('key'));
118
        $this->assertFalse(Container::$app->hasSession('subKey', 'subSet'));
119
    }
120
121
    public function testConfig(): void
122
    {
123
        Container::$app->setConfig(['key' => ['subKey' => 'value']]);
124
125
        $this->assertInternalType('array', Container::$app->config('key'));
126
        $this->assertEquals('value', Container::$app->config('key', 'subKey'));
127
    }
128
129
    /**
130
     * @runInSeparateProcess
131
     */
132
//    public function testJsonResponse(): void
133
//    {
134
//        $data = ['key' => ['subKey' => 'value']];
135
//
136
//        ob_start();
137
//        Container::$app->jsonResponse($data);
138
//        $json = ob_get_clean();
139
//
140
//        $this->assertEquals(json_encode($data), $json);
141
//    }
142
143
    /**
144
     * @return ClassWithContainerTrait
145
     */
146
    public function getStub(): ClassWithContainerTrait
147
    {
148
        return $this->stub;
149
    }
150
}
151