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