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

Commit 8cbd7b1

Browse files
author
fhein
committed
Changes as requested.
1 parent c679418 commit 8cbd7b1

File tree

3 files changed

+73
-29
lines changed

3 files changed

+73
-29
lines changed

test/CommonServiceLocatorBehaviorsTrait.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use function restore_error_handler;
3131
use function set_error_handler;
3232
use ZendTest\ServiceManager\TestAsset\SampleFactory;
33+
use ZendTest\ServiceManager\TestAsset\AbstractFactoryFoo;
3334

3435
trait CommonServiceLocatorBehaviorsTrait
3536
{
@@ -882,28 +883,29 @@ public function testCoverageDepthFirstTaggingOnRecursiveAliasDefinitions()
882883
}
883884

884885
/**
885-
* The particular test procedure called by
886-
* testConsistencyOverInternalStates
886+
* The ServiceManager can change internal state on calls to get,
887+
* build or has, latter not currently. Possible state changes
888+
* are caching a factory, registering a service produced by
889+
* a factory, ...
887890
*
888-
* This test calls build()/get() and in between has() 3 times for
889-
* the given service name.
890-
* It asserts that sm continues to have (has) a service, asserts
891-
* that the built services each reference an object different
892-
* from all other objects, and asserts that the services got
893-
* reference the same object.
891+
* This tests performs three consecutive calls to build/get for
892+
* each registered service to push the service manager through
893+
* all internal states, thereby verifying that build/get/has
894+
* remain stable through the internal states.
894895
*
895-
* @see testConsistencyOverInternalStates
896+
* @dataProvider provideConsistencyOverInternalStatesTests
896897
*
897898
* @param ContainerInterface $smTemplate
898899
* @param string $name
899900
* @param array[] string $test
900901
*/
901-
protected function checkConsistencyOverInternalStates($smTemplate, $name, $test)
902+
public function testConsistencyOverInternalStates($smTemplate, $name, $test)
902903
{
903904
$sm = clone $smTemplate;
904905
$object['get'] = [];
905906
$object['build'] = [];
906907

908+
907909
// call get()/build() and store the retrieved
908910
// objects in $object['get'] or $object['build']
909911
// respectively
@@ -912,18 +914,14 @@ protected function checkConsistencyOverInternalStates($smTemplate, $name, $test)
912914
$this->assertTrue($sm->has($name));
913915
}
914916

915-
// if there is more than one object in $object['get']
916-
// compare all to the first
917-
$nrShared = count($object['get']);
918-
for ($i = 1; $i < $nrShared; $i++) {
919-
$this->assertSame($object['get'][0], $object['get'][$i]);
917+
// compares the first to the first also, but ok
918+
foreach ($object['get'] as $sharedObj) {
919+
$this->assertSame($object['get'][0], $sharedObj);
920920
}
921921
// objects from object['build'] have to be different
922922
// from all other objects
923923
foreach ($object['build'] as $idx1 => $nonSharedObj) {
924-
foreach ($object['get'] as $sharedObj) {
925-
$this->assertNotSame($nonSharedObj, $sharedObj);
926-
}
924+
$this->assertNotContains($nonSharedObj, $object['get']);
927925
foreach ($object['build'] as $idx2 => $nonSharedObj2) {
928926
if ($idx1 !== $idx2) {
929927
$this->assertNotSame($nonSharedObj, $nonSharedObj2);
@@ -947,9 +945,9 @@ protected function checkConsistencyOverInternalStates($smTemplate, $name, $test)
947945
*
948946
* @param ContainerInterface $smTemplate
949947
* @param string $name
950-
* @param array[] string $test
948+
* @param string[] $test
951949
*/
952-
public function testConsistencyOverInternalStates()
950+
public function provideConsistencyOverInternalStatesTests()
953951
{
954952
$config = [
955953
'factories' => [
@@ -968,10 +966,10 @@ public function testConsistencyOverInternalStates()
968966
'serviceAlias' => 'service',
969967
'invokableAlias' => 'invokable',
970968
'factoryAlias' => 'factory',
971-
'abstractFactoryAlias' => stdClass::class
969+
'abstractFactoryAlias' => 'foo'
972970
],
973971
'abstract_factories' => [
974-
SimpleAbstractFactory::class,
972+
AbstractFactoryFoo::class
975973
]
976974
];
977975

@@ -982,26 +980,27 @@ public function testConsistencyOverInternalStates()
982980
foreach ($methods as $method1) {
983981
foreach ($methods as $method2) {
984982
foreach ($methods as $method3) {
985-
$tests[] = [$method1, $method2, $method3];
983+
$callSequences[] = [$method1, $method2, $method3];
986984
}
987985
}
988986
}
989987

990988
// To allow changes to the config above
991989
// $names is not hard coded
992-
$names = array_merge(
990+
$names = array_keys(array_merge(
993991
$config['factories'],
994992
$config['invokables'],
995993
$config['services'],
996994
$config['aliases']
997-
);
998-
$names[stdClass::class] = true;
995+
));
996+
$names[] = 'foo';
999997

1000-
foreach ($names as $name => $_) {
1001-
foreach ($tests as $test) {
998+
foreach ($names as $name) {
999+
foreach ($callSequences as $callSequence) {
10021000
$sm = clone $smTemplate;
1003-
$this->checkConsistencyOverInternalStates($smTemplate, $name, $test);
1001+
$tests[] = [$smTemplate, $name, $callSequence];
10041002
}
10051003
}
1004+
return $tests;
10061005
}
10071006
}

test/TestAsset/AbstractFactoryFoo.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* @link https://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\TestAsset;
9+
10+
use Interop\Container\ContainerInterface;
11+
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
12+
13+
class AbstractFactoryFoo implements AbstractFactoryInterface
14+
{
15+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
16+
{
17+
if ($requestedName === 'foo') {
18+
return new Foo($options);
19+
}
20+
return false;
21+
}
22+
23+
public function canCreate(ContainerInterface $container, $requestedName)
24+
{
25+
return ($requestedName === 'foo');
26+
}
27+
}

test/TestAsset/Foo.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* @link https://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\TestAsset;
9+
10+
class Foo
11+
{
12+
protected $options;
13+
14+
public function __construct($options = null)
15+
{
16+
$this->options = $options;
17+
}
18+
}

0 commit comments

Comments
 (0)