Skip to content

Commit

Permalink
Merge pull request #84 from open-source-contributions/test_enhancement
Browse files Browse the repository at this point in the history
Test enhancement
  • Loading branch information
goetas committed Oct 24, 2019
2 parents 8d89581 + 02e75c4 commit 8692edc
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 105 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ matrix:
- php: 7.2
- php: 7.2
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
- php: 7.3
fast_finish: true

before_script:
Expand All @@ -29,5 +30,3 @@ script:
after_success:
- if [[ $TRAVIS_PHP_VERSION = '7.2' ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [[ $TRAVIS_PHP_VERSION = '7.2' ]]; then php ocular.phar code-coverage:upload --format=php-clover clover; fi


3 changes: 2 additions & 1 deletion src/MetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ private function addClassMetadata(&$metadata, ClassMetadata $toAdd): void
}
} else {
if (null === $metadata) {
$metadata = new $this->hierarchyMetadataClass();
$class = $this->hierarchyMetadataClass;
$metadata = new $class();
}

$metadata->addClassMetadata($toAdd);
Expand Down
2 changes: 1 addition & 1 deletion tests/Cache/DoctrineCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class DoctrineCacheAdapterTest extends TestCase
{
public function setUp()
protected function setUp()
{
if (!interface_exists('Doctrine\Common\Cache\Cache')) {
$this->markTestSkipped('Doctrine\Common is not installed.');
Expand Down
4 changes: 2 additions & 2 deletions tests/Cache/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FileCacheTest extends TestCase
{
private $dir;

public function setUp()
protected function setUp()
{
$this->dir = sys_get_temp_dir() . '/jms-' . md5(__CLASS__);
if (is_dir($this->dir)) {
Expand Down Expand Up @@ -49,7 +49,7 @@ public function testNonReturningCache(string $fileContents)
{
$cache = new FileCache($this->dir);

file_put_contents($this->dir.'/Metadata-Tests-Fixtures-TestObject.cache.php', $fileContents);
file_put_contents($this->dir . '/Metadata-Tests-Fixtures-TestObject.cache.php', $fileContents);

$this->assertNull($cache->load(TestObject::class));
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Cache/PsrCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
use Metadata\Tests\Fixtures\TestObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\CacheItem;

/**
* @requires PHP 5.5
*/
class PsrCacheAdapterTest extends TestCase
{
public function setUp()
protected function setUp()
{
if (!class_exists('Symfony\Component\Cache\CacheItem')) {
if (!class_exists(CacheItem::class)) {
$this->markTestSkipped('symfony/cache is not installed.');
}
}
Expand Down
7 changes: 4 additions & 3 deletions tests/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
namespace Metadata\Tests;

use Metadata\ClassMetadata;
use Metadata\Tests\Fixtures\TestObject;
use PHPUnit\Framework\TestCase;

class ClassMetadataTest extends TestCase
{
public function testConstructor()
{
$metadata = new ClassMetadata('Metadata\Tests\Fixtures\TestObject');
$metadata = new ClassMetadata(TestObject::class);

$this->assertEquals('Metadata\Tests\Fixtures\TestObject', $metadata->name);
}

public function testSerializeUnserialize()
{
$metadata = new ClassMetadata('Metadata\Tests\Fixtures\TestObject');
$metadata = new ClassMetadata(TestObject::class);

$this->assertEquals($metadata, unserialize(serialize($metadata)));
}

public function testIsFresh()
{
$ref = new \ReflectionClass('Metadata\Tests\Fixtures\TestObject');
$ref = new \ReflectionClass(TestObject::class);
touch($ref->getFilename());
sleep(2);

Expand Down
27 changes: 15 additions & 12 deletions tests/Driver/AbstractFileDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Metadata\Tests\Driver;

use Metadata\ClassMetadata;
use Metadata\Driver\AbstractFileDriver;
use Metadata\Driver\FileLocator;
use Metadata\Driver\FileLocatorInterface;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -20,10 +23,10 @@ class AbstractFileDriverTest extends TestCase
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $driver;

public function setUp()
protected function setUp()
{
$this->locator = $this->createMock('Metadata\Driver\FileLocator', [], [], '', false);
$this->driver = $this->getMockBuilder('Metadata\Driver\AbstractFileDriver')
$this->locator = $this->createMock(FileLocator::class, [], [], '', false);
$this->driver = $this->getMockBuilder(AbstractFileDriver::class)
->setConstructorArgs([$this->locator])
->getMockForAbstractClass();

Expand All @@ -32,7 +35,7 @@ public function setUp()

public function testLoadMetadataForClass()
{
$class = new \ReflectionClass('\stdClass');
$class = new \ReflectionClass(\stdClass::class);
$this->locator
->expects($this->once())
->method('findFileForClass')
Expand All @@ -43,26 +46,26 @@ public function testLoadMetadataForClass()
->expects($this->once())
->method('loadMetadataFromFile')
->with($class, 'Some\Path')
->will($this->returnValue($metadata = new ClassMetadata('\stdClass')));
->will($this->returnValue($metadata = new ClassMetadata(\stdClass::class)));

$this->assertSame($metadata, $this->driver->loadMetadataForClass($class));
}

public function testLoadMetadataForClassWillReturnNull()
{
$class = new \ReflectionClass('\stdClass');
$class = new \ReflectionClass(\stdClass::class);
$this->locator
->expects($this->once())
->method('findFileForClass')
->with($class, self::$extension)
->will($this->returnValue(null));

$this->assertSame(null, $this->driver->loadMetadataForClass($class));
$this->assertNull($this->driver->loadMetadataForClass($class));
}

public function testGetAllClassNames()
{
$class = new \ReflectionClass('\stdClass');
$class = new \ReflectionClass(\stdClass::class);
$this->locator
->expects($this->once())
->method('findAllClasses')
Expand All @@ -74,13 +77,13 @@ public function testGetAllClassNames()

public function testGetAllClassNamesThrowsRuntimeException()
{
$this->expectException('RuntimeException');
$this->expectException(\RuntimeException::class);

$locator = $this->createMock('Metadata\Driver\FileLocatorInterface');
$driver = $this->getMockBuilder('Metadata\Driver\AbstractFileDriver')
$locator = $this->createMock(FileLocatorInterface::class);
$driver = $this->getMockBuilder(AbstractFileDriver::class)
->setConstructorArgs([$locator])
->getMockForAbstractClass();
$class = new \ReflectionClass('\stdClass');
$class = new \ReflectionClass(\stdClass::class);

$driver->getAllClassNames($class);
}
Expand Down
24 changes: 13 additions & 11 deletions tests/Driver/DriverChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,35 @@
namespace Metadata\Tests\Driver;

use Metadata\ClassMetadata;
use Metadata\Driver\AdvancedDriverInterface;
use Metadata\Driver\DriverChain;
use Metadata\Driver\DriverInterface;
use PHPUnit\Framework\TestCase;

class DriverChainTest extends TestCase
{
public function testLoadMetadataForClass()
{
$driver = $this->createMock('Metadata\\Driver\\DriverInterface');
$driver = $this->createMock(DriverInterface::class);
$driver
->expects($this->once())
->method('loadMetadataForClass')
->will($this->returnValue($metadata = new ClassMetadata('\stdClass')))
->will($this->returnValue($metadata = new ClassMetadata(\stdClass::class)))
;
$chain = new DriverChain([$driver]);

$this->assertSame($metadata, $chain->loadMetadataForClass(new \ReflectionClass('\stdClass')));
$this->assertSame($metadata, $chain->loadMetadataForClass(new \ReflectionClass(\stdClass::class)));
}

public function testGetAllClassNames()
{
$driver1 = $this->createMock('Metadata\\Driver\\AdvancedDriverInterface');
$driver1 = $this->createMock(AdvancedDriverInterface::class);
$driver1
->expects($this->once())
->method('getAllClassNames')
->will($this->returnValue(['Foo']));

$driver2 = $this->createMock('Metadata\\Driver\\AdvancedDriverInterface');
$driver2 = $this->createMock(AdvancedDriverInterface::class);
$driver2
->expects($this->once())
->method('getAllClassNames')
Expand All @@ -45,22 +47,22 @@ public function testGetAllClassNames()
public function testLoadMetadataForClassReturnsNullWhenNoMetadataIsFound()
{
$driver = new DriverChain([]);
$this->assertNull($driver->loadMetadataForClass(new \ReflectionClass('\stdClass')));
$this->assertNull($driver->loadMetadataForClass(new \ReflectionClass(\stdClass::class)));

$driver = $this->createMock('Metadata\\Driver\\DriverInterface');
$driver = $this->createMock(DriverInterface::class);
$driver
->expects($this->once())
->method('loadMetadataForClass')
->will($this->returnValue(null))
;
$driverChain = new DriverChain([$driver]);
$this->assertNull($driver->loadMetadataForClass(new \ReflectionClass('\stdClass')));
new DriverChain([$driver]);
$this->assertNull($driver->loadMetadataForClass(new \ReflectionClass(\stdClass::class)));
}

public function testGetAllClassNamesThrowsException()
{
$this->expectException('RuntimeException');
$driver = $this->createMock('Metadata\\Driver\\DriverInterface');
$this->expectException(\RuntimeException::class);
$driver = $this->createMock(DriverInterface::class);
$chain = new DriverChain([$driver]);
$chain->getAllClassNames();
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Driver/FileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Metadata\Tests\Driver;

use Metadata\Driver\FileLocator;
use Metadata\Tests\Driver\Fixture\A\A;
use Metadata\Tests\Driver\Fixture\B\B;
use Metadata\Tests\Driver\Fixture\C\SubDir\C;
use Metadata\Tests\Driver\Fixture\T\T;
use PHPUnit\Framework\TestCase;

class FileLocatorTest extends TestCase
Expand All @@ -17,27 +21,23 @@ public function testFindFileForClass()
'Metadata\Tests\Driver\Fixture\C' => __DIR__ . '/Fixture/C',
]);

$ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\A\A');
$ref = new \ReflectionClass(A::class);
$this->assertEquals(realpath(__DIR__ . '/Fixture/A/A.xml'), realpath($locator->findFileForClass($ref, 'xml')));

$ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\B\B');
$ref = new \ReflectionClass(B::class);
$this->assertNull($locator->findFileForClass($ref, 'xml'));

$ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\C\SubDir\C');
$ref = new \ReflectionClass(C::class);
$this->assertEquals(realpath(__DIR__ . '/Fixture/C/SubDir.C.yml'), realpath($locator->findFileForClass($ref, 'yml')));
}

public function testTraits()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('No traits available');
}

$locator = new FileLocator([
'Metadata\Tests\Driver\Fixture\T' => __DIR__ . '/Fixture/T',
]);

$ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\T\T');
$ref = new \ReflectionClass(T::class);
$this->assertEquals(realpath(__DIR__ . '/Fixture/T/T.xml'), realpath($locator->findFileForClass($ref, 'xml')));
}

Expand Down
6 changes: 4 additions & 2 deletions tests/MergeableClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
namespace Metadata\Tests;

use Metadata\MergeableClassMetadata;
use Metadata\Tests\Fixtures\TestObject;
use Metadata\Tests\Fixtures\TestParent;
use PHPUnit\Framework\TestCase;

class MergeableClassMetadataTest extends TestCase
{
public function testMerge()
{
$parentMetadata = new MergeableClassMetadata('Metadata\Tests\Fixtures\TestParent');
$parentMetadata = new MergeableClassMetadata(TestParent::class);
$parentMetadata->propertyMetadata['foo'] = 'bar';
$parentMetadata->propertyMetadata['baz'] = 'baz';
$parentMetadata->methodMetadata['foo'] = 'bar';
$parentMetadata->createdAt = 2;
$parentMetadata->fileResources[] = 'foo';

$childMetadata = new MergeableClassMetadata('Metadata\Tests\Fixtures\TestObject');
$childMetadata = new MergeableClassMetadata(TestObject::class);
$childMetadata->propertyMetadata['foo'] = 'baz';
$childMetadata->methodMetadata['foo'] = 'baz';
$childMetadata->createdAt = 1;
Expand Down
Loading

0 comments on commit 8692edc

Please sign in to comment.