Skip to content

Commit

Permalink
Added a method for isntantiating models. Small change to Util tests
Browse files Browse the repository at this point in the history
  • Loading branch information
runz0rd committed Oct 1, 2016
1 parent c8341ac commit f3b2c0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/Common/ModelReflection/ModelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions tests/Common/ModelReflection/ModelClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down
10 changes: 5 additions & 5 deletions tests/Common/Util/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit f3b2c0b

Please sign in to comment.