Skip to content

Commit

Permalink
Added a custom name support for adding DataMapper to DataMappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurimas Niekis committed Apr 21, 2016
1 parent 9d48825 commit aeec316
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/DataMappers.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public function setValidator($validator)
*
* @return $this
*/
public function addMapper(DataMapperInterface $dataMapper) : self
public function addMapper(DataMapperInterface $dataMapper, string $name = null) : self
{
$this->dataMappers[get_class($dataMapper)] = $dataMapper;
if (null === $name) {
$name = get_class($dataMapper);
}

$this->dataMappers[$name] = $dataMapper;

$dataMapper->setDataMappers($this);

Expand Down
19 changes: 19 additions & 0 deletions src/Tests/DataMappersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,23 @@ public function testNotExistingMapper()

$dataMappers->getMapper('demo');
}

public function testCustomName()
{
$dataMappers = new DataMappers();

$mapperMock = $this->getMockForAbstractClass('\Thruster\Component\DataMapper\DataMapperInterface');
$mapperName = 'demo';

$this->assertFalse($dataMappers->hasMapper($mapperName));

$dataMappers->addMapper($mapperMock, $mapperName);

$this->assertTrue($dataMappers->hasMapper($mapperName));

$wrappedMapper = $dataMappers->getMapper($mapperName);

$this->assertInstanceOf('\Thruster\Component\DataMapper\DataMapper', $wrappedMapper);
$this->assertEquals($mapperMock, $wrappedMapper->getDataMapper());
}
}

0 comments on commit aeec316

Please sign in to comment.