1<?php
2
3declare(strict_types=1);
4
5namespace Metadata\Tests\Driver;
6
7use Metadata\ClassMetadata;
8use Metadata\Driver\DriverChain;
9use PHPUnit\Framework\TestCase;
10
11class DriverChainTest extends TestCase
12{
13    public function testLoadMetadataForClass()
14    {
15        $driver = $this->createMock('Metadata\\Driver\\DriverInterface');
16        $driver
17            ->expects($this->once())
18            ->method('loadMetadataForClass')
19            ->will($this->returnValue($metadata = new ClassMetadata('\stdClass')))
20        ;
21        $chain = new DriverChain([$driver]);
22
23        $this->assertSame($metadata, $chain->loadMetadataForClass(new \ReflectionClass('\stdClass')));
24    }
25
26    public function testGetAllClassNames()
27    {
28        $driver1 = $this->createMock('Metadata\\Driver\\AdvancedDriverInterface');
29        $driver1
30            ->expects($this->once())
31            ->method('getAllClassNames')
32            ->will($this->returnValue(['Foo']));
33
34        $driver2 = $this->createMock('Metadata\\Driver\\AdvancedDriverInterface');
35        $driver2
36            ->expects($this->once())
37            ->method('getAllClassNames')
38            ->will($this->returnValue(['Bar']));
39
40        $chain = new DriverChain([$driver1, $driver2]);
41
42        $this->assertSame(['Foo', 'Bar'], $chain->getAllClassNames());
43    }
44
45    public function testLoadMetadataForClassReturnsNullWhenNoMetadataIsFound()
46    {
47        $driver = new DriverChain([]);
48        $this->assertNull($driver->loadMetadataForClass(new \ReflectionClass('\stdClass')));
49
50        $driver = $this->createMock('Metadata\\Driver\\DriverInterface');
51        $driver
52            ->expects($this->once())
53            ->method('loadMetadataForClass')
54            ->will($this->returnValue(null))
55        ;
56        $driverChain = new DriverChain([$driver]);
57        $this->assertNull($driver->loadMetadataForClass(new \ReflectionClass('\stdClass')));
58    }
59
60    public function testGetAllClassNamesThrowsException()
61    {
62        $this->expectException('RuntimeException');
63        $driver = $this->createMock('Metadata\\Driver\\DriverInterface');
64        $chain = new DriverChain([$driver]);
65        $chain->getAllClassNames();
66    }
67}
68