Skip to content

Commit fce4b8e

Browse files
committed
fixed a bug when container matches interface handler but it is does not have key zero
1 parent d695c3f commit fce4b8e

3 files changed

Lines changed: 26 additions & 26 deletions

File tree

src/HydratorContainer/FallbackHydratorContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getHandler($class)
6161
throw new \RuntimeException(sprintf('Class %s implements interfaces with colliding handlers!', $class));
6262
}
6363

64-
return $this->interfaces[$interfaces[0]];
64+
return $this->interfaces[array_shift($interfaces)];
6565
}
6666

6767
return null;

src/NormalizerContainer/FallbackNormalizerContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getHandler($class)
6161
throw new \RuntimeException(sprintf('Class %s implements interfaces with colliding handlers!', $class));
6262
}
6363

64-
return $this->interfaces[$interfaces[0]];
64+
return $this->interfaces[array_shift($interfaces)];
6565
}
6666

6767
return null;

tests/NormalizerContainerTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,37 @@ class NormalizerContainerTest extends \PHPUnit_Framework_TestCase
1010
{
1111
public function testAlias()
1212
{
13-
$handlers = new FallbackNormalizerContainer();
14-
$handlers->add('stdClass', 'std', function() { return 'value'; });
15-
$handlers->addAlias('DateTime', 'stdClass');
13+
$normalizers = new FallbackNormalizerContainer();
14+
$normalizers->add('stdClass', 'std', function() { return 'value'; });
15+
$normalizers->addAlias('DateTime', 'stdClass');
1616

17-
$this->assertSame('value', call_user_func_array($handlers->getHandler('stdClass'), array()));
18-
$this->assertSame('value', call_user_func_array($handlers->getHandler('DateTime'), array()));
17+
$this->assertSame('value', call_user_func_array($normalizers->getHandler('stdClass'), array()));
18+
$this->assertSame('value', call_user_func_array($normalizers->getHandler('DateTime'), array()));
1919
}
2020

2121
public function testInterface()
2222
{
23-
$hydrators = new FallbackNormalizerContainer();
23+
$normalizers = new FallbackNormalizerContainer();
2424
$interfaceName = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface';
2525
$interfaceTypeA = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeA';
2626
$interfaceTypeB = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeB';
27-
$hydrators->add($interfaceName, 'type', function() { return 'type'; });
27+
$normalizers->add($interfaceName, 'type', function() { return 'type'; });
2828

29-
$this->assertSame('type', call_user_func_array($hydrators->getHandler($interfaceTypeA), array()));
30-
$this->assertSame('type', call_user_func_array($hydrators->getHandler($interfaceTypeB), array()));
29+
$this->assertSame('type', call_user_func_array($normalizers->getHandler($interfaceTypeA), array()));
30+
$this->assertSame('type', call_user_func_array($normalizers->getHandler($interfaceTypeB), array()));
3131
}
3232

3333
public function testInheritance()
3434
{
35-
$hydrators = new FallbackNormalizerContainer();
35+
$normalizers = new FallbackNormalizerContainer();
3636
$ancestorName = 'Thunder\Serializard\Tests\Fake\FakeUserParentParent';
3737
$parentName = 'Thunder\Serializard\Tests\Fake\FakeUserParent';
3838
$userName = 'Thunder\Serializard\Tests\Fake\FakeUser';
39-
$hydrators->add($ancestorName, 'type', function() { return 'ancestor'; });
39+
$normalizers->add($ancestorName, 'type', function() { return 'ancestor'; });
4040

41-
$this->assertSame('ancestor', call_user_func_array($hydrators->getHandler($ancestorName), array()));
42-
$this->assertSame('ancestor', call_user_func_array($hydrators->getHandler($parentName), array()));
43-
$this->assertSame('ancestor', call_user_func_array($hydrators->getHandler($userName), array()));
41+
$this->assertSame('ancestor', call_user_func_array($normalizers->getHandler($ancestorName), array()));
42+
$this->assertSame('ancestor', call_user_func_array($normalizers->getHandler($parentName), array()));
43+
$this->assertSame('ancestor', call_user_func_array($normalizers->getHandler($userName), array()));
4444
}
4545

4646
public function testMultipleInterfacesException()
@@ -49,32 +49,32 @@ public function testMultipleInterfacesException()
4949
$typeAnother = 'Thunder\Serializard\Tests\Fake\Interfaces\AnotherTypeInterface';
5050
$typeMultiple = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeMultiple';
5151

52-
$hydrators = new FallbackNormalizerContainer();
53-
$hydrators->add($typeInterface, 'type', function() { return 'multiple'; });
54-
$hydrators->add($typeAnother, 'type', function() { return 'multiple'; });
52+
$normalizers = new FallbackNormalizerContainer();
53+
$normalizers->add($typeInterface, 'type', function() { return 'multiple'; });
54+
$normalizers->add($typeAnother, 'type', function() { return 'multiple'; });
5555

5656
$this->setExpectedException('RuntimeException');
57-
$hydrators->getHandler($typeMultiple);
57+
$normalizers->getHandler($typeMultiple);
5858
}
5959

6060
public function testInvalidClassOrInterfaceName()
6161
{
62-
$handlers = new FallbackNormalizerContainer();
62+
$normalizers = new FallbackNormalizerContainer();
6363
$this->setExpectedException('RuntimeException');
64-
$handlers->add('invalid', 'root', function() {});
64+
$normalizers->add('invalid', 'root', function() {});
6565
}
6666

6767
public function testAliasForInvalidClass()
6868
{
69-
$handlers = new FallbackNormalizerContainer();
69+
$normalizers = new FallbackNormalizerContainer();
7070
$this->setExpectedException('RuntimeException');
71-
$handlers->addAlias('stdClass', 'DateTime');
71+
$normalizers->addAlias('stdClass', 'DateTime');
7272
}
7373

7474
public function testInvalidHandler()
7575
{
76-
$handlers = new FallbackNormalizerContainer();
76+
$normalizers = new FallbackNormalizerContainer();
7777
$this->setExpectedException('RuntimeException');
78-
$handlers->add('stdClass', 'name', 'invalid');
78+
$normalizers->add('stdClass', 'name', 'invalid');
7979
}
8080
}

0 commit comments

Comments
 (0)