Skip to content

Commit

Permalink
model: enhanced error message for MetadataStorage, when called to ear…
Browse files Browse the repository at this point in the history
…ly. [closes #102]
  • Loading branch information
hrach committed Aug 21, 2015
1 parent ee39017 commit 7920016
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Model/MetadataStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Nextras\Orm\Entity\Reflection\EntityMetadata;
use Nextras\Orm\Entity\Reflection\MetadataValidator;
use Nextras\Orm\InvalidArgumentException;
use Nextras\Orm\InvalidStateException;


class MetadataStorage extends Object
Expand All @@ -26,6 +27,9 @@ class MetadataStorage extends Object
public static function get($className)
{
if (!isset(static::$metadata[$className])) {
if (static::$metadata === NULL) {
throw new InvalidStateException("MetadataStorage::get() called too early. You have to instantiate your model first.");
}
throw new InvalidArgumentException("Entity metadata for '{$className}' does not exist.");
}
return static::$metadata[$className];
Expand Down
46 changes: 46 additions & 0 deletions tests/cases/unit/Model/MetadataStorageTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* @testCase
*/

namespace NextrasTests\Orm\Model;

use Mockery;
use Nextras\Orm\Model\MetadataStorage;
use Nextras\Orm\Repository\IdentityMap;
use NextrasTests\Orm\TestCase;
use Tester\Assert;

$dic = require_once __DIR__ . '/../../../bootstrap.php';


class MetadataStorageTest extends TestCase
{
protected function setUp()
{
// do not call default setup which instantiate ORM model
}


public function testExceptions()
{
Assert::throws(function() {
MetadataStorage::get('NextrasTests\Orm\Book');
}, 'Nextras\Orm\InvalidStateException');

parent::setUp();

$metadata = MetadataStorage::get('NextrasTests\Orm\Book');
Assert::type('Nextras\Orm\Entity\Reflection\EntityMetadata', $metadata);

Assert::throws(function() {
MetadataStorage::get('NextrasTests\Orm\InvalidEntityName');
}, 'Nextras\Orm\InvalidArgumentException');
}

}


$test = new MetadataStorageTest($dic);
$test->run();

0 comments on commit 7920016

Please sign in to comment.