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

Commit 3b005cb

Browse files
author
Moln
committed
Add CallOnlyOnceAbstractFactoryTest.
1 parent 0b3fe37 commit 3b005cb

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

test/CommonServiceLocatorBehaviorsTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Zend\ServiceManager\Factory\InvokableFactory;
2222
use Zend\ServiceManager\Initializer\InitializerInterface;
2323
use Zend\ServiceManager\ServiceLocatorInterface;
24+
use ZendTest\ServiceManager\TestAsset\CallOnlyOnceAbstractFactory;
2425
use ZendTest\ServiceManager\TestAsset\FailingAbstractFactory;
2526
use ZendTest\ServiceManager\TestAsset\FailingFactory;
2627
use ZendTest\ServiceManager\TestAsset\InvokableObject;
@@ -139,6 +140,22 @@ public function testCanCreateServiceWithAbstractFactory()
139140
$serviceManager->get(DateTime::class);
140141
}
141142

143+
public function testCallOnlyOnceWithMultipleIdenticalAbstractFactory()
144+
{
145+
CallOnlyOnceAbstractFactory::setCallTimes(0);
146+
147+
$serviceManager = $this->createContainer([
148+
'abstract_factories' => [
149+
new CallOnlyOnceAbstractFactory(),
150+
new CallOnlyOnceAbstractFactory(),
151+
]
152+
]);
153+
$serviceManager->addAbstractFactory(CallOnlyOnceAbstractFactory::class);
154+
$serviceManager->has(stdClass::class);
155+
156+
$this->assertEquals(1, CallOnlyOnceAbstractFactory::getCallTimes());
157+
}
158+
142159
public function testCanCreateServiceWithAlias()
143160
{
144161
$serviceManager = $this->createContainer([
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\ServiceManager\TestAsset;
11+
12+
use Interop\Container\ContainerInterface;
13+
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
14+
15+
class CallOnlyOnceAbstractFactory implements AbstractFactoryInterface
16+
{
17+
protected static $callTimes = 0;
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function canCreate(ContainerInterface $container, $name)
23+
{
24+
self::$callTimes++;
25+
26+
return false;
27+
}
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function __invoke(ContainerInterface $container, $className, array $options = null)
33+
{
34+
}
35+
36+
/**
37+
* @return int
38+
*/
39+
public static function getCallTimes()
40+
{
41+
return self::$callTimes;
42+
}
43+
44+
/**
45+
* @param int $callTimes
46+
*/
47+
public static function setCallTimes($callTimes)
48+
{
49+
self::$callTimes = $callTimes;
50+
}
51+
}

0 commit comments

Comments
 (0)