Completed
Branch master (c930c0)
by Alejandro
10:35 queued 05:57
created

ContainerTest::testCountThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Acelaya\SlimContainerSm\Test;
3
4
use Acelaya\SlimContainerSm\Container;
5
use PHPUnit_Framework_TestCase as TestCase;
6
use Slim\Helper\Set;
7
use Zend\ServiceManager\ServiceManager;
8
9
class ContainerTest extends TestCase
10
{
11
    /**
12
     * @var Container
13
     */
14
    private $container;
15
    /**
16
     * @var ServiceManager
17
     */
18
    private $sm;
19
20
    public function setUp()
21
    {
22
        $this->sm = new ServiceManager();
23
        $this->container = new Container($this->sm);
24
    }
25
26 View Code Duplication
    public function testSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $expected = new \stdClass();
29
        $this->container->set('foo', $expected);
30
        $this->assertSame($expected, $this->sm->get('foo'));
31
32
        $this->container->set('bar', function () {
33
            return new \stdClass();
34
        });
35
        $this->assertInstanceOf('stdClass', $this->sm->get('bar'));
36
    }
37
38
    public function testGet()
39
    {
40
        // Via container
41
        $this->container->foo = [];
42
        $this->assertEquals([], $this->container->get('foo'));
43
44
        // Via service manager
45
        $this->sm->setService('foo', new \stdClass());
46
        $this->sm->setAlias('bar', 'foo');
47
        $this->assertInstanceOf('stdClass', $this->container->get('bar'));
48
49
        $this->sm->setFactory('factory', function (ServiceManager $sm) {
50
            return $sm->get('bar');
51
        });
52
        $this->assertInstanceOf('stdClass', $this->container->get('factory'));
53
54
        $this->sm->setInvokableClass('invokable', 'stdClass');
55
        $this->assertInstanceOf('stdClass', $this->container->get('invokable'));
56
    }
57
58
    /**
59
     * @expectedException \Acelaya\SlimContainerSm\Exception\BadMethodCallException
60
     */
61
    public function testAllThrowsException()
62
    {
63
        $this->container->all();
64
    }
65
66
    public function testHas()
67
    {
68
        $this->assertFalse($this->container->has('foo'));
69
        $this->sm->setService('foo', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
        $this->assertFalse($this->container->has('foo'));
71
        $this->container->foo = 'Hello!';
72
        $this->assertTrue($this->container->has('foo'));
73
    }
74
75
    public function testRemove()
76
    {
77
        $this->container->foo = [];
78
        $this->assertTrue($this->container->has('foo'));
79
        $this->container->remove('foo');
80
        $this->assertFalse($this->container->has('foo'));
81
    }
82
83
    public function testClear()
84
    {
85
        $this->container->foo = new \stdClass();
86
        $this->container->bar = new \stdClass();
87
        $this->container->clear();
88
        $this->assertFalse($this->container->has('foo'));
89
        $this->assertFalse($this->container->has('bar'));
90
    }
91
92
    /**
93
     * @expectedException \Acelaya\SlimContainerSm\Exception\BadMethodCallException
94
     */
95
    public function testCountThrowsException()
96
    {
97
        $this->container->count();
98
    }
99
100
    /**
101
     * @expectedException \Acelaya\SlimContainerSm\Exception\BadMethodCallException
102
     */
103
    public function testkeysThrowsException()
104
    {
105
        $iterator = $this->container->keys();
0 ignored issues
show
Unused Code introduced by
$iterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
    }
107
108
    /**
109
     * @expectedException \Acelaya\SlimContainerSm\Exception\BadMethodCallException
110
     */
111
    public function testGetIteratorThrowsException()
112
    {
113
        $iterator = $this->container->getIterator();
0 ignored issues
show
Unused Code introduced by
$iterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
    }
115
116 View Code Duplication
    public function testSignleton()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $expected = new \stdClass();
119
        $this->container->singleton('foo', $expected);
120
        $this->sm->has('foo');
121
        $this->assertSame($expected, $this->container->get('foo'));
122
        $this->assertSame($expected, $this->sm->get('foo'));
123
    }
124
125
    public function testSingletonWithCallable()
126
    {
127
        $expected = new \stdClass();
128
        $this->container->singleton('foo', function () use ($expected) {
129
            return $expected;
130
        });
131
        $this->assertSame($expected, $this->container->get('foo'));
132
    }
133
134
    public function testConsumeSlimContainer()
135
    {
136
        $anoterContainer = new Set();
137
        $anoterContainer->foo = [];
138
        $anoterContainer->bar = new \stdClass();
139
        $anoterContainer->baz = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
            return 'Hello';
141
        };
142
        $anoterContainer->singleton('foobar', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
            return 'Hello';
144
        });
145
        $anoterContainer->barfoo = [$this, 'fakeMethod'];
146
        $this->container->consumeSlimContainer($anoterContainer);
147
148
        $this->assertTrue($this->sm->has('foo'));
149
        $this->assertTrue($this->container->has('foo'));
150
        $this->assertTrue($this->sm->has('bar'));
151
        $this->assertTrue($this->container->has('bar'));
152
        $this->assertTrue($this->sm->has('baz'));
153
        $this->assertTrue($this->container->has('baz'));
154
        $this->assertTrue($this->sm->has('foobar'));
155
        $this->assertTrue($this->container->has('foobar'));
156
        $this->assertTrue($this->sm->has('barfoo'));
157
        $this->assertTrue($this->container->has('barfoo'));
158
    }
159
160
    public function fakeMethod()
161
    {
162
    }
163
}
164