1<?php
2
3declare(strict_types=1);
4
5namespace Metadata\Tests\Driver;
6
7use Metadata\ClassMetadata;
8use PHPUnit\Framework\TestCase;
9
10/**
11 * @author Jordan Stout <j@jrdn.org>
12 */
13class AbstractFileDriverTest extends TestCase
14{
15    private static $extension = 'jms_metadata.yml';
16
17    /** @var \PHPUnit_Framework_MockObject_MockObject */
18    private $locator;
19
20    /** @var \PHPUnit_Framework_MockObject_MockObject */
21    private $driver;
22
23    public function setUp()
24    {
25        $this->locator = $this->createMock('Metadata\Driver\FileLocator', [], [], '', false);
26        $this->driver = $this->getMockBuilder('Metadata\Driver\AbstractFileDriver')
27            ->setConstructorArgs([$this->locator])
28            ->getMockForAbstractClass();
29
30        $this->driver->expects($this->any())->method('getExtension')->will($this->returnValue(self::$extension));
31    }
32
33    public function testLoadMetadataForClass()
34    {
35        $class = new \ReflectionClass('\stdClass');
36        $this->locator
37            ->expects($this->once())
38            ->method('findFileForClass')
39            ->with($class, self::$extension)
40            ->will($this->returnValue('Some\Path'));
41
42        $this->driver
43            ->expects($this->once())
44            ->method('loadMetadataFromFile')
45            ->with($class, 'Some\Path')
46            ->will($this->returnValue($metadata = new ClassMetadata('\stdClass')));
47
48        $this->assertSame($metadata, $this->driver->loadMetadataForClass($class));
49    }
50
51    public function testLoadMetadataForClassWillReturnNull()
52    {
53        $class = new \ReflectionClass('\stdClass');
54        $this->locator
55            ->expects($this->once())
56            ->method('findFileForClass')
57            ->with($class, self::$extension)
58            ->will($this->returnValue(null));
59
60        $this->assertSame(null, $this->driver->loadMetadataForClass($class));
61    }
62
63    public function testGetAllClassNames()
64    {
65        $class = new \ReflectionClass('\stdClass');
66        $this->locator
67            ->expects($this->once())
68            ->method('findAllClasses')
69            ->with(self::$extension)
70            ->will($this->returnValue(['\stdClass']));
71
72        $this->assertSame(['\stdClass'], $this->driver->getAllClassNames($class));
73    }
74
75    public function testGetAllClassNamesThrowsRuntimeException()
76    {
77        $this->expectException('RuntimeException');
78
79        $locator = $this->createMock('Metadata\Driver\FileLocatorInterface');
80        $driver = $this->getMockBuilder('Metadata\Driver\AbstractFileDriver')
81            ->setConstructorArgs([$locator])
82            ->getMockForAbstractClass();
83        $class = new \ReflectionClass('\stdClass');
84
85        $driver->getAllClassNames($class);
86    }
87}
88