78 lines
1.9 KiB
PHP
78 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pcm\MetadataBundle\Tests\Model;
|
|
|
|
use Pcm\MetadataBundle\Exception\MissingKeyException;
|
|
use Pcm\MetadataBundle\Model\Metadata;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class MetadataTest extends TestCase
|
|
{
|
|
private Metadata $metadata;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$array = []; // Needs to be a var as it's passed by reference
|
|
$this->metadata = new Metadata($array);
|
|
}
|
|
|
|
public function testGetReturnsSetValue(): void
|
|
{
|
|
$value = 'brad woz ere';
|
|
$this->metadata->set('key', $value);
|
|
$result = $this->metadata->get('key');
|
|
$this->assertSame($value, $result);
|
|
}
|
|
|
|
public function testGetThrowsWhenKeyNotSet(): void
|
|
{
|
|
$this->expectException(MissingKeyException::class);
|
|
$this->metadata->get('missing');
|
|
}
|
|
|
|
public function testValueIsOverwritten(): void
|
|
{
|
|
$key = 'key';
|
|
$valueA = 'meta';
|
|
$valueB = 'data';
|
|
|
|
$this->metadata->set($key, $valueA);
|
|
$result = $this->metadata->get($key);
|
|
$this->assertSame($valueA, $result);
|
|
|
|
$this->metadata->set($key, $valueB);
|
|
$result = $this->metadata->get($key);
|
|
$this->assertSame($valueB, $result);
|
|
}
|
|
|
|
public function testIsSetReturnsTrueWhenKeyIsSet(): void
|
|
{
|
|
$key = 'null';
|
|
$this->metadata->set($key, null);
|
|
$result = $this->metadata->isSet($key);
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testIsSetReturnsFalseWhenKeyIsNotSet(): void
|
|
{
|
|
$result = $this->metadata->isSet('missing');
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
public function testLoopOverMetadata(): void
|
|
{
|
|
$this->metadata->set("a", "A")->set("b", "B")->set("c", "C");
|
|
|
|
$result = [];
|
|
|
|
foreach ($this->metadata as $key => $value) {
|
|
$result[$key] = $value;
|
|
}
|
|
|
|
$this->assertSame(["a" => "A", "b" => "B", "c" => "C"], $result);
|
|
}
|
|
}
|
|
|