-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/interface-mapping'
Showing
8 changed files
with
177 additions
and
11 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
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
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
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,8 @@ | ||
<?php | ||
|
||
namespace AutoMapperPlus\Test\Models\Interfaces; | ||
|
||
class DestinationImplementation implements DestinationInterface | ||
{ | ||
public $name; | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace AutoMapperPlus\Test\Models\Interfaces; | ||
|
||
interface DestinationInterface | ||
{ | ||
|
||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace AutoMapperPlus\Test\Models\Interfaces; | ||
|
||
class SourceImplementation implements SourceInterface | ||
{ | ||
private $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace AutoMapperPlus\Test\Models\Interfaces; | ||
|
||
/** | ||
* Interface SourceInterface | ||
* | ||
* @package AutoMapperPlus\Test\Models\Interfaces | ||
*/ | ||
interface SourceInterface | ||
{ | ||
public function getName(): string; | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace AutoMapperPlus\Test\Scenarios; | ||
|
||
use AutoMapperPlus\AutoMapper; | ||
use AutoMapperPlus\Configuration\AutoMapperConfig; | ||
use AutoMapperPlus\Exception\AutoMapperPlusException; | ||
use AutoMapperPlus\Test\Models\Interfaces\DestinationImplementation; | ||
use AutoMapperPlus\Test\Models\Interfaces\DestinationInterface; | ||
use AutoMapperPlus\Test\Models\Interfaces\SourceImplementation; | ||
use AutoMapperPlus\Test\Models\Interfaces\SourceInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InterfacesTest extends TestCase | ||
{ | ||
public function testItMapsFromAnInterface() | ||
{ | ||
$config = new AutoMapperConfig(); | ||
$config->registerMapping(SourceInterface::class, DestinationImplementation::class); | ||
$mapper = new AutoMapper($config); | ||
$source = new SourceImplementation('a name'); | ||
|
||
$result = $mapper->map($source, DestinationImplementation::class); | ||
|
||
$this->assertEquals('a name', $result->name); | ||
} | ||
|
||
public function testItDoesntAllowMappingToAnInterface() | ||
{ | ||
$this->expectException(AutoMapperPlusException::class); | ||
|
||
$config = new AutoMapperConfig(); | ||
$config->registerMapping(SourceImplementation::class, DestinationInterface::class) | ||
->dontSkipConstructor(); | ||
$mapper = new AutoMapper($config); | ||
$source = new SourceImplementation('a name'); | ||
|
||
$result = $mapper->map($source, DestinationInterface::class); | ||
|
||
$this->assertEquals('a name', $result->name); | ||
} | ||
|
||
public function testMappingToAnInterfaceIsAllowedForMapToObject() | ||
{ | ||
$config = new AutoMapperConfig(); | ||
$config->registerMapping(SourceImplementation::class, DestinationInterface::class); | ||
$mapper = new AutoMapper($config); | ||
$source = new SourceImplementation('a name'); | ||
$destination = new DestinationImplementation(); | ||
$result = $mapper->mapToObject($source, $destination); | ||
|
||
$this->assertEquals('a name', $result->name); | ||
} | ||
} |