diff --git a/src/Common/ModelReflection/ModelClass.php b/src/Common/ModelReflection/ModelClass.php index 969e65b..861c3d6 100644 --- a/src/Common/ModelReflection/ModelClass.php +++ b/src/Common/ModelReflection/ModelClass.php @@ -106,4 +106,16 @@ public function getDocBlock() { return $this->docBlock; } + + public static function instantiate(string $modelClassName) { + try { + $reflectionClass = new \ReflectionClass($modelClassName); + $model = $reflectionClass->newInstanceWithoutConstructor(); + } + catch(\Exception $ex) { + throw new \InvalidArgumentException('Could not instantiate model ' . $modelClassName . '. ' . $ex->getMessage()); + } + + return $model; + } } \ No newline at end of file diff --git a/tests/Common/ModelReflection/ModelClassTest.php b/tests/Common/ModelReflection/ModelClassTest.php index d4b582d..3dd073f 100644 --- a/tests/Common/ModelReflection/ModelClassTest.php +++ b/tests/Common/ModelReflection/ModelClassTest.php @@ -82,6 +82,20 @@ public function testConstructFail($invalidModel) { $modelClass = new ModelClass($invalidModel); } + public function testInstantiate() { + $expected = new TestModel(); + $actual = ModelClass::instantiate('\TestModel'); + $this->assertEquals($expected, $actual); + } + + /** + * @expectedException Exception + */ + public function testInstantiateFail() { + $expected = new TestModel(); + $actual = ModelClass::instantiate('Asd\TestModel'); + } + public function invalidModels() { return [ [new stdClass()], diff --git a/tests/Common/Util/DirectoryTest.php b/tests/Common/Util/DirectoryTest.php index 3927fcf..b569835 100644 --- a/tests/Common/Util/DirectoryTest.php +++ b/tests/Common/Util/DirectoryTest.php @@ -10,12 +10,12 @@ class DirectoryTest extends PHPUnit_Framework_TestCase { public function testScan() { $expected = [ - __DIR__ . '\testDir\new\testFile1.asd', - __DIR__ . '\testDir\new\testFile2.asd', - __DIR__ . '\testDir\testFile1.asd', - __DIR__ . '\testDir\testFile2.asd' + __DIR__.DIRECTORY_SEPARATOR.'testDir'.DIRECTORY_SEPARATOR.'new'.DIRECTORY_SEPARATOR.'testFile1.asd', + __DIR__.DIRECTORY_SEPARATOR.'testDir'.DIRECTORY_SEPARATOR.'new'.DIRECTORY_SEPARATOR.'testFile2.asd', + __DIR__.DIRECTORY_SEPARATOR.'testDir'.DIRECTORY_SEPARATOR.'testFile1.asd', + __DIR__.DIRECTORY_SEPARATOR.'testDir'.DIRECTORY_SEPARATOR.'testFile2.asd', ]; - $actual = \Common\Util\Directory::scan(__DIR__ . '/testDir', 'asd'); + $actual = \Common\Util\Directory::scan(__DIR__.DIRECTORY_SEPARATOR.'testDir', 'asd'); $this->assertEquals($expected, $actual); } }