Skip to content

Commit

Permalink
Merge pull request propelorm#768 from rvitaliy/fix-typehinting
Browse files Browse the repository at this point in the history
fixed type-hinting in addDefaultMutator and added tests for it
  • Loading branch information
willdurand committed Nov 11, 2013
2 parents 15384ff + 7b073a2 commit 93f9fc8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion generator/lib/builder/om/PHP5ObjectBuilder.php
Expand Up @@ -1986,8 +1986,16 @@ protected function addDefaultMutator(&$script, Column $col)
// Perform type-casting to ensure that we can use type-sensitive
// checking in mutators.
if ($col->isPhpPrimitiveType()) {
if($col->isTextType()) {
$script .= "
if (\$v !== null) {";
} else {
$script .= "
if (\$v !== null && is_numeric(\$v)) {";
}


$script .= "
if (\$v !== null && is_numeric(\$v)) {
\$v = (" . $col->getPhpType() . ") \$v;
}
";
Expand Down
42 changes: 42 additions & 0 deletions test/testsuite/generator/builder/om/GeneratedObjectTest.php
Expand Up @@ -100,6 +100,48 @@ public function testDefaultValues()
$acct->setPassword("testpass");
$this->assertTrue($acct->isModified());
}


public function testTypeHintingValues()
{
$test_name = 'test name';
$a = new Author();
$a2 = new Author();
$a2->setFirstName($test_name);


$a->setAge(2);
$this->assertEquals(2, $a->getAge());
$this->assertTrue(is_int($a->getAge()));


$a->clear();
$a->setAge('2');
$this->assertEquals(2, $a->getAge());
$this->assertTrue(is_int($a->getAge()));


$a->clear();
$a->setAge('wrong integer');
$this->assertTrue(!is_int($a->getAge()));


$a->clear();
$a->setFirstName($test_name);
$this->assertEquals($test_name, $a->getFirstName());
$this->assertTrue(is_string($a->getFirstName()));


$a->clear();
$a->setFirstName($a2);
$this->assertTrue(is_string($a->getFirstName()));
$this->assertEquals($a->getFirstName(), (string)$a2);


$a->clear();
$a->setFirstName(true);
$this->assertTrue(is_string($a->getFirstName()));
}

/**
* Tests the use of default expressions and the reloadOnInsert and reloadOnUpdate attributes.
Expand Down

0 comments on commit 93f9fc8

Please sign in to comment.