Skip to content

Commit

Permalink
Merge pull request #291 from cristianoc72/issue-257
Browse files Browse the repository at this point in the history
Issue #257
  • Loading branch information
willdurand committed Aug 10, 2012
2 parents 77fa696 + 8a3a580 commit 338752a
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -31,3 +31,5 @@ tests/Fixtures/reverse/mysql/runtime-conf.xml

tests/Fixtures/reverse/pgsql/build/
tests/Fixtures/reverse/pgsql/runtime-conf.xml

.idea
2 changes: 2 additions & 0 deletions src/Propel/Generator/Builder/Om/ObjectBuilder.php
Expand Up @@ -184,6 +184,8 @@ protected function getDefaultValueString(Column $column)
$defaultValue = var_export($val, true);
} elseif ($column->isPhpObjectType()) {
$defaultValue = 'new '.$column->getPhpType().'(' . var_export($val, true) . ')';
} elseif ($column->isPhpArrayType()) {
$defaultValue = var_export($val, true);
} else {
throw new EngineException("Cannot get default value string for " . $column->getFullyQualifiedName());
}
Expand Down
9 changes: 9 additions & 0 deletions src/Propel/Generator/Model/Column.php
Expand Up @@ -1046,6 +1046,15 @@ public function isTemporalType()
return PropelTypes::isTemporalType($this->getType());
}

/**
* Utility method to know whether column is an array column.
* @return boolean
*/
public function isPhpArrayType()
{
return PropelTypes::isPhpArrayType($this->getType());
}

/**
* Returns whether or not this column is an ENUM column.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Propel/Generator/Model/Domain.php
Expand Up @@ -281,6 +281,10 @@ public function getPhpDefaultValue()
return $this->booleanValue($this->defaultValue->getValue());
}

if (PropelTypes::PHP_ARRAY === $this->mappingType) {
return $this->getDefaultValueForArray($this->defaultValue->getValue());
}

return $this->defaultValue->getValue();
}

Expand Down
21 changes: 21 additions & 0 deletions src/Propel/Generator/Model/MappingModel.php
Expand Up @@ -101,6 +101,27 @@ protected function booleanValue($value)
return in_array(strtolower($value), array('true', 't', 'y', 'yes'), true);
}

protected function getDefaultValueForArray($stringValue)
{
$stringValue = trim($stringValue);

if (empty($stringValue)) {
return null;
}

$values = array();
foreach (explode(',', $stringValue) as $v) {
$values[] = trim($v);
}

$value = implode($values, ' | ');
if (empty($value) || ' | ' === $value) {
return null;
}

return sprintf('||%s||', $value);
}

/**
* Appends DOM elements to represent this object in XML.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Propel/Generator/Model/PropelTypes.php
Expand Up @@ -364,4 +364,15 @@ public static function isPhpObjectType($phpType)
{
return !self::isPhpPrimitiveType($phpType) && !in_array($phpType, array('resource', 'array'));
}

/**
* Convenience method to indicate whether a passed-in PHP type is an array.
*
* @param string $phpType The PHP type to check
* @return boolean
*/
public static function isPhpArrayType($phpType)
{
return strtoupper($phpType) === self::PHP_ARRAY;
}
}
27 changes: 27 additions & 0 deletions src/Propel/Generator/Platform/DefaultPlatform.php
Expand Up @@ -381,6 +381,13 @@ public function getColumnDefaultValueDDL(Column $col)
$default .= $this->getBooleanString($defaultValue->getValue());
} elseif ($col->getType() == PropelTypes::ENUM) {
$default .= array_search($defaultValue->getValue(), $col->getValueSet());
} elseif ($col->isPhpArrayType()) {
$value = $this->getPhpArrayString($defaultValue->getValue());
if (null === $value) {
$default = '';
} else {
$default .= $value;
}
} else {
$default .= $defaultValue->getValue();
}
Expand Down Expand Up @@ -1163,6 +1170,26 @@ public function getBooleanString($b)
return '0';
}

public function getPhpArrayString($stringValue)
{
$stringValue = trim($stringValue);
if (empty($stringValue)) {
return null;
}

$values = array();
foreach (explode(',', $stringValue) as $v) {
$values[] = trim($v);
}

$value = implode($values, ' | ');
if (empty($value) || ' | ' === $value) {
return null;
}

return $this->quote(sprintf('||%s||', $value));
}

/**
* Gets the preferred timestamp formatter for setting date/time values.
* @return string
Expand Down
Expand Up @@ -35,11 +35,15 @@ public function setUp()
<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
<column name="tags" type="ARRAY" />
<column name="value_set" type="ARRAY" />
<column name="defaults" type="ARRAY" defaultValue="FOO" />
<column name="multiple_defaults" type="ARRAY" defaultValue="FOO, BAR,BAZ" />
</table>
</database>
EOF;
QuickBuilder::buildSchema($schema);
}

ComplexColumnTypeEntity2Peer::doDeleteAll();
}

public function testActiveRecordMethods()
Expand All @@ -63,6 +67,56 @@ public function testGetterDefaultValue()
$this->assertEquals(array(), $e->getTags(), 'array columns return an empty array by default');
}

public function testGetterDefaultValueWithData()
{
$e = new ComplexColumnTypeEntity2();
$this->assertEquals(array('FOO'), $e->getDefaults());
}

public function testGetterDefaultValueWithMultipleData()
{
$e = new ComplexColumnTypeEntity2();
$this->assertEquals(array('FOO', 'BAR', 'BAZ'), $e->getMultipleDefaults());
}

public function testAdderAddsNewValueToExistingData()
{
$e = new ComplexColumnTypeEntity2();
$this->assertEquals(array('FOO'), $e->getDefaults());
$e->addDefault('bar');
$this->assertEquals(array('FOO', 'bar'), $e->getDefaults());
}

public function testAdderAddsNewValueToMultipleExistingData()
{
$e = new ComplexColumnTypeEntity2();
$this->assertEquals(array('FOO', 'BAR', 'BAZ'), $e->getMultipleDefaults());
$e->addMultipleDefault('bar');
$this->assertEquals(array('FOO', 'BAR', 'BAZ', 'bar'), $e->getMultipleDefaults());
}

public function testDefaultValuesAreWellPersisted()
{
$e = new ComplexColumnTypeEntity2();
$e->save();

ComplexColumnTypeEntity2Peer::clearInstancePool();
$e = ComplexColumnTypeEntity2Query::create()->findOne();

$this->assertEquals(array('FOO'), $e->getDefaults());
}

public function testMultipleDefaultValuesAreWellPersisted()
{
$e = new ComplexColumnTypeEntity2();
$e->save();

ComplexColumnTypeEntity2Peer::clearInstancePool();
$e = ComplexColumnTypeEntity2Query::create()->findOne();

$this->assertEquals(array('FOO', 'BAR', 'BAZ'), $e->getMultipleDefaults());
}

public function testSetterArrayValue()
{
$e = new ComplexColumnTypeEntity2();
Expand Down
10 changes: 10 additions & 0 deletions tests/Propel/Tests/Generator/Model/ColumnTest.php
Expand Up @@ -10,6 +10,7 @@
namespace Propel\Tests\Generator\Model;

use Propel\Generator\Model\Column;
use Propel\Generator\Model\PropelTypes;

/**
* Tests for package handling.
Expand Down Expand Up @@ -844,6 +845,15 @@ public function testHasPlatform()
$this->assertInstanceOf('Propel\Generator\Platform\PlatformInterface', $column->getPlatform());
}

public function testIsPhpArrayType()
{
$column = new Column();
$this->assertFalse($column->isPhpArrayType());

$column->setType(PropelTypes::PHP_ARRAY);
$this->assertTrue($column->isPhpArrayType());
}

public function testSetSize()
{
$domain = $this->getDomainMock();
Expand Down
58 changes: 58 additions & 0 deletions tests/Propel/Tests/Generator/Model/MappingModelTest.php
@@ -0,0 +1,58 @@
<?php

/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Propel\Tests\Generator\Model;

use Propel\Generator\Model\MappingModel;

/**
* @author William Durand <william.durand1@gmail.com>
*/
class MappingModelTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerForGetDefaultValueForArray
*/
public function testGetDefaultValueForArray($value, $expected)
{
$mappingModel = new TestableMappingModel();
$this->assertEquals($expected, $mappingModel->getDefaultValueForArray($value));
}

public static function providerForGetDefaultValueForArray()
{
return array(
array('', null),
array(null, null),
array('FOO', '||FOO||'),
array('FOO, BAR', '||FOO | BAR||'),
array('FOO , BAR', '||FOO | BAR||'),
array('FOO,BAR', '||FOO | BAR||'),
array(' ', null),
array(', ', null),
);
}
}

class TestableMappingModel extends MappingModel
{
public function getDefaultValueForArray($value)
{
return parent::getDefaultValueForArray($value);
}

public function appendXml(DOMNode $node)
{
}

protected function setupObject()
{
}
}

0 comments on commit 338752a

Please sign in to comment.