30
30
use function restore_error_handler ;
31
31
use function set_error_handler ;
32
32
use ZendTest \ServiceManager \TestAsset \SampleFactory ;
33
+ use ZendTest \ServiceManager \TestAsset \AbstractFactoryFoo ;
33
34
34
35
trait CommonServiceLocatorBehaviorsTrait
35
36
{
@@ -882,28 +883,29 @@ public function testCoverageDepthFirstTaggingOnRecursiveAliasDefinitions()
882
883
}
883
884
884
885
/**
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, ...
887
890
*
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.
894
895
*
895
- * @see testConsistencyOverInternalStates
896
+ * @dataProvider provideConsistencyOverInternalStatesTests
896
897
*
897
898
* @param ContainerInterface $smTemplate
898
899
* @param string $name
899
900
* @param array[] string $test
900
901
*/
901
- protected function checkConsistencyOverInternalStates ($ smTemplate , $ name , $ test )
902
+ public function testConsistencyOverInternalStates ($ smTemplate , $ name , $ test )
902
903
{
903
904
$ sm = clone $ smTemplate ;
904
905
$ object ['get ' ] = [];
905
906
$ object ['build ' ] = [];
906
907
908
+
907
909
// call get()/build() and store the retrieved
908
910
// objects in $object['get'] or $object['build']
909
911
// respectively
@@ -912,18 +914,14 @@ protected function checkConsistencyOverInternalStates($smTemplate, $name, $test)
912
914
$ this ->assertTrue ($ sm ->has ($ name ));
913
915
}
914
916
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 );
920
920
}
921
921
// objects from object['build'] have to be different
922
922
// from all other objects
923
923
foreach ($ object ['build ' ] as $ idx1 => $ nonSharedObj ) {
924
- foreach ($ object ['get ' ] as $ sharedObj ) {
925
- $ this ->assertNotSame ($ nonSharedObj , $ sharedObj );
926
- }
924
+ $ this ->assertNotContains ($ nonSharedObj , $ object ['get ' ]);
927
925
foreach ($ object ['build ' ] as $ idx2 => $ nonSharedObj2 ) {
928
926
if ($ idx1 !== $ idx2 ) {
929
927
$ this ->assertNotSame ($ nonSharedObj , $ nonSharedObj2 );
@@ -947,9 +945,9 @@ protected function checkConsistencyOverInternalStates($smTemplate, $name, $test)
947
945
*
948
946
* @param ContainerInterface $smTemplate
949
947
* @param string $name
950
- * @param array[] string $test
948
+ * @param string[] $test
951
949
*/
952
- public function testConsistencyOverInternalStates ()
950
+ public function provideConsistencyOverInternalStatesTests ()
953
951
{
954
952
$ config = [
955
953
'factories ' => [
@@ -968,10 +966,10 @@ public function testConsistencyOverInternalStates()
968
966
'serviceAlias ' => 'service ' ,
969
967
'invokableAlias ' => 'invokable ' ,
970
968
'factoryAlias ' => 'factory ' ,
971
- 'abstractFactoryAlias ' => stdClass::class
969
+ 'abstractFactoryAlias ' => ' foo '
972
970
],
973
971
'abstract_factories ' => [
974
- SimpleAbstractFactory ::class,
972
+ AbstractFactoryFoo ::class
975
973
]
976
974
];
977
975
@@ -982,26 +980,27 @@ public function testConsistencyOverInternalStates()
982
980
foreach ($ methods as $ method1 ) {
983
981
foreach ($ methods as $ method2 ) {
984
982
foreach ($ methods as $ method3 ) {
985
- $ tests [] = [$ method1 , $ method2 , $ method3 ];
983
+ $ callSequences [] = [$ method1 , $ method2 , $ method3 ];
986
984
}
987
985
}
988
986
}
989
987
990
988
// To allow changes to the config above
991
989
// $names is not hard coded
992
- $ names = array_merge (
990
+ $ names = array_keys ( array_merge (
993
991
$ config ['factories ' ],
994
992
$ config ['invokables ' ],
995
993
$ config ['services ' ],
996
994
$ config ['aliases ' ]
997
- );
998
- $ names [stdClass::class ] = true ;
995
+ )) ;
996
+ $ names [] = ' foo ' ;
999
997
1000
- foreach ($ names as $ name => $ _ ) {
1001
- foreach ($ tests as $ test ) {
998
+ foreach ($ names as $ name ) {
999
+ foreach ($ callSequences as $ callSequence ) {
1002
1000
$ sm = clone $ smTemplate ;
1003
- $ this -> checkConsistencyOverInternalStates ( $ smTemplate , $ name , $ test ) ;
1001
+ $ tests [] = [ $ smTemplate , $ name , $ callSequence ] ;
1004
1002
}
1005
1003
}
1004
+ return $ tests ;
1006
1005
}
1007
1006
}
0 commit comments