PageRenderTime 62ms CodeModel.GetById 30ms app.highlight 9ms RepoModel.GetById 21ms app.codeStats 0ms

/Tests/Unit/Object/Proxy/ProxyMethodTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 173 lines | 129 code | 20 blank | 24 comment | 0 complexity | c3b49ae9a00c842fbe61ee8e9f2446f8 MD5 | raw file
  1<?php
  2namespace TYPO3\FLOW3\Tests\Unit\Object\Proxy;
  3
  4/*                                                                        *
  5 * This script belongs to the FLOW3 framework.                            *
  6 *                                                                        *
  7 * It is free software; you can redistribute it and/or modify it under    *
  8 * the terms of the GNU Lesser General Public License, either version 3   *
  9 * of the License, or (at your option) any later version.                 *
 10 *                                                                        *
 11 * The TYPO3 project - inspiring people to share!                         *
 12 *                                                                        */
 13
 14/**
 15 *
 16 */
 17class ProxyMethodTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
 18
 19	/**
 20	 * @test
 21	 */
 22	public function buildMethodDocumentationAddsAllExpectedAnnotations() {
 23		$validateFoo1 = new \TYPO3\FLOW3\Annotations\Validate(array('value' => 'foo1', 'type' => 'bar1'));
 24		$validateFoo2 = new \TYPO3\FLOW3\Annotations\Validate(array('value' => 'foo2', 'type' => 'bar2'));
 25
 26		$mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
 27		$mockReflectionService->expects($this->any())->method('hasMethod')->will($this->returnValue(TRUE));
 28		$mockReflectionService->expects($this->any())->method('getIgnoredTags')->will($this->returnValue(array('return')));
 29		$mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with('My\Class\Name', 'myMethod')->will($this->returnValue(array(
 30			'param' => array('string $name')
 31		)));
 32		$mockReflectionService->expects($this->any())->method('getMethodAnnotations')->with('My\Class\Name', 'myMethod')->will($this->returnValue(array(
 33			$validateFoo1,
 34			$validateFoo2,
 35			new \TYPO3\FLOW3\Annotations\SkipCsrfProtection(array())
 36		)));
 37
 38
 39		$mockProxyMethod = $this->getAccessibleMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
 40		$mockProxyMethod->injectReflectionService($mockReflectionService);
 41		$methodDocumentation = $mockProxyMethod->_call('buildMethodDocumentation', 'My\Class\Name', 'myMethod');
 42
 43		$expected =
 44			'	/**' . chr(10) .
 45			'	 * Autogenerated Proxy Method' . chr(10) .
 46			'	 * @param string $name' . chr(10) .
 47			'	 * @\TYPO3\FLOW3\Annotations\Validate(type="bar1", argumentName="foo1")' . chr(10) .
 48			'	 * @\TYPO3\FLOW3\Annotations\Validate(type="bar2", argumentName="foo2")' . chr(10) .
 49			'	 * @\TYPO3\FLOW3\Annotations\SkipCsrfProtection' . chr(10) .
 50			'	 */' . chr(10);
 51		$this->assertEquals($expected, $methodDocumentation);
 52	}
 53
 54	/**
 55	 * @test
 56	 */
 57	public function buildMethodParametersCodeRendersParametersCodeWithCorrectTypeHintsAndDefaultValues() {
 58		$className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
 59		eval('
 60			/**
 61			 * @param string $arg1 Arg1
 62			 */
 63			class ' . $className . ' {
 64				public function foo($arg1, array $arg2, \ArrayObject $arg3, $arg4= "foo", $arg5 = TRUE, array $arg6 = array(TRUE, \'foo\' => \'bar\', NULL, 3 => 1, 2.3)) {}
 65			}
 66		');
 67		$methodParameters = array(
 68			'arg1' => array(
 69				'position' => 0,
 70				'byReference' => FALSE,
 71				'array' => FALSE,
 72				'optional' => FALSE,
 73				'allowsNull' => TRUE,
 74				'class' => NULL
 75			),
 76			'arg2' => array(
 77				'position' => 1,
 78				'byReference' => FALSE,
 79				'array' => TRUE,
 80				'optional' => FALSE,
 81				'allowsNull' => TRUE,
 82				'class' => NULL
 83			),
 84			'arg3' => array(
 85				'position' => 2,
 86				'byReference' => FALSE,
 87				'array' => FALSE,
 88				'optional' => FALSE,
 89				'allowsNull' => TRUE,
 90				'class' => 'ArrayObject'
 91			),
 92			'arg4' => array(
 93				'position' => 3,
 94				'byReference' => FALSE,
 95				'array' => FALSE,
 96				'optional' => TRUE,
 97				'allowsNull' => TRUE,
 98				'class' => NULL,
 99				'defaultValue' => 'foo'
100			),
101			'arg5' => array(
102				'position' => 4,
103				'byReference' => FALSE,
104				'array' => FALSE,
105				'optional' => TRUE,
106				'allowsNull' => TRUE,
107				'class' => NULL,
108				'defaultValue' => TRUE
109			),
110			'arg6' => array(
111				'position' => 5,
112				'byReference' => FALSE,
113				'array' => TRUE,
114				'optional' => TRUE,
115				'allowsNull' => TRUE,
116				'class' => NULL,
117				'defaultValue' => array(0 => TRUE, 'foo' => 'bar', 1 => NULL, 3 => 1, 4 => 2.3)
118			),
119		);
120
121		$mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService');
122		$mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue($methodParameters));
123
124		$expectedCode = '$arg1, array $arg2, \ArrayObject $arg3, $arg4 = \'foo\', $arg5 = TRUE, array $arg6 = array(0 => TRUE, \'foo\' => \'bar\', 1 => NULL, 3 => 1, 4 => 2.3)';
125
126		$builder = $this->getMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
127		$builder->injectReflectionService($mockReflectionService);
128
129		$actualCode = $builder->buildMethodParametersCode($className, 'foo', TRUE);
130		$this->assertSame($expectedCode, $actualCode);
131	}
132
133	/**
134	 * @test
135	 */
136	public function buildMethodParametersCodeOmitsTypeHintsAndDefaultValuesIfToldSo() {
137		$className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
138		eval('
139			class ' . $className . ' {
140				public function foo($arg1, array $arg2, \ArrayObject $arg3, $arg4= "foo", $arg5 = TRUE) {}
141			}
142		');
143
144		$mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService');
145		$mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue(array(
146			'arg1' => array(),
147			'arg2' => array(),
148			'arg3' => array(),
149			'arg4' => array(),
150			'arg5' => array()
151		)));
152
153		$expectedCode = '$arg1, $arg2, $arg3, $arg4, $arg5';
154
155		$builder = $this->getMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
156		$builder->injectReflectionService($mockReflectionService);
157
158		$actualCode = $builder->buildMethodParametersCode($className, 'foo', FALSE);
159		$this->assertSame($expectedCode, $actualCode);
160	}
161
162	/**
163	 * @test
164	 */
165	public function buildMethodParametersCodeReturnsAnEmptyStringIfTheClassNameIsNULL() {
166		$builder = $this->getMock('TYPO3\FLOW3\Object\Proxy\ProxyMethod', array('dummy'), array(), '', FALSE);
167
168		$actualCode = $builder->buildMethodParametersCode(NULL, 'foo', TRUE);
169		$this->assertSame('', $actualCode);
170	}
171}
172
173?>