/Tests/Unit/Object/Proxy/ProxyMethodTest.php
PHP | 173 lines | 129 code | 20 blank | 24 comment | 0 complexity | c3b49ae9a00c842fbe61ee8e9f2446f8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
- <?php
- namespace TYPO3\FLOW3\Tests\Unit\Object\Proxy;
- /* *
- * This script belongs to the FLOW3 framework. *
- * *
- * It is free software; you can redistribute it and/or modify it under *
- * the terms of the GNU Lesser General Public License, either version 3 *
- * of the License, or (at your option) any later version. *
- * *
- * The TYPO3 project - inspiring people to share! *
- * */
- /**
- *
- */
- class ProxyMethodTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
- /**
- * @test
- */
- public function buildMethodDocumentationAddsAllExpectedAnnotations() {
- $validateFoo1 = new \TYPO3\FLOW3\Annotations\Validate(array('value' => 'foo1', 'type' => 'bar1'));
- $validateFoo2 = new \TYPO3\FLOW3\Annotations\Validate(array('value' => 'foo2', 'type' => 'bar2'));
- $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
- $mockReflectionService->expects($this->any())->method('hasMethod')->will($this->returnValue(TRUE));
- $mockReflectionService->expects($this->any())->method('getIgnoredTags')->will($this->returnValue(array('return')));
- $mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with('My\Class\Name', 'myMethod')->will($this->returnValue(array(
- 'param' => array('string $name')
- )));
- $mockReflectionService->expects($this->any())->method('getMethodAnnotations')->with('My\Class\Name', 'myMethod')->will($this->returnValue(array(
- $validateFoo1,
- $validateFoo2,
- new \TYPO3\FLOW3\Annotations\SkipCsrfProtection(array())
- )));
- $mockProxyMethod = $this->getAccessibleMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
- $mockProxyMethod->injectReflectionService($mockReflectionService);
- $methodDocumentation = $mockProxyMethod->_call('buildMethodDocumentation', 'My\Class\Name', 'myMethod');
- $expected =
- ' /**' . chr(10) .
- ' * Autogenerated Proxy Method' . chr(10) .
- ' * @param string $name' . chr(10) .
- ' * @\TYPO3\FLOW3\Annotations\Validate(type="bar1", argumentName="foo1")' . chr(10) .
- ' * @\TYPO3\FLOW3\Annotations\Validate(type="bar2", argumentName="foo2")' . chr(10) .
- ' * @\TYPO3\FLOW3\Annotations\SkipCsrfProtection' . chr(10) .
- ' */' . chr(10);
- $this->assertEquals($expected, $methodDocumentation);
- }
- /**
- * @test
- */
- public function buildMethodParametersCodeRendersParametersCodeWithCorrectTypeHintsAndDefaultValues() {
- $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
- eval('
- /**
- * @param string $arg1 Arg1
- */
- class ' . $className . ' {
- public function foo($arg1, array $arg2, \ArrayObject $arg3, $arg4= "foo", $arg5 = TRUE, array $arg6 = array(TRUE, \'foo\' => \'bar\', NULL, 3 => 1, 2.3)) {}
- }
- ');
- $methodParameters = array(
- 'arg1' => array(
- 'position' => 0,
- 'byReference' => FALSE,
- 'array' => FALSE,
- 'optional' => FALSE,
- 'allowsNull' => TRUE,
- 'class' => NULL
- ),
- 'arg2' => array(
- 'position' => 1,
- 'byReference' => FALSE,
- 'array' => TRUE,
- 'optional' => FALSE,
- 'allowsNull' => TRUE,
- 'class' => NULL
- ),
- 'arg3' => array(
- 'position' => 2,
- 'byReference' => FALSE,
- 'array' => FALSE,
- 'optional' => FALSE,
- 'allowsNull' => TRUE,
- 'class' => 'ArrayObject'
- ),
- 'arg4' => array(
- 'position' => 3,
- 'byReference' => FALSE,
- 'array' => FALSE,
- 'optional' => TRUE,
- 'allowsNull' => TRUE,
- 'class' => NULL,
- 'defaultValue' => 'foo'
- ),
- 'arg5' => array(
- 'position' => 4,
- 'byReference' => FALSE,
- 'array' => FALSE,
- 'optional' => TRUE,
- 'allowsNull' => TRUE,
- 'class' => NULL,
- 'defaultValue' => TRUE
- ),
- 'arg6' => array(
- 'position' => 5,
- 'byReference' => FALSE,
- 'array' => TRUE,
- 'optional' => TRUE,
- 'allowsNull' => TRUE,
- 'class' => NULL,
- 'defaultValue' => array(0 => TRUE, 'foo' => 'bar', 1 => NULL, 3 => 1, 4 => 2.3)
- ),
- );
- $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService');
- $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($methodParameters));
- $expectedCode = '$arg1, array $arg2, \ArrayObject $arg3, $arg4 = \'foo\', $arg5 = TRUE, array $arg6 = array(0 => TRUE, \'foo\' => \'bar\', 1 => NULL, 3 => 1, 4 => 2.3)';
- $builder = $this->getMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
- $builder->injectReflectionService($mockReflectionService);
- $actualCode = $builder->buildMethodParametersCode($className, 'foo', TRUE);
- $this->assertSame($expectedCode, $actualCode);
- }
- /**
- * @test
- */
- public function buildMethodParametersCodeOmitsTypeHintsAndDefaultValuesIfToldSo() {
- $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
- eval('
- class ' . $className . ' {
- public function foo($arg1, array $arg2, \ArrayObject $arg3, $arg4= "foo", $arg5 = TRUE) {}
- }
- ');
- $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService');
- $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue(array(
- 'arg1' => array(),
- 'arg2' => array(),
- 'arg3' => array(),
- 'arg4' => array(),
- 'arg5' => array()
- )));
- $expectedCode = '$arg1, $arg2, $arg3, $arg4, $arg5';
- $builder = $this->getMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
- $builder->injectReflectionService($mockReflectionService);
- $actualCode = $builder->buildMethodParametersCode($className, 'foo', FALSE);
- $this->assertSame($expectedCode, $actualCode);
- }
- /**
- * @test
- */
- public function buildMethodParametersCodeReturnsAnEmptyStringIfTheClassNameIsNULL() {
- $builder = $this->getMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
- $actualCode = $builder->buildMethodParametersCode(NULL, 'foo', TRUE);
- $this->assertSame('', $actualCode);
- }
- }
- ?>