-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing the basic functionality of public interface for the repository
- Loading branch information
Showing
3 changed files
with
200 additions
and
0 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
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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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'); | ||
} | ||
} |