104 lines
3.0 KiB
PHP
104 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* Copyright 2019 Kacper Donat
|
|
*
|
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
|
*
|
|
* Full license available in separate LICENSE file
|
|
*/
|
|
|
|
class PropertiesObject {
|
|
private $getter;
|
|
private $magic = [];
|
|
public $field = 'value';
|
|
|
|
public function __construct($field, $getter, array $magic = [])
|
|
{
|
|
$this->field = $field;
|
|
$this->getter = $getter;
|
|
$this->magic = $magic;
|
|
}
|
|
|
|
public function getValue()
|
|
{
|
|
return $this->getter;
|
|
}
|
|
|
|
public function __get($name)
|
|
{
|
|
return $this->magic[$name];
|
|
}
|
|
|
|
public function __toString()
|
|
{
|
|
return __CLASS__;
|
|
}
|
|
}
|
|
|
|
class ObjectTransformsTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
use RandomDataTrait;
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('randomDataProvider')]
|
|
public function testFieldTransform($data = null)
|
|
{
|
|
$object = new PropertiesObject($data, null, []);
|
|
$transform = \Kadet\Functional\Transforms\field('field');
|
|
|
|
$this->assertSame($transform($object), $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('randomDataProvider')]
|
|
public function testFieldTransformMagic($data = null)
|
|
{
|
|
$object = new PropertiesObject(false, false, ['magic' => $data]);
|
|
$transform = \Kadet\Functional\Transforms\property('magic');
|
|
|
|
$this->assertSame($transform($object), $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('randomDataProvider')]
|
|
public function testPropertyTransformGetter($data = null)
|
|
{
|
|
$object = new PropertiesObject(false, $data, []);
|
|
$transform = \Kadet\Functional\Transforms\property('value');
|
|
|
|
$this->assertSame($transform($object), $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('randomDataProvider')]
|
|
public function testGetterTransform($data = null)
|
|
{
|
|
$object = new PropertiesObject(false, $data, []);
|
|
$transform = \Kadet\Functional\Transforms\getter('value');
|
|
|
|
$this->assertSame($transform($object), $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('randomDataProvider')]
|
|
public function testPropertyTransformField($data = null)
|
|
{
|
|
$object = new PropertiesObject($data, false, []);
|
|
$transform = \Kadet\Functional\Transforms\property('field');
|
|
|
|
$this->assertSame($transform($object), $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('randomDataProvider')]
|
|
public function testPropertyTransformMagic($data = null)
|
|
{
|
|
$object = new PropertiesObject(false, false, ['magic' => $data]);
|
|
$transform = \Kadet\Functional\Transforms\property('magic');
|
|
|
|
$this->assertSame($transform($object), $data);
|
|
}
|
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('randomDataProvider')]
|
|
public function testMethodTransform($data = null)
|
|
{
|
|
$object = new PropertiesObject(false, false);
|
|
$transform = \Kadet\Functional\Transforms\method('__toString');
|
|
|
|
$this->assertSame($transform($object), PropertiesObject::class);
|
|
}
|
|
} |