FLOW3-Composer /Tests/Unit/Object/Proxy/ProxyMethodTest.php

Language PHP Lines 174
MD5 Hash c3b49ae9a00c842fbe61ee8e9f2446f8
Repository https://github.com/christianjul/FLOW3-Composer.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?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);
	}
}

?>
Back to Top