PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Tests/Unit/Aop/Pointcut/PointcutMethodNameFilterTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 141 lines | 94 code | 22 blank | 25 comment | 0 complexity | a50fb0318b4a71c6dca63952cc8fda13 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Aop\Pointcut;
  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. * Testcase for the Pointcut Method Name Filter
  14. *
  15. */
  16. class PointcutMethodNameFilterTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  17. /**
  18. * @test
  19. */
  20. public function matchesIgnoresFinalMethodsEvenIfTheirNameMatches() {
  21. $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
  22. eval("
  23. class $className {
  24. final public function someFinalMethod() {}
  25. }"
  26. );
  27. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array('isMethodFinal'), array(), '', FALSE);
  28. $mockReflectionService->expects($this->atLeastOnce())->method('isMethodFinal')->with($className, 'someFinalMethod')->will($this->returnValue(TRUE));
  29. $methodNameFilter = new \TYPO3\FLOW3\Aop\Pointcut\PointcutMethodNameFilter('someFinalMethod');
  30. $methodNameFilter->injectReflectionService($mockReflectionService);
  31. $this->assertFalse($methodNameFilter->matches($className, 'someFinalMethod', $className, 1));
  32. }
  33. /**
  34. * @test
  35. */
  36. public function matchesTakesTheVisibilityModifierIntoAccountIfOneWasSpecified() {
  37. $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
  38. eval("
  39. class $className {
  40. public function somePublicMethod() {}
  41. protected function someProtectedMethod() {}
  42. private function somePrivateMethod() {}
  43. }"
  44. );
  45. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService');
  46. $mockReflectionService->expects($this->atLeastOnce())->method('isMethodPublic')->will($this->onConsecutiveCalls(TRUE, FALSE, FALSE, TRUE));
  47. $mockReflectionService->expects($this->atLeastOnce())->method('isMethodProtected')->will($this->onConsecutiveCalls(FALSE, TRUE, FALSE, FALSE));
  48. $mockReflectionService->expects($this->atLeastOnce())->method('isMethodFinal')->will($this->returnValue(FALSE));
  49. $mockReflectionService->expects($this->atLeastOnce())->method('getMethodParameters')->will($this->returnValue(array()));
  50. $methodNameFilter = new \TYPO3\FLOW3\Aop\Pointcut\PointcutMethodNameFilter('some.*', 'public');
  51. $methodNameFilter->injectReflectionService($mockReflectionService);
  52. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'somePublicMethod', $className, 1));
  53. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'someProtectedMethod', $className, 1));
  54. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePrivateMethod', $className, 1));
  55. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePublicMethod', NULL, 1));
  56. $methodNameFilter = new \TYPO3\FLOW3\Aop\Pointcut\PointcutMethodNameFilter('some.*', 'protected');
  57. $methodNameFilter->injectReflectionService($mockReflectionService);
  58. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePublicMethod', $className, 1));
  59. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someProtectedMethod', $className, 1));
  60. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePrivateMethod', $className, 1));
  61. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'someProtectedMethod', NULL, 1));
  62. }
  63. /**
  64. * @test
  65. */
  66. public function matchesChecksTheAvailablityOfAnArgumentNameIfArgumentConstraintsHaveBeenConfigured() {
  67. $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
  68. eval("
  69. class $className {
  70. public function somePublicMethod(\$arg1) {}
  71. public function someOtherPublicMethod(\$arg1, \$arg2 = 'default') {}
  72. public function someThirdMethod(\$arg1, \$arg2, \$arg3 = 'default') {}
  73. }"
  74. );
  75. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService');
  76. $mockReflectionService->expects($this->exactly(3))->method('getMethodParameters')->will($this->onConsecutiveCalls(
  77. array('arg1' => array()),
  78. array('arg1' => array(), 'arg2' => array()),
  79. array('arg1' => array(), 'arg2' => array(), 'arg3' => array())
  80. ));
  81. $mockSystemLogger = $this->getMock('TYPO3\FLOW3\Log\Logger');
  82. $mockSystemLogger->expects($this->once())->method('log')->with($this->equalTo(
  83. 'The argument "arg2" declared in pointcut does not exist in method ' . $className . '->somePublicMethod'
  84. ));
  85. $argumentConstraints = array(
  86. 'arg1' => array(
  87. 'operator' => '==',
  88. 'value' => 'someValue'
  89. ),
  90. 'arg2.some.sub.object' => array(
  91. 'operator' => '==',
  92. 'value' => 'someValue'
  93. )
  94. );
  95. $methodNameFilter = new \TYPO3\FLOW3\Aop\Pointcut\PointcutMethodNameFilter('some.*', null, $argumentConstraints);
  96. $methodNameFilter->injectReflectionService($mockReflectionService);
  97. $methodNameFilter->injectSystemLogger($mockSystemLogger);
  98. $methodNameFilter->matches(__CLASS__, 'somePublicMethod', $className, 1);
  99. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someOtherPublicMethod', $className, 1));
  100. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someThirdMethod', $className, 1));
  101. }
  102. /**
  103. * @test
  104. */
  105. public function getRuntimeEvaluationsReturnsTheMethodArgumentConstraintsDefinitions() {
  106. $argumentConstraints = array(
  107. 'arg2' => array(
  108. 'operator' => '==',
  109. 'value' => 'someValue'
  110. )
  111. );
  112. $expectedRuntimeEvaluations = array(
  113. 'methodArgumentConstraints' => $argumentConstraints
  114. );
  115. $methodNameFilter = new \TYPO3\FLOW3\Aop\Pointcut\PointcutMethodNameFilter('some.*', null, $argumentConstraints);
  116. $this->assertEquals($expectedRuntimeEvaluations, $methodNameFilter->getRuntimeEvaluationsDefinition(), 'The argument constraint definitions have not been returned as expected.');
  117. }
  118. }
  119. ?>