Skip to content

Commit

Permalink
Add more unit tests for metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Aug 24, 2016
1 parent 0f7ec87 commit 2b6dd3c
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 0 deletions.
78 changes: 78 additions & 0 deletions tests/Metadata/Property/PropertyMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Tests\Metadata\Property;

use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Symfony\Component\PropertyInfo\Type;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class PropertyMetadataTest extends \PHPUnit_Framework_TestCase
{
public function testValueObject()
{
$type = new Type(Type::BUILTIN_TYPE_STRING);
$metadata = new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'desc', true, true, false, false, true, false, 'http://example.com/foo', ['foo' => 'bar']);
$this->assertEquals($type, $metadata->getType());
$this->assertEquals('desc', $metadata->getDescription());
$this->assertTrue($metadata->isReadable());
$this->assertTrue($metadata->isWritable());
$this->assertFalse($metadata->isReadableLink());
$this->assertFalse($metadata->isWritableLink());
$this->assertTrue($metadata->isRequired());
$this->assertFalse($metadata->isIdentifier());
$this->assertEquals('http://example.com/foo', $metadata->getIri());
$this->assertEquals(['foo' => 'bar'], $metadata->getAttributes());

$newType = new Type(Type::BUILTIN_TYPE_BOOL);
$newMetadata = $metadata->withType($newType);
$this->assertFalse($newMetadata === $metadata);
$this->assertEquals($newType, $newMetadata->getType());

$newMetadata = $metadata->withDescription('description');
$this->assertFalse($newMetadata === $metadata);
$this->assertEquals('description',$newMetadata->getDescription());

$newMetadata = $metadata->withReadable(false);
$this->assertFalse($newMetadata === $metadata);
$this->assertFalse($newMetadata->isReadable());

$newMetadata = $metadata->withWritable(false);
$this->assertFalse($newMetadata === $metadata);
$this->assertFalse($newMetadata->isWritable());

$newMetadata = $metadata->withReadableLink(true);
$this->assertFalse($newMetadata === $metadata);
$this->assertTrue($newMetadata->isReadableLink());

$newMetadata = $metadata->withWritableLink(true);
$this->assertFalse($newMetadata === $metadata);
$this->assertTrue($newMetadata->isWritableLink());

$newMetadata = $metadata->withRequired(false);
$this->assertFalse($newMetadata === $metadata);
$this->assertFalse($newMetadata->isRequired());

$newMetadata = $metadata->withIdentifier(true);
$this->assertFalse($newMetadata === $metadata);
$this->assertTrue($newMetadata->isIdentifier());

$newMetadata = $metadata->withIri('foo:bar');
$this->assertFalse($newMetadata === $metadata);
$this->assertEquals('foo:bar', $newMetadata->getIri());

$newMetadata = $metadata->withAttributes(['a' => 'b']);
$this->assertFalse($newMetadata === $metadata);
$this->assertEquals(['a' => 'b'], $newMetadata->getAttributes());
}
}
30 changes: 30 additions & 0 deletions tests/Metadata/Property/PropertyNameCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/


namespace ApiPlatform\Core\Tests\Metadata\Property;

use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class PropertyNameCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testValueObject()
{
$collection = new PropertyNameCollection(['foo', 'bar']);
$this->assertInstanceOf(\Countable::class, $collection);
$this->assertInstanceOf(\IteratorAggregate::class, $collection);
$this->assertCount(2, $collection);
$this->assertInstanceOf(\ArrayIterator::class, $collection->getIterator());
}
}
65 changes: 65 additions & 0 deletions tests/Metadata/Resource/ResourceMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\tests\Metadata\Resource;

use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ResourceMetadataTest extends \PHPUnit_Framework_TestCase
{
public function testValueObject()
{
$metadata = new ResourceMetadata('shortName', 'desc', 'http://example.com/foo', ['iop1' => ['foo' => 'a'], 'iop2' => ['bar' => 'b']], ['cop1' => ['foo' => 'c'], 'cop2' => ['bar' => 'd']], ['baz' => 'bar']);
$this->assertEquals('shortName', $metadata->getShortName());
$this->assertEquals('desc', $metadata->getDescription());
$this->assertEquals('http://example.com/foo', $metadata->getIri());
$this->assertEquals(['iop1' => ['foo' => 'a'], 'iop2' => ['bar' => 'b']], $metadata->getItemOperations());
$this->assertEquals('a', $metadata->getItemOperationAttribute('iop1', 'foo', 'z', false));
$this->assertEquals('bar', $metadata->getItemOperationAttribute('iop1', 'baz', 'z', true));
$this->assertEquals('z', $metadata->getItemOperationAttribute('iop1', 'notExist', 'z', true));
$this->assertEquals('z', $metadata->getItemOperationAttribute('notExist', 'notExist', 'z', true));
$this->assertEquals(['cop1' => ['foo' => 'c'], 'cop2' => ['bar' => 'd']], $metadata->getCollectionOperations());
$this->assertEquals('c', $metadata->getCollectionOperationAttribute('cop1', 'foo', 'z', false));
$this->assertEquals('bar', $metadata->getCollectionOperationAttribute('cop1', 'baz', 'z', true));
$this->assertEquals('z', $metadata->getCollectionOperationAttribute('cop1', 'notExist', 'z', true));
$this->assertEquals('z', $metadata->getCollectionOperationAttribute('notExist', 'notExist', 'z', true));
$this->assertEquals(['baz' => 'bar'], $metadata->getAttributes());
$this->assertEquals('bar', $metadata->getAttribute('baz'));
$this->assertEquals('z', $metadata->getAttribute('notExist', 'z'));

$newMetadata = $metadata->withShortName('name');
$this->assertFalse($metadata === $newMetadata);
$this->assertEquals('name', $newMetadata->getShortName());

$newMetadata = $metadata->withDescription('description');
$this->assertFalse($metadata === $newMetadata);
$this->assertEquals('description', $newMetadata->getDescription());

$newMetadata = $metadata->withIri('foo:bar');
$this->assertFalse($metadata === $newMetadata);
$this->assertEquals('foo:bar', $newMetadata->getIri());

$newMetadata = $metadata->withItemOperations(['a' => ['b' => 'c']]);
$this->assertFalse($metadata === $newMetadata);
$this->assertEquals(['a' => ['b' => 'c']], $newMetadata->getItemOperations());

$newMetadata = $metadata->withCollectionOperations(['a' => ['b' => 'c']]);
$this->assertFalse($metadata === $newMetadata);
$this->assertEquals(['a' => ['b' => 'c']], $newMetadata->getCollectionOperations());

$newMetadata = $metadata->withAttributes(['a' => ['b' => 'c']]);
$this->assertFalse($metadata === $newMetadata);
$this->assertEquals(['a' => ['b' => 'c']], $newMetadata->getAttributes());
}
}
29 changes: 29 additions & 0 deletions tests/Metadata/Resource/ResourceNameCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\tests\Metadata\Resource;

use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ResourceNameCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testValueObject()
{
$collection = new ResourceNameCollection(['foo', 'bar']);
$this->assertInstanceOf(\Countable::class, $collection);
$this->assertInstanceOf(\IteratorAggregate::class, $collection);
$this->assertCount(2, $collection);
$this->assertInstanceOf(\ArrayIterator::class, $collection->getIterator());
}
}

0 comments on commit 2b6dd3c

Please sign in to comment.