Issues (15)

tests/ContainerTest.php (3 issues)

1
<?php
2
/*
3
 * @copyright (c) 2018 Mendel <[email protected]>
4
 * @license see license.txt
5
 */
6
namespace drycart\di\tests;
7
use PHPUnit\Framework\TestCase;
0 ignored issues
show
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
10
/**
11
 * Description of DiTest
12
 *
13
 * @author mendel
14
 */
15
class ContainerTest extends TestCase
16
{
17
    
18
    public function testInstantiate()
19
    {
20
        $di = new \drycart\di\Container();
21
        $di->setConfig([
22
            'stdClass' => ['#class'=>'stdClass']
23
        ]);
24
        $obj = $di->get('stdClass');
25
        $this->assertTrue(is_a($obj, 'stdClass'));
26
    }
27
    
28
    public function testAlias()
29
    {
30
        $di = new \drycart\di\Container();
31
        $di->setConfig([
32
            '#other' => ['#class'=>'stdClass']
33
        ]);
34
        $obj = $di->get('#other');
35
        $this->assertTrue(is_a($obj, 'stdClass'));
36
    }
37
    
38
    public function testNotFoundParameters()
39
    {
40
        $di = new \drycart\di\Container();
41
        $this->expectException(\drycart\di\ContainerException::class);
42
        $this->expectExceptionMessage('Unknown parameter intDummy');
43
        $di->get('drycart\di\tests\dummy\DummyPlusParameter');
44
    }
45
    
46
    public function testDefaultParams()
47
    {
48
        $di = new \drycart\di\Container();
49
        $di->setConfig([
50
            'drycart\di\tests\dummy\DummyInterface' => ['#class'=>'drycart\di\tests\dummy\DummyComplex']
51
        ]);
52
        $obj = $di->get('drycart\di\tests\dummy\DummyInterface');
53
        $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyInterface'));
54
    }
55
56
    public function testNoConfig()
57
    {
58
        $di = new \drycart\di\Container();
59
        $obj = $di->get('stdClass');
60
        $this->assertTrue(is_a($obj, 'stdClass'));
61
    }
62
63
    public function testNotFound()
64
    {
65
        $di = new \drycart\di\Container();
66
        $this->expectException(\drycart\di\NotFoundException::class);
67
        $di->get('NotExistClass');
68
    }
69
    
70
    public function testSinglton()
71
    {
72
        $di = new \drycart\di\Container();
73
        $di->setConfig([
74
            'drycart\di\tests\dummy\Dummy' => [
75
                '#singleton' => true
76
            ]
77
        ]);
78
        $obj1 = $di->get('drycart\di\tests\dummy\Dummy');
79
        $obj2 = $di->get('drycart\di\tests\dummy\Dummy');
80
        $this->assertEquals(spl_object_id($obj1), spl_object_id($obj2));
81
    }
82
    
83
    public function testFactory()
84
    {
85
        $di = new \drycart\di\Container();
86
        $di->setConfig([
87
            'drycart\di\tests\Dummy' => [
88
                '#singleton' => false
89
            ]
90
        ]);
91
        $obj1 = $di->get('drycart\di\tests\dummy\Dummy');
92
        $obj2 = $di->get('drycart\di\tests\dummy\Dummy');
93
        $this->assertNotEquals(spl_object_id($obj1), spl_object_id($obj2));
94
    }
95
    
96
    public function testWrongClass()
97
    {
98
        $di = new \drycart\di\Container();
99
        $di->setConfig([
100
            'drycart\di\tests\dummy\DummyInterface' => ['#class'=>'stdClass']
101
        ]);
102
        $this->expectException(\drycart\di\ContainerException::class);
103
        $this->expectExceptionMessage('Wrong class, will be drycart\di\tests\dummy\DummyInterface');
104
        $di->get('drycart\di\tests\dummy\DummyInterface');
105
    }
106
    
107
    public function testSpeed()
108
    {
109
        $di = new \drycart\di\Container();
110
        $di->setConfig([
111
            'drycart\di\tests\dummy\DummyInterface' => ['#class'=>'drycart\di\tests\dummy\DummyComplex']
112
        ]);
113
        for ($i = 0; $i < 1000; $i++) {
114
            $obj = $di->make('drycart\di\tests\dummy\DummyInterface', ['intDummy'=>$i]);
115
//            $obj = new DummyComplex($i, null);
116
        }
117
        $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyInterface'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj does not seem to be defined for all execution paths leading up to this point.
Loading history...
118
    }
119
    
120
    public function testSpeed2()
121
    {
122
        $di = new \drycart\di\Container();
123
        $di->setConfig([
124
            'stdClass' => ['#class'=>'stdClass']
125
        ]);
126
        $className = 'stdClass';
127
        for ($i = 0; $i < 10000; $i++) {
128
            $obj = $di->get($className);
129
//            $obj = new $className;
130
        }
131
        $this->assertTrue(is_a($obj, 'stdClass'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj does not seem to be defined for all execution paths leading up to this point.
Loading history...
132
    }
133
    
134
}
135