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

Commit 54f5a52

Browse files
committed
Merge branch 'fix/#241-improved-alias-tests' into develop
Close #241
2 parents e35a8d1 + 39b4444 commit 54f5a52

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

src/ServiceManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ public function has($name)
278278
return true;
279279
}
280280
}
281+
282+
return false;
281283
}
282284

283285
/**

test/ServiceManagerTest.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
namespace ZendTest\ServiceManager;
99

1010
use DateTime;
11-
use Interop\Container\Exception\ContainerException;
1211
use PHPUnit\Framework\TestCase;
1312
use Psr\Container\ContainerInterface;
1413
use stdClass;
15-
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
16-
use Zend\ServiceManager\Exception\ServiceNotFoundException;
1714
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
1815
use Zend\ServiceManager\Factory\FactoryInterface;
1916
use Zend\ServiceManager\Factory\InvokableFactory;
@@ -285,18 +282,15 @@ public function testAbstractFactoryShouldBeCheckedForResolvedAliasesInsteadOfAli
285282
],
286283
]);
287284

288-
$valueMap = [
289-
['Alias', false],
290-
['ServiceName', true],
291-
];
292-
293285
$abstractFactory
294286
->method('canCreate')
295287
->withConsecutive(
296288
[ $this->anything(), $this->equalTo('Alias') ],
297289
[ $this->anything(), $this->equalTo('ServiceName')]
298290
)
299-
->willReturn($this->returnValueMap($valueMap));
291+
->willReturnCallback(function ($context, $name) {
292+
return $name === 'Alias';
293+
});
300294
$this->assertTrue($serviceManager->has('Alias'));
301295
}
302296

@@ -315,4 +309,56 @@ public function testFactoryMayBeStaticMethodDescribedByCallableString()
315309
$serviceManager = new SimpleServiceManager($config);
316310
$this->assertEquals(stdClass::class, get_class($serviceManager->get(stdClass::class)));
317311
}
312+
313+
public function testResolvedAliasFromAbstractFactory()
314+
{
315+
$abstractFactory = $this->createMock(AbstractFactoryInterface::class);
316+
317+
$serviceManager = new SimpleServiceManager([
318+
'aliases' => [
319+
'Alias' => 'ServiceName',
320+
],
321+
'abstract_factories' => [
322+
$abstractFactory,
323+
],
324+
]);
325+
326+
$abstractFactory
327+
->expects(self::any())
328+
->method('canCreate')
329+
->withConsecutive(
330+
[self::anything(), 'Alias'],
331+
[self::anything(), 'ServiceName']
332+
)
333+
->will(self::returnCallback(function ($context, $name) {
334+
return $name === 'ServiceName';
335+
}));
336+
337+
self::assertTrue($serviceManager->has('Alias'));
338+
}
339+
340+
public function testResolvedAliasNoMatchingAbstractFactoryReturnsFalse()
341+
{
342+
$abstractFactory = $this->createMock(AbstractFactoryInterface::class);
343+
344+
$serviceManager = new SimpleServiceManager([
345+
'aliases' => [
346+
'Alias' => 'ServiceName',
347+
],
348+
'abstract_factories' => [
349+
$abstractFactory,
350+
],
351+
]);
352+
353+
$abstractFactory
354+
->expects(self::any())
355+
->method('canCreate')
356+
->withConsecutive(
357+
[self::anything(), 'Alias'],
358+
[self::anything(), 'ServiceName']
359+
)
360+
->willReturn(false);
361+
362+
self::assertFalse($serviceManager->has('Alias'));
363+
}
318364
}

0 commit comments

Comments
 (0)