Permalink
Browse files

introduces normalizers in peer class

  • Loading branch information...
1 parent e579748 commit 4820cc87b80e32dca88888b259bc1df5f2e41411 @themouette themouette committed Mar 16, 2012
Showing with 198 additions and 0 deletions.
  1. +71 −0 src/ExtraPropertiesBehaviorPeerBuilderModifier.php
  2. +127 −0 tests/ExtraPropertiesBehaviorPeerBuilderTest.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * This file declare the ExtraPropertiesBehaviorPeerBuilderModifier class.
+ *
+ * @copyright (c) Carpe Hora SARL 2012
+ * @since 2012-03-16
+ * @license MIT License
+ */
+
+/**
+ * @author Julien Muetton <julien_muetton@carpe-hora.com>
+ * @package propel.generator.behavior.extra_properties
+ */
+class ExtraPropertiesBehaviorPeerBuilderModifier
+{
+ protected $behavior, $table;
+
+ public function __construct($behavior)
+ {
+ $this->behavior = $behavior;
+ $this->table = $behavior->getTable();
+ }
+ protected function getParameter($key)
+ {
+ return $this->behavior->getParameter($key);
+ }
+
+ public function shouldNormalize()
+ {
+ return 'true' === $this->getParameter('normalize');
+ }
+
+ public function staticMethods()
+ {
+ $script = <<<EOF
+/**
+ * Normalizes property name.
+ *
+ * @param String \$propertyName the property name to normalize.
+ * @param String the normalized property name
+ */
+static function normalizeExtraPropertyName(\$propertyName)
+{
+EOF;
+ if ($this->shouldNormalize()) {
+ $script .= <<<EOF
+ return strtoupper(\$propertyName);
+EOF;
+ } else {
+ $script .= <<<EOF
+ return \$propertyName;
+EOF;
+ }
+ $script .= <<<EOF
+}
+
+/**
+ * Normalizes property value.
+ *
+ * @param String \$propertyValue the property value to normalize.
+ * @param String the normalized property value
+ */
+static function normalizeExtraPropertyValue(\$propertyValue)
+{
+ return \$propertyValue;
+}
+EOF;
+
+ return $script;
+ }
+} // END OF ExtraPropertiesBehaviorPeerBuilderModifier
@@ -0,0 +1,127 @@
+<?php
+
+/*
+ * $Id: ExtraPropertiesBehaviorPeerBuilderTest.php 1460 2010-01-17 22:36:48Z francois $
+ * 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
+ */
+
+
+/**
+ * Test for ExtraPropertiesBehaviorPeerBuilder
+ *
+ * @author Julien Muetton
+ * @version $Revision$
+ * @package generator.behavior.extra_properties
+ */
+class ExtraPropertiesBehaviorPeerBuilderTest extends PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ if (!class_exists('ExtraPropertiesBehaviorTestNormalize')) {
+ $schema = <<<EOF
+<database name="extra_properties_behavior_test_normalize">
+ <table name="extra_properties_behavior_test_normalize">
+ <column name="id" type="INTEGER" primaryKey="true" autoincrement="true" />
+ <column name="name" type="VARCHAR" size="255" />
+ <behavior name="extra_properties">
+ <parameter name="normalize" value="true" />
+ </behavior>
+ </table>
+</database>
+EOF;
+ PropelQuickBuilder::buildSchema($schema);
+ }
+ if (!class_exists('ExtraPropertiesBehaviorTestDoNotNormalize')) {
+ $schema = <<<EOF
+<database name="extra_properties_behavior_test_do_not_normalize">
+ <table name="extra_properties_behavior_test_do_not_normalize">
+ <column name="id" type="INTEGER" primaryKey="true" autoincrement="true" />
+ <column name="name" type="VARCHAR" size="255" />
+ <behavior name="extra_properties">
+ <parameter name="normalize" value="false" />
+ </behavior>
+ </table>
+</database>
+EOF;
+ PropelQuickBuilder::buildSchema($schema);
+ }
+ }
+
+ public function allGeneratedClassesDataProvider()
+ {
+ return array(
+ array('ExtraPropertiesBehaviorTestNormalizePeer'),
+ array('ExtraPropertiesBehaviorTestDoNotNormalizePeer'),
+ );
+ }
+
+ /**
+ * @dataProvider allGeneratedClassesDataProvider
+ */
+ public function testMethodExists($class)
+ {
+ $obj = new $class();
+ $this->assertTrue(method_exists($obj, 'normalizeExtraPropertyName'));
+ $this->assertTrue(method_exists($obj, 'normalizeExtraPropertyValue'));
+ }
+
+ public function normalizeDataProvider()
+ {
+ return array(
+ array('foo', 'FOO'),
+ array('#this is the Key', '#THIS IS THE KEY'),
+ );
+ }
+
+ /**
+ * @dataProvider normalizeDataProvider
+ */
+ public function testNormalizePropertyName($source, $expected)
+ {
+ $result = ExtraPropertiesBehaviorTestNormalizePeer::normalizeExtraPropertyName($source);
+ $this->assertInternalType('string', $result);
+ $this->assertEquals($result, $expected);
+ }
+
+ /**
+ * @dataProvider normalizeDataProvider
+ */
+ public function testDoNotNormalizePropertyName($source, $expected)
+ {
+ $result = ExtraPropertiesBehaviorTestDoNotNormalizePeer::normalizeExtraPropertyName($source);
+ $this->assertInternalType('string', $result);
+ $this->assertEquals($result, $source);
+ }
+
+ public function normalizeValueDataProvider()
+ {
+ return array(
+ array('foo', 'foo'),
+ array('#this is the Key', '#this is the Key'),
+ );
+ }
+
+ /**
+ * @dataProvider normalizeValueDataProvider
+ */
+ public function testNormalizePropertyValue($source, $expected)
+ {
+ $result = ExtraPropertiesBehaviorTestNormalizePeer::normalizeExtraPropertyValue($source);
+ $this->assertInternalType('string', $result);
+ $this->assertEquals($result, $expected);
+ }
+
+ /**
+ * @dataProvider normalizeValueDataProvider
+ */
+ public function testDoNotNormalizePropertyValue($source, $expected)
+ {
+ $result = ExtraPropertiesBehaviorTestDoNotNormalizePeer::normalizeExtraPropertyValue($source);
+ $this->assertInternalType('string', $result);
+ $this->assertEquals($result, $expected);
+ }
+}

0 comments on commit 4820cc8

Please sign in to comment.