This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
NoShareByDefaultTest.php
130 lines (115 loc) · 3.96 KB
/
NoShareByDefaultTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ServiceManager
*/
namespace ZendTest\ServiceManager;
use Zend\ServiceManager\ServiceManager;
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use ReflectionObject;
/**
* Non-ShareByDefault service manager test
*
* This will test the service manager behavior when shareByDefault is set to
* FALSE
*/
class NoShareByDefaultTest extends TestCase
{
/**
* Service manager test instance
*
* @var \Zend\ServiceManager\ServiceManager
*/
private $serviceManager = null;
/**
* (non-PHPdoc)
* @see PHPUnit_Framework_TestCase::setUp()
*/
protected function setUp()
{
$this->serviceManager = $this->createServiceManager();
}
/**
* (non-PHPdoc)
* @see PHPUnit_Framework_TestCase::tearDown()
*/
protected function tearDown()
{
$this->serviceManager = null;
}
/**
* Create a service manager instance with shareByDefault set to FALSE
*
* @return \Zend\ServiceManager\ServiceManager
*/
private function createServiceManager()
{
$instance = new ServiceManager();
$reflection = new ReflectionObject($instance);
$property = $reflection->getProperty('shareByDefault');
$property->setAccessible(true);
$property->setValue($instance, false);
return $instance;
}
/**
* Test creating non-shared services by abstract factory when shareByDefault is FALSE
*
* shareByDefault = false
* Service definition share = undefined -> not shared
*
* @covers Zend\ServiceManager\ServiceManager::get
*/
public function testAbstractFactoryServiceIsNotSharedWithNoShareByDefault()
{
$factory = $this->getMockForAbstractClass('Zend\ServiceManager\AbstractFactoryInterface');
$factory->expects($this->any())
->method('canCreateServiceWithName')
->will($this->returnValue(true));
$factory->expects($this->any())
->method('createServiceWithName')
->will($this->returnCallback(function() {
return new stdClass();
}));
$this->serviceManager->addAbstractFactory($factory);
$a = $this->serviceManager->get('testservice');
$this->assertNotSame($a, $this->serviceManager->get('testservice'), 'Retrived service must not be shared');
}
/**
* Test non-shared declared service works
*
* Services declared as not shared explicitly should still not be shared.
*
* shareByDefault = false
* Service definition share = false -> not shared
*
* @covers Zend\ServiceManager\ServiceManager::get
*/
public function testServiceDeclaredNotSharedWorksWithNoShareByDefault()
{
$this->serviceManager->setInvokableClass('notsharedtest', 'stdClass', false);
$a = $this->serviceManager->get('notsharedtest');
$this->assertNotSame($a, $this->serviceManager->get('notsharedtest'), 'Registered service is not expected to be shared');
}
/**
* Test shared service works
*
* Even though shareByDefault is set to false, services declared as shared
* explicitly should still be shared.
*
* shareByDefault = false
* Service definition share = true -> shared
*
* @covers Zend\ServiceManager\ServiceManager::get
*/
public function testServiceDeclaredSharedWorksWithNoShareByDefault()
{
$this->serviceManager->setInvokableClass('sharedtest', 'stdClass', true);
$a = $this->serviceManager->get('sharedtest');
$this->assertSame($a, $this->serviceManager->get('sharedtest'), 'Registered service is expected to be shared');
}
}