Passed
Pull Request — master (#9)
by Korotkov
01:49
created

ContainerTest::testSessionData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @copyright Copyright (c) 2016, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 *
10
 *  phpunit src/tests/ContainerTest --coverage-html src/tests/coverage-html
11
 */
12
13
namespace Rudra\Tests;
14
15
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;
16
use Rudra\Container;
17
use Rudra\ContainerInterface;
18
use Rudra\Tests\Stub\ClassWithDefaultParameters;
19
use Rudra\Tests\Stub\ClassWithDependency;
20
use Rudra\Tests\Stub\ClassWithoutConstructor;
21
use Rudra\Tests\Stub\ClassWithoutParameters;
22
23
/**
24
 * Class ContainerTest
25
 */
26
class ContainerTest extends PHPUnit_Framework_TestCase
27
{
28
29
    /**
30
     * @var ContainerInterface
31
     */
32
    protected $container;
33
34
    protected function setUp(): void
35
    {
36
        $this->container = Container::app();
37
        $this->container->setBinding(ContainerInterface::class, $this->container);
38
    }
39
40
    /**
41
     * Проверяем синглтон
42
     */
43
    public function testCallSingleton(): void
44
    {
45
        $this->assertInstanceOf(Container::class, Container::app());
46
        $this->assertInstanceOf(Container::class, $this->container);
47
    }
48
49
    public function testGet(): void
50
    {
51
        $this->assertTrue(empty($this->container->get()));
0 ignored issues
show
Bug introduced by
The call to Rudra\ContainerInterface::get() has too few arguments starting with key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->assertTrue(empty($this->container->/** @scrutinizer ignore-call */ get()));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
    }
53
54
    public function testSetServices(): void
55
    {
56
        $this->container->setServices([
57
            'contracts' => [
58
                ContainerInterface::class => $this->container
59
            ],
60
61
            'services' => [
62
                'CWC'  => [ClassWithoutConstructor::class],
63
                'CWP'  => [ClassWithoutParameters::class],
64
                'CWDP' => [ClassWithDefaultParameters::class, ['param' => '123']],
65
            ]
66
        ]);
67
        $this->assertInstanceOf(ClassWithoutConstructor::class, $this->container->get('CWC'));
68
        $this->assertInstanceOf(ClassWithoutParameters::class, $this->container->get('CWP'));
69
        $this->assertInstanceOf(ClassWithDefaultParameters::class, $this->container->get('CWDP'));
70
    }
71
72
    public function testSetRaw(): void
73
    {
74
        $this->container->set(ContainerTest::class, $this, 'raw');
0 ignored issues
show
Bug introduced by
'raw' of type string is incompatible with the type array expected by parameter $params of Rudra\ContainerInterface::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $this->container->set(ContainerTest::class, $this, /** @scrutinizer ignore-type */ 'raw');
Loading history...
75
        $this->assertInstanceOf(ContainerTest::class, $this->container->get(ContainerTest::class));
76
    }
77
78
    public function testGetArrayHasKey(): void
79
    {
80
        $this->assertArrayHasKey(ContainerTest::class, $this->container->get());
0 ignored issues
show
Bug introduced by
The call to Rudra\ContainerInterface::get() has too few arguments starting with key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $this->assertArrayHasKey(ContainerTest::class, $this->container->/** @scrutinizer ignore-call */ get());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
    }
82
83
    public function testIoCClassWithoutConstructor(): void
84
    {
85
        $newClassWithoutConstructor = $this->container->new(ClassWithoutConstructor::class);
86
        $this->assertInstanceOf(ClassWithoutConstructor::class, $newClassWithoutConstructor);
87
88
        $this->container->set('ClassWithoutConstructor', $newClassWithoutConstructor);
89
        $this->assertInstanceOf(ClassWithoutConstructor::class, $this->container->get('ClassWithoutConstructor'));
90
    }
91
92
    public function testIoCwithoutParameters(): void
93
    {
94
        $newClassWithoutParameters = $this->container->new(ClassWithoutParameters::class);
95
        $this->assertInstanceOf(ClassWithoutParameters::class, $newClassWithoutParameters);
96
97
        $this->container->set('ClassWithoutParameters', $newClassWithoutParameters);
98
        $this->assertInstanceOf(ClassWithoutParameters::class, $this->container->get('ClassWithoutParameters'));
99
    }
100
101
    public function testIoCwithDefaultParameters(): void
102
    {
103
        $newClassWithDefaultParameters = $this->container->new(ClassWithDefaultParameters::class);
104
        $this->assertInstanceOf(ClassWithDefaultParameters::class, $newClassWithDefaultParameters);
105
106
        $this->container->set('ClassWithDefaultParameters', $newClassWithDefaultParameters);
107
        $this->assertInstanceOf(ClassWithDefaultParameters::class, $this->container->get('ClassWithDefaultParameters'));
108
    }
109
110
    public function testIoCwithDependency(): void
111
    {
112
        $newClassWithDependency = $this->container->new(ClassWithDependency::class);
113
        $this->assertInstanceOf(ClassWithDependency::class, $newClassWithDependency);
114
115
        $this->container->set('ClassWithDependency', $newClassWithDependency);
116
        $this->assertInstanceOf(ClassWithDependency::class, $this->container->get('ClassWithDependency'));
117
    }
118
119
    public function testHas(): void
120
    {
121
        $this->assertTrue($this->container->has(ContainerTest::class));
122
        $this->assertTrue($this->container->has('ClassWithoutConstructor'));
123
        $this->assertTrue($this->container->has('ClassWithoutParameters'));
124
        $this->assertTrue($this->container->has('ClassWithDefaultParameters'));
125
        $this->assertTrue($this->container->has('ClassWithDependency'));
126
        $this->assertFalse($this->container->has('SomeClass'));
127
    }
128
129
    public function testSetParam(): void
130
    {
131
        $param = 'value';
132
        $this->container->setParam('ClassWithDependency', 'param', $param);
133
        $this->assertEquals($param, $this->container->getParam('ClassWithDependency', 'param'));
134
    }
135
136
    public function testHasParam(): void
137
    {
138
        $this->assertTrue($this->container->hasParam('ClassWithDependency', 'param'));
139
        $this->assertNull($this->container->hasParam('SomeClass', 'param'));
140
    }
141
142
    public function testFailureGetParam(): void
143
    {
144
        $this->assertNull($this->container->getParam('SomeClass', 'param'));
145
    }
146
147
    public function testGetData(): void
148
    {
149
        $this->container->setGet(['key' => 'value']);
0 ignored issues
show
Bug introduced by
The method setGet() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        $this->container->/** @scrutinizer ignore-call */ 
150
                          setGet(['key' => 'value']);
Loading history...
150
        $this->assertEquals('value', $this->container->getGet('key'));
0 ignored issues
show
Bug introduced by
The method getGet() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
        $this->assertEquals('value', $this->container->/** @scrutinizer ignore-call */ getGet('key'));
Loading history...
151
        $this->assertContains('value', $this->container->getGet());
152
        $this->assertTrue($this->container->hasGet('key'));
0 ignored issues
show
Bug introduced by
The method hasGet() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        $this->assertTrue($this->container->/** @scrutinizer ignore-call */ hasGet('key'));
Loading history...
153
        $this->assertFalse($this->container->hasGet('false'));
154
    }
155
156
    public function testPostData(): void
157
    {
158
        $this->container->setPost(['key' => 'value']);
0 ignored issues
show
Bug introduced by
The method setPost() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        $this->container->/** @scrutinizer ignore-call */ 
159
                          setPost(['key' => 'value']);
Loading history...
159
        $this->assertEquals('value', $this->container->getPost('key'));
0 ignored issues
show
Bug introduced by
The method getPost() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
        $this->assertEquals('value', $this->container->/** @scrutinizer ignore-call */ getPost('key'));
Loading history...
160
        $this->assertContains('value', $this->container->getPost());
161
        $this->assertTrue($this->container->hasPost('key'));
0 ignored issues
show
Bug introduced by
The method hasPost() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
        $this->assertTrue($this->container->/** @scrutinizer ignore-call */ hasPost('key'));
Loading history...
162
        $this->assertFalse($this->container->hasPost('false'));
163
    }
164
165
    public function testServerData(): void
166
    {
167
        $this->container->setServer('key', 'value');
0 ignored issues
show
Bug introduced by
The method setServer() does not exist on Rudra\ContainerInterface. Did you maybe mean setServices()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
        $this->container->/** @scrutinizer ignore-call */ 
168
                          setServer('key', 'value');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
        $this->assertEquals('value', $this->container->getServer('key'));
0 ignored issues
show
Bug introduced by
The method getServer() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        $this->assertEquals('value', $this->container->/** @scrutinizer ignore-call */ getServer('key'));
Loading history...
169
        $this->assertArrayHasKey('key', $this->container->getServer());
170
    }
171
172
    public function testSessionData(): void
173
    {
174
        $this->container->setSession('key', 'value');
0 ignored issues
show
Bug introduced by
The method setSession() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
        $this->container->/** @scrutinizer ignore-call */ 
175
                          setSession('key', 'value');
Loading history...
175
        $this->container->setSession('subKey', 'value', 'subSet');
176
        $this->container->setSession('increment', 'value', 'increment');
177
        $this->assertEquals('value', $this->container->getSession('key'));
0 ignored issues
show
Bug introduced by
The method getSession() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
        $this->assertEquals('value', $this->container->/** @scrutinizer ignore-call */ getSession('key'));
Loading history...
178
        $this->assertEquals('value', $this->container->getSession('subKey', 'subSet'));
179
        $this->assertEquals('value', $this->container->getSession('increment', '0'));
180
        $this->assertTrue($this->container->hasSession('key'));
0 ignored issues
show
Bug introduced by
The method hasSession() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

180
        $this->assertTrue($this->container->/** @scrutinizer ignore-call */ hasSession('key'));
Loading history...
181
        $this->assertTrue($this->container->hasSession('subKey', 'subSet'));
182
        $this->assertNull($this->container->unsetSession('key'));
0 ignored issues
show
Bug introduced by
The method unsetSession() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
        $this->assertNull($this->container->/** @scrutinizer ignore-call */ unsetSession('key'));
Loading history...
183
        $this->assertNull($this->container->unsetSession('subKey', 'subSet'));
184
        $this->assertFalse($this->container->hasSession('key'));
185
        $this->assertFalse($this->container->hasSession('subKey', 'subSet'));
186
        $this->assertNull($this->container->clearSession());
0 ignored issues
show
Bug introduced by
The method clearSession() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        $this->assertNull($this->container->/** @scrutinizer ignore-call */ clearSession());
Loading history...
187
    }
188
189
    public function testCookieData(): void
190
    {
191
        $this->container->setCookie('key', 'value');
0 ignored issues
show
Bug introduced by
The method setCookie() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
        $this->container->/** @scrutinizer ignore-call */ 
192
                          setCookie('key', 'value');
Loading history...
192
        $this->assertEquals('value', $this->container->getCookie('key'));
0 ignored issues
show
Bug introduced by
The method getCookie() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
        $this->assertEquals('value', $this->container->/** @scrutinizer ignore-call */ getCookie('key'));
Loading history...
193
        $this->assertTrue($this->container->hasCookie('key'));
0 ignored issues
show
Bug introduced by
The method hasCookie() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
        $this->assertTrue($this->container->/** @scrutinizer ignore-call */ hasCookie('key'));
Loading history...
194
        $this->assertFalse($this->container->hasCookie('false'));
195
    }
196
197
    public function testFilesData(): void
198
    {
199
        $this->container->setFiles(
0 ignored issues
show
Bug introduced by
The method setFiles() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
        $this->container->/** @scrutinizer ignore-call */ 
200
                          setFiles(
Loading history...
200
            [
201
                'upload' => ['name' => ['img' => '41146.png']],
202
                'type'   => ['img' => 'image/png'],
203
            ]
204
        );
205
206
        $this->assertTrue($this->container->isUploaded('img'));
0 ignored issues
show
Bug introduced by
The method isUploaded() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        $this->assertTrue($this->container->/** @scrutinizer ignore-call */ isUploaded('img'));
Loading history...
207
        $this->assertTrue($this->container->isFileType('img', 'image/png'));
0 ignored issues
show
Bug introduced by
The method isFileType() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

207
        $this->assertTrue($this->container->/** @scrutinizer ignore-call */ isFileType('img', 'image/png'));
Loading history...
208
        $this->assertEquals('41146.png', $this->container->getUpload('img', 'name'));
0 ignored issues
show
Bug introduced by
The method getUpload() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
        $this->assertEquals('41146.png', $this->container->/** @scrutinizer ignore-call */ getUpload('img', 'name'));
Loading history...
209
    }
210
}
211