Skip to content

Commit

Permalink
Testing the basic functionality of public interface for the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
teresko committed Feb 28, 2018
1 parent 3f847ba commit dfec5f8
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/mock/RepoEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Mock;

use Palladium\Contract\HasId;

class RepoEntity implements HasId
{
private $value;


public function __construct($value = 'default')
{
$this->value = $value;
}


public function getId()
{
return $this->value;
}


public function setId($value)
{
$this->value = $value;
}


public function setAction($action)
{
$this->value = $action;
}


public function getAction()
{
return $this->value;
}
}
38 changes: 38 additions & 0 deletions tests/mock/RepoMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Mock;

use Palladium\Component\DataMapper;

class RepoMapper extends DataMapper
{
private $value;

public function __construct($value = null)
{
$this->value = $value;
}

public function fetch(RepoEntity $entity)
{
$entity->setAction('loaded');
return $this->value;
}

public function store(RepoEntity $entity)
{
$entity->setAction('saved');
return $this->value;
}

public function remove(RepoEntity $entity)
{
$entity->setAction('deleted');
return $this->value;
}

public function exists(RepoEntity $entity)
{
return $this->value;
}
}
122 changes: 122 additions & 0 deletions tests/unit/Palladium/Repository/IdentityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Palladium\Repository;

use PHPUnit\Framework\TestCase;
use PDO;
use PDOStatement;
use Palladium\Entity;

/**
* @covers Palladium\Repository\Identity
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
final class IdentityTest extends TestCase
{
/**
* @test
*/
public function use_assigned_mapper_for_loading_entity()
{
$instance = new Identity(new \Mock\Factory([
\Mock\RepoMapper::class => new \Mock\RepoMapper,
]));
$instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class);

$item = new \Mock\RepoEntity;
$instance->load($item);

$this->assertSame('loaded', $item->getAction());
}


/**
* @test
*/
public function use_assigned_mapper_for_storing_entity()
{
$instance = new Identity(new \Mock\Factory([
\Mock\RepoMapper::class => new \Mock\RepoMapper,
]));
$instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class);

$item = new \Mock\RepoEntity;
$instance->save($item);

$this->assertSame('saved', $item->getAction());
}


/**
* @test
*/
public function use_assigned_mapper_for_deleting_entity()
{
$instance = new Identity(new \Mock\Factory([
\Mock\RepoMapper::class => new \Mock\RepoMapper,
]));
$instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class);

$item = new \Mock\RepoEntity;
$instance->delete($item);

$this->assertSame('deleted', $item->getAction());
}


/**
* @test
*/
public function use_assigned_mapper_for_checking_whether_entity_exists()
{
$instance = new Identity(new \Mock\Factory([
\Mock\RepoMapper::class => new \Mock\RepoMapper(true),
]));
$instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class);

$item = new \Mock\RepoEntity;
$this->assertSame(true, $instance->has($item));
}


/**
* @test
*/
public function deny_definition_of_fake_entity_in_repo()
{
$this->expectException(\RuntimeException::class);

$instance = new Identity(new \Mock\Factory([]));
$instance->define('FooBar', \Mock\RepoMapper::class);
}


/**
* @test
*/
public function deny_definition_of_fake_mapper_in_repo()
{
$this->expectException(\RuntimeException::class);

$instance = new Identity(new \Mock\Factory([]));
$instance->define(\Mock\RepoEntity::class, 'FooBar');
}


/**
* @test
*/
public function deny_override_with_undefined_mapper()
{
$this->expectException(\RuntimeException::class);

$instance = new Identity(new \Mock\Factory([
\Mock\RepoMapper::class => new \Mock\RepoMapper,
]));
$instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class);

$item = new \Mock\RepoEntity;
$instance->load($item, 'FooBar');
}
}

0 comments on commit dfec5f8

Please sign in to comment.