| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_CodeGenerator
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id $
- */
- require_once 'Zend/CodeGenerator/Php/Property.php';
- require_once 'Zend/Reflection/Class.php';
- /**
- * @category Zend
- * @package Zend_CodeGenerator
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- *
- * @group Zend_CodeGenerator
- * @group Zend_CodeGenerator_Php
- */
- class Zend_CodeGenerator_Php_PropertyTest extends PHPUnit_Framework_TestCase
- {
- public function setup()
- {
- if (!class_exists('Zend_CodeGenerator_Php_TestClassWithManyProperties')) {
- require_once dirname(__FILE__) . '/_files/TestClassWithManyProperties.php';
- }
- }
- public function testPropertyConstructor()
- {
- $codeGenProperty = new Zend_CodeGenerator_Php_Property();
- $this->isInstanceOf($codeGenProperty, 'Zend_CodeGenerator_Php_Property');
- }
- public function testPropertyReturnsSimpleValue()
- {
- $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value'));
- $this->assertEquals(' public $someVal = \'some string value\';', $codeGenProperty->generate());
- }
- public function testPropertyMultilineValue()
- {
- $targetValue = array(
- 5,
- 'one' => 1,
- 'two' => '2',
- 'null' => null,
- 'true' => true,
- "bar's" => "bar's",
- );
- $expectedSource = <<<EOS
- public \$myFoo = array(
- 5,
- 'one' => 1,
- 'two' => '2',
- 'null' => null,
- 'true' => true,
- 'bar\'s' => 'bar\'s'
- );
- EOS;
- $property = new Zend_CodeGenerator_Php_Property(array(
- 'name' => 'myFoo',
- 'defaultValue' => $targetValue
- ));
- $targetSource = $property->generate();
- $targetSource = str_replace("\r", '', $targetSource);
- $this->assertEquals($expectedSource, $targetSource);
- }
- public function testPropertyCanProduceContstantModifier()
- {
- $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'const' => true));
- $this->assertEquals(' const someVal = \'some string value\';', $codeGenProperty->generate());
- }
- public function testPropertyCanProduceStaticModifier()
- {
- $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'static' => true));
- $this->assertEquals(' public static $someVal = \'some string value\';', $codeGenProperty->generate());
- }
- /**
- * @group ZF-6444
- */
- public function testPropertyWillLoadFromReflection()
- {
- $reflectionClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_TestClassWithManyProperties');
- // test property 1
- $reflProp = $reflectionClass->getProperty('_bazProperty');
- $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
- $this->assertEquals('_bazProperty', $cgProp->getName());
- $this->assertEquals(array(true, false, true), $cgProp->getDefaultValue()->getValue());
- $this->assertEquals('private', $cgProp->getVisibility());
- $reflProp = $reflectionClass->getProperty('_bazStaticProperty');
- // test property 2
- $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
- $this->assertEquals('_bazStaticProperty', $cgProp->getName());
- $this->assertEquals(Zend_CodeGenerator_Php_TestClassWithManyProperties::FOO, $cgProp->getDefaultValue()->getValue());
- $this->assertTrue($cgProp->isStatic());
- $this->assertEquals('private', $cgProp->getVisibility());
- }
- /**
- * @group ZF-6444
- */
- public function testPropertyWillEmitStaticModifier()
- {
- $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
- 'name' => 'someVal',
- 'static' => true,
- 'visibility' => 'protected',
- 'defaultValue' => 'some string value'
- ));
- $this->assertEquals(' protected static $someVal = \'some string value\';', $codeGenProperty->generate());
- }
- /**
- * @group ZF-7205
- */
- public function testPropertyCanHaveDocblock()
- {
- $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
- 'name' => 'someVal',
- 'static' => true,
- 'visibility' => 'protected',
- 'defaultValue' => 'some string value',
- 'docblock' => '@var string $someVal This is some val'
- ));
- $expected = <<<EOS
- /**
- * @var string \$someVal This is some val
- */
- protected static \$someVal = 'some string value';
- EOS;
- $this->assertEquals($expected, $codeGenProperty->generate());
- }
- public function testOtherTypesThrowExceptionOnGenerate()
- {
- $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
- 'name' => 'someVal',
- 'defaultValue' => new stdClass(),
- ));
- $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
- $codeGenProperty->generate();
- }
- static public function dataSetTypeSetValueGenerate()
- {
- return array(
- array('string', 'foo', "'foo';"),
- array('int', 1, "1;"),
- array('integer', 1, "1;"),
- array('bool', true, "true;"),
- array('bool', false, "false;"),
- array('boolean', true, "true;"),
- array('number', 1, '1;'),
- array('float', 1.23, '1.23;'),
- array('double', 1.23, '1.23;'),
- array('constant', 'FOO', 'FOO;'),
- array('null', null, 'null;'),
- );
- }
- /**
- * @dataProvider dataSetTypeSetValueGenerate
- * @param string $type
- * @param mixed $value
- * @param string $code
- */
- public function testSetTypeSetValueGenerate($type, $value, $code)
- {
- $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
- $defaultValue->setType($type);
- $defaultValue->setValue($value);
- $this->assertEquals($type, $defaultValue->getType());
- $this->assertEquals($code, $defaultValue->generate());
- }
- /**
- * @dataProvider dataSetTypeSetValueGenerate
- * @param string $type
- * @param mixed $value
- * @param string $code
- */
- public function testSetBogusTypeSetValueGenerateUseAutoDetection($type, $value, $code)
- {
- if($type == 'constant') {
- return; // constant can only be detected explicitly
- }
- $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
- $defaultValue->setType("bogus");
- $defaultValue->setValue($value);
- $this->assertEquals($code, $defaultValue->generate());
- }
- /**
- * @group ZF-8849
- */
- public function testZF8849()
- {
- $property = new Zend_CodeGenerator_Php_Property(array(
- 'defaultValue' => array('value' => 1.337, 'type' => 'string'),
- 'name' => 'ZF8849',
- 'const' => true
- ));
- $this->assertEquals(
- $property->generate(),
- " const ZF8849 = '1.337';"
- );
- }
- }
|