|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @see https://github.com/zendframework/zend-container-test 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-container-test/blob/master/LICENSE.md New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Zend\ContainerConfigTest; |
| 11 | + |
| 12 | +use Generator; |
| 13 | + |
| 14 | +trait SharedTestTrait |
| 15 | +{ |
| 16 | + public function config() : Generator |
| 17 | + { |
| 18 | + yield 'factory' => [ |
| 19 | + ['factories' => ['service' => TestAsset\Factory::class]], |
| 20 | + 'service' |
| 21 | + ]; |
| 22 | + |
| 23 | + yield 'invokable' => [ |
| 24 | + ['invokables' => [TestAsset\Service::class => TestAsset\Service::class]], |
| 25 | + TestAsset\Service::class |
| 26 | + ]; |
| 27 | + |
| 28 | + yield 'aliased-invokable' => [ |
| 29 | + [ |
| 30 | + 'aliases' => ['service' => TestAsset\Service::class], |
| 31 | + 'invokables' => [TestAsset\Service::class => TestAsset\Service::class], |
| 32 | + ], |
| 33 | + 'service', |
| 34 | + ]; |
| 35 | + |
| 36 | + yield 'aliased-factory' => [ |
| 37 | + [ |
| 38 | + 'aliases' => ['service' => TestAsset\Service::class], |
| 39 | + 'factories' => [TestAsset\Service::class => TestAsset\Factory::class], |
| 40 | + ], |
| 41 | + 'service', |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @dataProvider config |
| 47 | + */ |
| 48 | + public function testIsSharedByDefault(array $config, string $serviceToTest) : void |
| 49 | + { |
| 50 | + $container = $this->createContainer($config); |
| 51 | + |
| 52 | + $service1 = $container->get($serviceToTest); |
| 53 | + $service2 = $container->get($serviceToTest); |
| 54 | + |
| 55 | + self::assertSame($service1, $service2); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @dataProvider config |
| 60 | + */ |
| 61 | + public function testCanDisableSharedByDefault(array $config, string $serviceToTest) : void |
| 62 | + { |
| 63 | + $container = $this->createContainer(array_merge($config, [ |
| 64 | + 'shared_by_default' => false, |
| 65 | + ])); |
| 66 | + |
| 67 | + $service1 = $container->get($serviceToTest); |
| 68 | + $service2 = $container->get($serviceToTest); |
| 69 | + |
| 70 | + self::assertNotSame($service1, $service2); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @dataProvider config |
| 75 | + */ |
| 76 | + public function testCanDisableSharedForSingleService(array $config, string $serviceToTest) : void |
| 77 | + { |
| 78 | + $container = $this->createContainer(array_merge($config, [ |
| 79 | + 'shared' => [ |
| 80 | + $serviceToTest => false, |
| 81 | + ], |
| 82 | + ])); |
| 83 | + |
| 84 | + $service1 = $container->get($serviceToTest); |
| 85 | + $service2 = $container->get($serviceToTest); |
| 86 | + |
| 87 | + self::assertNotSame($service1, $service2); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @dataProvider config |
| 92 | + */ |
| 93 | + public function testCanEnableSharedForSingleService(array $config, string $serviceToTest) : void |
| 94 | + { |
| 95 | + $container = $this->createContainer(array_merge($config, [ |
| 96 | + 'shared_by_default' => false, |
| 97 | + 'shared' => [ |
| 98 | + $serviceToTest => true, |
| 99 | + ], |
| 100 | + ])); |
| 101 | + |
| 102 | + $service1 = $container->get($serviceToTest); |
| 103 | + $service2 = $container->get($serviceToTest); |
| 104 | + |
| 105 | + self::assertSame($service1, $service2); |
| 106 | + } |
| 107 | + |
| 108 | + public function testServiceIsSharedByDefault() : void |
| 109 | + { |
| 110 | + $service = new TestAsset\Service(); |
| 111 | + $container = $this->createContainer([ |
| 112 | + 'services' => [ |
| 113 | + 'service' => $service, |
| 114 | + ], |
| 115 | + ]); |
| 116 | + |
| 117 | + $service1 = $container->get('service'); |
| 118 | + $service2 = $container->get('service'); |
| 119 | + |
| 120 | + self::assertSame($service, $service1); |
| 121 | + self::assertSame($service, $service2); |
| 122 | + } |
| 123 | + |
| 124 | + public function testServiceIsSharedEvenIfSharedByDefaultIsFalse() : void |
| 125 | + { |
| 126 | + $service = new TestAsset\Service(); |
| 127 | + $container = $this->createContainer([ |
| 128 | + 'services' => [ |
| 129 | + 'service' => $service, |
| 130 | + ], |
| 131 | + 'shared_by_default' => false, |
| 132 | + ]); |
| 133 | + |
| 134 | + $service1 = $container->get('service'); |
| 135 | + $service2 = $container->get('service'); |
| 136 | + |
| 137 | + self::assertSame($service, $service1); |
| 138 | + self::assertSame($service, $service2); |
| 139 | + } |
| 140 | + |
| 141 | + public function testServiceIsSharedEvenIfHasSharedSetToFalse() : void |
| 142 | + { |
| 143 | + $service = new TestAsset\Service(); |
| 144 | + $container = $this->createContainer([ |
| 145 | + 'services' => [ |
| 146 | + 'service' => $service, |
| 147 | + ], |
| 148 | + 'shared' => [ |
| 149 | + 'service' => false, |
| 150 | + ], |
| 151 | + ]); |
| 152 | + |
| 153 | + $service1 = $container->get('service'); |
| 154 | + $service2 = $container->get('service'); |
| 155 | + |
| 156 | + self::assertSame($service, $service1); |
| 157 | + self::assertSame($service, $service2); |
| 158 | + } |
| 159 | + |
| 160 | + public function testServiceIsSharedWhenAccessedByAlias() : void |
| 161 | + { |
| 162 | + $service = new TestAsset\Service(); |
| 163 | + $container = $this->createContainer([ |
| 164 | + 'aliases' => [ |
| 165 | + 'alias' => 'service', |
| 166 | + ], |
| 167 | + 'services' => [ |
| 168 | + 'service' => $service, |
| 169 | + ], |
| 170 | + ]); |
| 171 | + |
| 172 | + $aliasService = $container->get('alias'); |
| 173 | + |
| 174 | + self::assertSame($service, $aliasService); |
| 175 | + } |
| 176 | +} |
0 commit comments