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

Commit af51b39

Browse files
committed
Improved tests aliases with abstract factories.
There was an error in the testcase `testAbstractFactoryShouldBeCheckedForResolvedAliasesInsteadOfAliasName` where the returnValueMap of phpunit was used wrong. This caused a false positive. The test was returning an array which is interpreted as `TRUE`. I discovered this when I added the `testResolvedAliasFromAbstractFactory`, which should do the opposite form the earlier mentioned testcase. Beside that I found a way that the servicemanager was returning `null` instead of returning false when the last fallback on abstract factories failed.
1 parent aea63ce commit af51b39

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,15 @@ public function testAbstractFactoryShouldBeCheckedForResolvedAliasesInsteadOfAli
285285
],
286286
]);
287287

288-
$valueMap = [
289-
['Alias', false],
290-
['ServiceName', true],
291-
];
292-
293288
$abstractFactory
294289
->method('canCreate')
295290
->withConsecutive(
296291
[ $this->anything(), $this->equalTo('Alias') ],
297292
[ $this->anything(), $this->equalTo('ServiceName')]
298293
)
299-
->willReturn($this->returnValueMap($valueMap));
294+
->willReturnCallback(function($context, $name) {
295+
return $name === 'Alias';
296+
});
300297
$this->assertTrue($serviceManager->has('Alias'));
301298
}
302299

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

0 commit comments

Comments
 (0)