Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Added more test for shared services (aliases, factories, invokables)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Apr 16, 2018
1 parent f9b2a18 commit e80999b
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions test/ContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* @see https://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\ServiceManager;

use PHPUnit\Framework\TestCase;
use Zend\ServiceManager\ServiceManager;

class ContainerTest extends TestCase
{
public function config()
{
yield 'factories' => [['factories' => ['service' => TestAsset\SampleFactory::class]]];
yield 'invokables' => [['invokables' => ['service' => TestAsset\InvokableObject::class]]];
yield 'aliases-invokables' => [
[
'aliases' => ['service' => TestAsset\InvokableObject::class],
'invokables' => [TestAsset\InvokableObject::class => TestAsset\InvokableObject::class],
],
];
yield 'aliases-factories' => [
[
'aliases' => ['service' => TestAsset\InvokableObject::class],
'factories' => [TestAsset\InvokableObject::class => TestAsset\SampleFactory::class],
],
];
}

/**
* @dataProvider config
*/
public function testIsSharedByDefault(array $config)
{
$container = $this->createContainer($config);

$service1 = $container->get('service');
$service2 = $container->get('service');

$this->assertSame($service1, $service2);
}

/**
* @dataProvider config
*/
public function testCanDisableSharedByDefault(array $config)
{
$container = $this->createContainer(array_merge($config, [
'shared_by_default' => false,
]));

$service1 = $container->get('service');
$service2 = $container->get('service');

$this->assertNotSame($service1, $service2);
}

/**
* @dataProvider config
*/
public function testCanDisableSharedForSingleService(array $config)
{
$container = $this->createContainer(array_merge($config, [
'shared' => [
'service' => false,
],
]));

$service1 = $container->get('service');
$service2 = $container->get('service');

$this->assertNotSame($service1, $service2);
}

/**
* @dataProvider config
*/
public function testCanEnableSharedForSingleService(array $config)
{
$container = $this->createContainer(array_merge($config, [
'shared_by_default' => false,
'shared' => [
'service' => true,
],
]));

$service1 = $container->get('service');
$service2 = $container->get('service');

$this->assertSame($service1, $service2);
}

private function createContainer(array $config)
{
return new ServiceManager($config);
}
}

0 comments on commit e80999b

Please sign in to comment.