Skip to content

Commit

Permalink
#30 fix repository by readding classes and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1stthomas committed Mar 10, 2022
1 parent db401d9 commit 9531faf
Show file tree
Hide file tree
Showing 11 changed files with 938 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/Models/AbstractLoadableModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Ht7\Base\Models;

use \Ht7\Base\Models\Loadable;
use \Ht7\Base\Utility\Traits\CanAddByPropertyName;

/**
* Base model.
*
* @author Thomas Pluess
* @version 0.0.1
* @since 0.0.1
*/
abstract class AbstractLoadableModel implements Loadable
{

use CanAddByPropertyName;

public function __construct(array $data = [])
{
$this->load($data);
}

public function load(array $data)
{
foreach ($data as $name => $value) {
$this->addByPropertyName($name, $value);
}
}

}
52 changes: 52 additions & 0 deletions src/Models/AbstractTransLoadableModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Ht7\Base\Models;

use \Ht7\Base\Models\TransLoadable;
use \Ht7\Base\Utility\Traits\CanAddByPropertyName;

/**
* Base model.
*
* @author Thomas Pluess
* @version 0.0.1
* @since 0.0.1
*/
abstract class AbstractTransLoadableModel implements TransLoadable
{

use CanAddByPropertyName;

protected $transformations;

public function __construct(array $data = [], array $transformations = [])
{
$this->setTransformations($transformations);

$this->load($data);
}

public function getTransformations()
{
return $this->transformations;
}

public function load(array $data)
{
$transformations = $this->getTransformations();

foreach ($data as $name => $value) {
if (array_key_exists($name, $transformations)) {
$name = $transformations[$name];
}

$this->addByPropertyName($name, $value);
}
}

public function setTransformations(array $transformations)
{
$this->transformations = $transformations;
}

}
36 changes: 36 additions & 0 deletions src/Models/Loadable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Ht7\Base\Models;

/**
* This interface describes a model, which is loadable with an array of property
* name/property value pairs.
*
* @author Thomas Pluess
* @version 0.0.1
* @since 0.0.1
*/
interface Loadable
{

/**
* Add a property to the present model by the property name.
*
* This method makes the first letter upper case and adds "set" to the
* beginning. Then the method is called by the composed method name, submitting
* the value as parameter.
*
* @param string $name The property name.
* @param mixed $value The new value of the property.
*/
public function addByPropertyName(string $name, $value);

/**
* Load an array of properties into the present model.
*
* @param array $data Assoc array of property names as array keys
* and the related property values as array
* values.
*/
public function load(array $data);
}
37 changes: 37 additions & 0 deletions src/Models/TransLoadable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Ht7\Base\Models;

use \Ht7\Base\Models\Loadable;

/**
* This interface describes a model, which is loadable with an array of property
* name/property value pairs.
*
* Additionally this model lets you define transformations, where e.g. short forms
* of property names can be transformed into the original ones.
*
* @author Thomas Pluess
* @version 0.0.1
* @since 0.0.1
*/
interface TransLoadable extends Loadable
{

/**
* Get the transformations.
*
* @return array Assoc array with the name to transform
* as key and the name to use as values.
*/
public function getTransformations();

/**
* Set the transformations.
*
* @param array $transformations Assoc array with the name to transform
* as key and the name to use as values.
* @return void
*/
public function setTransformations(array $transformations);
}
170 changes: 170 additions & 0 deletions tests/Unit/ContainerLwTest_1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace Ht7\Base\Tests;

use \Ht7\Base\ContainerLw;
use \Ht7\Base\Exceptions\ContainerResolvingException;
use \Ht7\Base\Exceptions\EntryNotFoundException;
use \PHPUnit\Framework\TestCase;

class ContainerLwTest extends TestCase
{
public function testConstruct()
{
$className = ContainerLw::class;

$mock = $this->getMockBuilder($className)
->setMethods(['bind'])
->disableOriginalConstructor()
->getMock();

$reflectedClass = new \ReflectionClass($className);
$propBindings = $reflectedClass->getProperty('bindings');
$propBindings->setAccessible(true);
$propInstances = $reflectedClass->getProperty('instances');
$propInstances->setAccessible(true);

$this->assertNull($propBindings->getValue($mock));
$this->assertNull($propInstances->getValue($mock));

$constructor = $reflectedClass->getConstructor();
$constructor->invoke($mock);

$this->assertIsArray($propBindings->getValue($mock));
$this->assertEmpty($propBindings->getValue($mock));
$this->assertIsArray($propInstances->getValue($mock));
$this->assertEmpty($propInstances->getValue($mock));
}
public function testGetInstance()
{
$this->assertInstanceOf(ContainerLw::class, ContainerLw::getInstance());
}
public function testGetInstanceSingleton()
{
$this->assertSame(ContainerLw::getInstance(), ContainerLw::getInstance());
}
public function testBind()
{
$className = ContainerLw::class;

$mock = $this->getMockBuilder($className)
->setMethods(['bound'])
->disableOriginalConstructor()
->getMock();

$reflectedClass = new \ReflectionClass($className);
$prop = $reflectedClass->getProperty('bindings');
$prop->setAccessible(true);
$prop->setValue($mock, []);

$mock->bind('ht7/test');

$this->assertArrayHasKey('ht7/test', $prop->getValue($mock));
$this->assertNull($prop->getValue($mock)['ht7/test']);

$mock->bind('ht7/test/exception', \InvalidArgumentException::class);

$this->assertArrayHasKey('ht7/test/exception', $prop->getValue($mock));
$this->assertEquals(\InvalidArgumentException::class, $prop->getValue($mock)['ht7/test/exception']);
}
public function testBond()
{
$className = ContainerLw::class;

$bindings = ['ht7/test' => null];

$mock = $this->getMockBuilder($className)
->setMethods(['getBindings'])
->disableOriginalConstructor()
->getMock();
$mock->expects($this->exactly(3))
->method('getBindings')
->willReturn($bindings);

$this->assertIsBool($mock->bound('ht7/test'));
$this->assertTrue($mock->bound('ht7/test'));
$this->assertFalse($mock->bound('ht7/test1'));
}
// public function testGet()
// {
// $className = ContainerLw::class;
//
// $bindings = ['ht7/test' => 'expected binding'];
//
// $mock = $this->getMockBuilder($className)
// ->setMethods(['getBindings'])
// ->disableOriginalConstructor()
// ->getMock();
// $mock->expects($this->exactly(1))
// ->method('getBindings')
// ->willReturn($bindings);
//
// $this->assertEquals($bindings['ht7/test'], $mock->get('ht7/test'));
// }

public function testGet()
{
$className = ContainerLw::class;

$bindings = ['ht7/test' => 'expected binding'];

$mock = $this->getMockBuilder($className)
->setMethods(['resolve'])
->disableOriginalConstructor()
->getMock();
$mock->expects($this->exactly(1))
->method('resolve')
->willReturn($bindings['ht7/test']);

$this->assertEquals($bindings['ht7/test'], $mock->get('ht7/test'));
}
// public function testGetExceptionContainer()
// {
// $className = ContainerLw::class;
//
// $excBinding = [
// 'ht7/exc' => function() {
// throw new \Exception();
// }
// ];
// $errBinding = ['exc/container/psr/container' => ContainerResolvingException::class];
//
// $mock = $this->getMockBuilder($className)
// ->setMethods(['has', 'getBindings'])
// ->disableOriginalConstructor()
// ->getMock();
// $mock->expects($this->exactly(1))
// ->method('has')
// ->willReturn(true);
// $mock->expects($this->exactly(2))
// ->method('getBindings')
//// ->will($this->returnCallback(function() {
// ->willReturnOnConsecutiveCalls($excBinding, $errBinding);
//// ->willReturnOnConsecutiveCalls($excBinding, $errBinding);
//// ->willReturn($excBinding, $errBinding);
//// ->will($this->returnValueMap([$bindings,]));
//// ->willReturn($bindings);
//
// $this->expectException(ContainerResolvingException::class);
//
// $mock->get('ht7/exc');
// }

public function testGetExceptionEntryNotFound()
{
$className = ContainerLw::class;

$mock = $this->getMockBuilder($className)
->setMethods(['getBindings'])
->disableOriginalConstructor()
->getMock();
$mock->expects($this->exactly(2))
->method('getBindings')
->willReturn([], ['exc/container/psr/notfound' => EntryNotFoundException::class]);

$this->expectException(EntryNotFoundException::class);

$mock->get('ht7/not_found');
}
// public function testGetBindings
}
18 changes: 18 additions & 0 deletions tests/Unit/DummyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Ht7\Base\Tests\Unit;

use \Ht7\Base\Dummy;
use \PHPUnit\Framework\TestCase;

class DummyTest extends TestCase
{

public function testGetDummy()
{
$dummy = new Dummy();

$this->assertTrue($dummy->getDummy());
}

}
41 changes: 41 additions & 0 deletions tests/Unit/EnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Ht7\Base\Tests;

use \InvalidArgumentException;
use \PHPUnit\Framework\TestCase;
use \Ht7\Base\Exceptions\UndefinedConstantException;
use \Ht7\Base\Tests\Implementations\Unit\EnumImplementation;

class EnumTest extends TestCase
{
private $object;

protected function setUp(): void
{
$this->object = new EnumImplementation();
}
/**
* Test to get a defined constant by comparing the expected value. An undefined
* constant will throw an exception. This case is tested by this method too.
*/
public function testGetConstant()
{
$this->assertEquals('test 1', EnumImplementation::getConstant('TEST_1'));

$this->expectException(UndefinedConstantException::class);

EnumImplementation::getConstant('test_11111');
}
/**
* Test setting an undefined property on the enum, which should throw an
* exception.
* This functionallity is implemented by a trait.
*/
public function testSet()
{
$this->expectException(InvalidArgumentException::class);

$this->object->test = 'Should throw an exception.';
}
}
Loading

0 comments on commit 9531faf

Please sign in to comment.