Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MappingFactory: validate property names
This complicates MappingFactory and adds dependence to weird place, but since this lib is aimed at practicality, I believe slightly ignoring invalid properties must not be allowed.
- Loading branch information
Showing
7 changed files
with
163 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Mikulas\OrmExt; | ||
|
||
|
||
class InvalidPropertyException extends \InvalidArgumentException implements Exception | ||
{ | ||
|
||
/** @var string */ | ||
private $propertyName; | ||
|
||
|
||
/** | ||
* @param string $propertyName | ||
* @param string $message | ||
* @param \Exception $previous | ||
* @internal | ||
*/ | ||
public function __construct($propertyName, $message, \Exception $previous = NULL) | ||
{ | ||
$this->propertyName = $propertyName; | ||
parent::__construct($message, NULL, $previous); | ||
} | ||
|
||
|
||
/** | ||
* @param string $propertyName | ||
* @param \Exception $previous | ||
* @return InvalidPropertyException | ||
*/ | ||
public static function createNonexistentProperty($propertyName, \Exception $previous = NULL) | ||
{ | ||
return new self($propertyName, "Property '$propertyName' does not exist", $previous); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,9 +1,5 @@ | ||
<?php | ||
|
||
/** | ||
* @testCase | ||
*/ | ||
|
||
namespace Mikulas\OrmExt\Tests; | ||
|
||
use Mikulas\OrmExt\Crypto; | ||
|
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,48 @@ | ||
<?php | ||
|
||
namespace Mikulas\OrmExt\Tests; | ||
|
||
use Mikulas\OrmExt\InvalidPropertyException; | ||
use Mikulas\OrmExt\MappingFactory; | ||
use Mockery; | ||
use Nextras\Orm\Entity\Reflection\EntityMetadata; | ||
use Nextras\Orm\InvalidArgumentException; | ||
use Nextras\Orm\Mapper\Dbal\StorageReflection\StorageReflection; | ||
use Tester\Assert; | ||
|
||
$dic = require_once __DIR__ . '/../../bootstrap.php'; | ||
|
||
|
||
class MappingFactoryTest extends TestCase | ||
{ | ||
|
||
public function testInvalidPropertyThrows() | ||
{ | ||
/** @var Mockery\MockInterface|StorageReflection $storageReflection */ | ||
$storageReflection = Mockery::mock(StorageReflection::class); | ||
$storageReflection->shouldReceive('convertEntityToStorageKey') | ||
->andReturnSelf(); | ||
$storageReflection->shouldReceive('addMapping') | ||
->andReturn(); | ||
|
||
/** @var Mockery\MockInterface|EntityMetadata $entityMetadata */ | ||
$entityMetadata = Mockery::mock(EntityMetadata::class); | ||
$entityMetadata->shouldReceive('getProperty') | ||
->andReturnUsing(function($prop) { | ||
if ($prop !== 'exists') { | ||
throw new InvalidArgumentException(); | ||
} | ||
}); | ||
|
||
$factory = new MappingFactory($storageReflection, $entityMetadata); | ||
|
||
Assert::exception(function() use ($factory) { | ||
$factory->addIntArrayMapping('unknown'); | ||
}, InvalidPropertyException::class); | ||
|
||
$factory->addIntArrayMapping('exists'); | ||
} | ||
|
||
} | ||
|
||
(new MappingFactoryTest($dic))->run(); |
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