| 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() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @expectedException \Acelaya\SlimContainerSm\Exception\BadMethodCallException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function testGetIteratorThrowsException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $iterator = $this->container->getIterator(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 116 |  | View Code Duplication |     public function testSignleton() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             return 'Hello'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $anoterContainer->singleton('foobar', function ($c) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.