-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typecasts list merging; add Defaults registry (#65)
- Loading branch information
Showing
13 changed files
with
269 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema; | ||
|
||
use Cycle\ORM\Mapper\Mapper; | ||
use Cycle\ORM\SchemaInterface; | ||
use Cycle\ORM\Select\Repository; | ||
use Cycle\ORM\Select\Source; | ||
|
||
final class Defaults implements \ArrayAccess | ||
{ | ||
/** | ||
* @param array<int, mixed> $defaults | ||
*/ | ||
public function __construct( | ||
private array $defaults = [ | ||
SchemaInterface::MAPPER => Mapper::class, | ||
SchemaInterface::REPOSITORY => Repository::class, | ||
SchemaInterface::SOURCE => Source::class, | ||
SchemaInterface::SCOPE => null, | ||
SchemaInterface::TYPECAST_HANDLER => null, | ||
] | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<int, mixed> $defaults | ||
*/ | ||
public function merge(array $defaults): self | ||
{ | ||
$this->defaults = $defaults + $this->defaults; | ||
|
||
return $this; | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return isset($this->defaults[$offset]); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
return $this->defaults[$offset]; | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
$this->defaults[$offset] = $value; | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
unset($this->defaults[$offset]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Schema\Tests; | ||
|
||
use Cycle\ORM\Mapper\Mapper; | ||
use Cycle\ORM\SchemaInterface; | ||
use Cycle\ORM\Select\Repository; | ||
use Cycle\ORM\Select\Source; | ||
use Cycle\Schema\Defaults; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DefaultsTest extends TestCase | ||
{ | ||
public function testDefaultValues(): void | ||
{ | ||
$defaults = new Defaults(); | ||
|
||
$this->assertSame(Mapper::class, $defaults[SchemaInterface::MAPPER]); | ||
$this->assertSame(Repository::class, $defaults[SchemaInterface::REPOSITORY]); | ||
$this->assertSame(Source::class, $defaults[SchemaInterface::SOURCE]); | ||
$this->assertNull($defaults[SchemaInterface::SCOPE]); | ||
$this->assertNull($defaults[SchemaInterface::TYPECAST_HANDLER]); | ||
} | ||
|
||
/** | ||
* @dataProvider mergeDataProvider | ||
*/ | ||
public function testMerge(array $expected, array $values): void | ||
{ | ||
$defaults = new Defaults(); | ||
$defaults->merge($values); | ||
|
||
$ref = new \ReflectionProperty($defaults, 'defaults'); | ||
$ref->setAccessible(true); | ||
|
||
$this->assertEquals($expected, $ref->getValue($defaults)); | ||
} | ||
|
||
public function testOffsetExists(): void | ||
{ | ||
$defaults = new Defaults(); | ||
|
||
$this->assertTrue($defaults->offsetExists(SchemaInterface::MAPPER)); | ||
$this->assertFalse($defaults->offsetExists('foo')); | ||
} | ||
|
||
public function testOffsetGet(): void | ||
{ | ||
$defaults = new Defaults(); | ||
|
||
$this->assertSame(Mapper::class, $defaults->offsetGet(SchemaInterface::MAPPER)); | ||
} | ||
|
||
public function testOffsetSet(): void | ||
{ | ||
$defaults = new Defaults(); | ||
$defaults->offsetSet('foo', 'bar'); | ||
|
||
$this->assertSame('bar', $defaults->offsetGet('foo')); | ||
} | ||
|
||
public function testOffsetUnset(): void | ||
{ | ||
$defaults = new Defaults(); | ||
|
||
$this->assertTrue($defaults->offsetExists(SchemaInterface::MAPPER)); | ||
$defaults->offsetUnset(SchemaInterface::MAPPER); | ||
$this->assertFalse($defaults->offsetExists(SchemaInterface::MAPPER)); | ||
} | ||
|
||
public static function mergeDataProvider(): \Traversable | ||
{ | ||
yield [ | ||
[ | ||
SchemaInterface::MAPPER => Mapper::class, | ||
SchemaInterface::REPOSITORY => Repository::class, | ||
SchemaInterface::SOURCE => Source::class, | ||
SchemaInterface::SCOPE => null, | ||
SchemaInterface::TYPECAST_HANDLER => null, | ||
], | ||
[], | ||
]; | ||
yield [ | ||
[ | ||
SchemaInterface::MAPPER => Mapper::class, | ||
SchemaInterface::REPOSITORY => Repository::class, | ||
SchemaInterface::SOURCE => Source::class, | ||
SchemaInterface::SCOPE => null, | ||
SchemaInterface::TYPECAST_HANDLER => null, | ||
'foo' => 'bar', | ||
], | ||
[ | ||
'foo' => 'bar', | ||
], | ||
]; | ||
yield [ | ||
[ | ||
SchemaInterface::MAPPER => Mapper::class, | ||
SchemaInterface::REPOSITORY => Repository::class, | ||
SchemaInterface::SOURCE => Source::class, | ||
SchemaInterface::SCOPE => null, | ||
SchemaInterface::TYPECAST_HANDLER => 'foo', | ||
], | ||
[ | ||
SchemaInterface::TYPECAST_HANDLER => 'foo', | ||
], | ||
]; | ||
yield [ | ||
[ | ||
SchemaInterface::MAPPER => null, | ||
SchemaInterface::REPOSITORY => Repository::class, | ||
SchemaInterface::SOURCE => Source::class, | ||
SchemaInterface::SCOPE => null, | ||
SchemaInterface::TYPECAST_HANDLER => null, | ||
], | ||
[ | ||
SchemaInterface::MAPPER => null, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.