Skip to content

Commit

Permalink
AUniformHostTest, AUniformHost
Browse files Browse the repository at this point in the history
  • Loading branch information
chkt committed May 18, 2018
1 parent c9980b4 commit b483f5e
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
37 changes: 37 additions & 0 deletions source/common/assembly/AUniformHost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace eve\common\assembly;

use eve\common\access\IItemAccessor;
use eve\common\assembly\exception\InvalidKeyException;



abstract class AUniformHost
extends AAssemblyHost
{

private $_map;


public function __construct(IItemAccessor $map, array& $data = []) {
parent::__construct($data);

$this->_map = $map;
}

abstract protected function _produceFromMap(IItemAccessor $map, string $key);


final protected function _produceItem(string $key) {

if (!$this->_map->hasKey($key)) throw new InvalidKeyException($key);

return $this->_produceFromMap($this->_map, $key);
}


public function hasKey(string $key) : bool {
return parent::hasKey($key) || $this->_map->hasKey($key);
}
}
104 changes: 104 additions & 0 deletions test/common/assembly/AUniformHostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace test\common\assembly;

use PHPUnit\Framework\TestCase;
use eve\common\access\IItemAccessor;
use eve\common\assembly\AUniformHost;
use eve\common\assembly\exception\InvalidKeyException;



final class AUniformHostTest
extends TestCase
{

private function _mockItemAccessor(array $data = []) {
$access = $this
->getMockBuilder(IItemAccessor::class)
->getMock();

$access
->method('hasKey')
->with($this->isType('string'))
->willReturnCallback(function (string $key) use ($data) {
return array_key_exists($key, $data);
});

$access
->method('getItem')
->with($this->isType('string'))
->willReturnCallback(function(string $key) use ($data) {
return $data[$key];
});

return $access;
}

private function _mockHost(array& $data = [], IItemAccessor $map = null, callable $fn = null) {
if (is_null($map)) $map = $this->_mockItemAccessor();
if (is_null($fn)) $fn = function(string $key) {
return null;
};

$host = $this
->getMockBuilder(AUniformHost::class)
->setConstructorArgs([ $map, & $data ])
->setMethods([ '_produceFromMap' ])
->getMock();

$host
->method('_produceFromMap')
->with(
$this->isInstanceOf(IItemAccessor::class),
$this->isType('string')
)
->willReturnCallback($fn);

return $host;
}


public function testInheritance() {
$host = $this->_mockHost();

$this->assertInstanceOf(\eve\common\assembly\AAssemblyHost::class, $host);
}


public function testHasKey() {
$map = $this->_mockItemAccessor([ 'bar' => 2 ]);
$data = [ 'foo' => 1 ];
$host = $this->_mockHost($data, $map, function(IItemAccessor $map, string $key) {
$this->fail($key);
});

$this->assertTrue($host->hasKey('foo'));
$this->assertTrue($host->hasKey('bar'));
$this->assertArrayNotHasKey('bar', $data);
$this->assertFalse($host->hasKey('baz'));
$this->assertArrayNotHasKey('baz', $data);
}


public function testGetItem() {
$map = $this->_mockItemAccessor([ 'bar' => 2 ]);
$data = [ 'foo' => 1 ];
$host = $this->_mockHost($data, $map, function(IItemAccessor $map, string $key) {
return $map->getItem($key);
});

$this->assertEquals(1, $host->getItem('foo'));
$this->assertEquals(2, $host->getItem('bar'));
$this->assertArrayHasKey('bar', $data);
$this->assertEquals(2, $data['bar']);
}

public function testGetItem_invalid() {
$host = $this->_mockHost();

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

$host->getItem('foo');
}
}

0 comments on commit b483f5e

Please sign in to comment.