Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultMapper: added $defaultEntityNamespace to constructor #113

Merged
merged 1 commit into from Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/LeanMapper/DefaultMapper.php
Expand Up @@ -21,14 +21,24 @@
class DefaultMapper implements IMapper
{

/** @var string */
protected $defaultEntityNamespace = 'Model\Entity';
/** @var string|null */
protected $defaultEntityNamespace;

/** @var string */
protected $relationshipTableGlue = '_';



/**
* @param string|null
*/
public function __construct($defaultEntityNamespace = 'Model\Entity')
{
$this->defaultEntityNamespace = $defaultEntityNamespace;
}



/**
* {@inheritdoc}
*/
Expand Down
64 changes: 64 additions & 0 deletions tests/LeanMapper/DefaultMapper.phpt
Expand Up @@ -30,3 +30,67 @@ Assert::equal('author_id', $mapper->getRelationshipColumn('book', 'author'));
Assert::equal('reviewer_id', $mapper->getRelationshipColumn('book', 'author', 'reviewer'));

Assert::equal('author', $mapper->getTableByRepositoryClass('Model\Repository\AuthorRepository'));

//////////

$mapper = new DefaultMapper('App\Entities');

Assert::equal('App\Entities\Author', $mapper->getEntityClass('author'));

Assert::equal('App\Entities\Book', $mapper->getEntityClass('book'));

//////////

$mapper = new DefaultMapper(null);

Assert::equal('Author', $mapper->getEntityClass('author'));

Assert::equal('Book', $mapper->getEntityClass('book'));

//////////

class MyMapper extends DefaultMapper
{
public function __construct(array $mapping)
{
parent::__construct();
// some stuff
}
}

$mapper = new MyMapper([]);

Assert::equal('Model\Entity\Author', $mapper->getEntityClass('author'));

Assert::equal('Model\Entity\Book', $mapper->getEntityClass('book'));

//////////

class MyMapper2 extends DefaultMapper
{
public function __construct()
{
$this->defaultEntityNamespace = 'MyMapper2';
}
}

$mapper = new MyMapper2();

Assert::equal('MyMapper2\Author', $mapper->getEntityClass('author'));

Assert::equal('MyMapper2\Book', $mapper->getEntityClass('book'));

//////////

// Old unsupported usage
//
// class MyMapper3 extends DefaultMapper
// {
// protected $defaultEntityNamespace = 'MyMapper3';
// }

// $mapper = new MyMapper3();

// Assert::equal('MyMapper3\Author', $mapper->getEntityClass('author'));

// Assert::equal('MyMapper3\Book', $mapper->getEntityClass('book'));
5 changes: 4 additions & 1 deletion tests/LeanMapper/Entity.singleTableInheritance.phpt
Expand Up @@ -10,7 +10,10 @@ require_once __DIR__ . '/../bootstrap.php';
class Mapper extends LeanMapper\DefaultMapper
{

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



Expand Down
5 changes: 4 additions & 1 deletion tests/LeanMapper/PrimaryEqualsForeign.phpt
Expand Up @@ -44,7 +44,10 @@ class AuthorContract extends Entity
class Mapper extends DefaultMapper
{

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



Expand Down
9 changes: 1 addition & 8 deletions tests/bootstrap.php
Expand Up @@ -32,18 +32,11 @@ class_alias('Tester\Assert', 'Assert');
exit(1);
}

class TestMapper extends DefaultMapper
{

protected $defaultEntityNamespace = null;

}

$connection = new Connection([
'driver' => 'sqlite3',
'database' => TEMP_DIR . '/library.sq3',
]);

$mapper = new TestMapper;
$mapper = new DefaultMapper(null);

$entityFactory = new DefaultEntityFactory;