|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @see https://github.com/zendframework/zend-servicemanager for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) |
| 5 | + * @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +namespace ZendTest\ServiceManager; |
| 9 | + |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Zend\ServiceManager\ServiceManager; |
| 12 | + |
| 13 | +class ContainerTest extends TestCase |
| 14 | +{ |
| 15 | + public function config() |
| 16 | + { |
| 17 | + yield 'factories' => [['factories' => ['service' => TestAsset\SampleFactory::class]]]; |
| 18 | + yield 'invokables' => [['invokables' => ['service' => TestAsset\InvokableObject::class]]]; |
| 19 | + yield 'aliases-invokables' => [ |
| 20 | + [ |
| 21 | + 'aliases' => ['service' => TestAsset\InvokableObject::class], |
| 22 | + 'invokables' => [TestAsset\InvokableObject::class => TestAsset\InvokableObject::class], |
| 23 | + ], |
| 24 | + ]; |
| 25 | + yield 'aliases-factories' => [ |
| 26 | + [ |
| 27 | + 'aliases' => ['service' => TestAsset\InvokableObject::class], |
| 28 | + 'factories' => [TestAsset\InvokableObject::class => TestAsset\SampleFactory::class], |
| 29 | + ], |
| 30 | + ]; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @dataProvider config |
| 35 | + */ |
| 36 | + public function testIsSharedByDefault(array $config) |
| 37 | + { |
| 38 | + $container = $this->createContainer($config); |
| 39 | + |
| 40 | + $service1 = $container->get('service'); |
| 41 | + $service2 = $container->get('service'); |
| 42 | + |
| 43 | + $this->assertSame($service1, $service2); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dataProvider config |
| 48 | + */ |
| 49 | + public function testCanDisableSharedByDefault(array $config) |
| 50 | + { |
| 51 | + $container = $this->createContainer(array_merge($config, [ |
| 52 | + 'shared_by_default' => false, |
| 53 | + ])); |
| 54 | + |
| 55 | + $service1 = $container->get('service'); |
| 56 | + $service2 = $container->get('service'); |
| 57 | + |
| 58 | + $this->assertNotSame($service1, $service2); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @dataProvider config |
| 63 | + */ |
| 64 | + public function testCanDisableSharedForSingleService(array $config) |
| 65 | + { |
| 66 | + $container = $this->createContainer(array_merge($config, [ |
| 67 | + 'shared' => [ |
| 68 | + 'service' => false, |
| 69 | + ], |
| 70 | + ])); |
| 71 | + |
| 72 | + $service1 = $container->get('service'); |
| 73 | + $service2 = $container->get('service'); |
| 74 | + |
| 75 | + $this->assertNotSame($service1, $service2); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @dataProvider config |
| 80 | + */ |
| 81 | + public function testCanEnableSharedForSingleService(array $config) |
| 82 | + { |
| 83 | + $container = $this->createContainer(array_merge($config, [ |
| 84 | + 'shared_by_default' => false, |
| 85 | + 'shared' => [ |
| 86 | + 'service' => true, |
| 87 | + ], |
| 88 | + ])); |
| 89 | + |
| 90 | + $service1 = $container->get('service'); |
| 91 | + $service2 = $container->get('service'); |
| 92 | + |
| 93 | + $this->assertSame($service1, $service2); |
| 94 | + } |
| 95 | + |
| 96 | + private function createContainer(array $config) |
| 97 | + { |
| 98 | + return new ServiceManager($config); |
| 99 | + } |
| 100 | +} |
0 commit comments