/Packages/Framework/TYPO3.FLOW3/Tests/Unit/AOP/Pointcut/PointcutMethodNameFilterTest.php

https://github.com/crashd0wn/fx-viper · PHP · 147 lines · 85 code · 23 blank · 39 comment · 0 complexity · 6b5bcaa44a86e168fa98b7af54bb9a1e MD5 · raw file

  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 as published by the *
  8. * Free Software Foundation, either version 3 of the License, or (at your *
  9. * option) any later version. *
  10. * *
  11. * This script is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
  13. * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
  14. * General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU Lesser General Public *
  17. * License along with the script. *
  18. * If not, see http://www.gnu.org/licenses/lgpl.html *
  19. * *
  20. * The TYPO3 project - inspiring people to share! *
  21. * */
  22. require_once (FLOW3_PATH_FLOW3 . 'Tests/Unit/AOP/Fixtures/MethodsTaggedWithSomething.php');
  23. /**
  24. * Testcase for the Pointcut Method Name Filter
  25. *
  26. */
  27. class PointcutMethodNameFilterTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  28. /**
  29. * @test
  30. * @author Robert Lemke <robert@typo3.org>
  31. */
  32. public function matchesIgnoresFinalMethodsEvenIfTheirNameMatches() {
  33. $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
  34. eval("
  35. class $className {
  36. final public function someFinalMethod() {}
  37. }"
  38. );
  39. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array('loadFromCache', 'saveToCache'));
  40. $methodNameFilter = new \TYPO3\FLOW3\AOP\Pointcut\PointcutMethodNameFilter('someFinalMethod');
  41. $methodNameFilter->injectReflectionService($mockReflectionService);
  42. $this->assertFalse($methodNameFilter->matches($className, 'someFinalMethod', $className, 1));
  43. }
  44. /**
  45. * @test
  46. * @author Robert Lemke <robert@typo3.org>
  47. */
  48. public function matchesTakesTheVisibilityModifierIntoAccountIfOneWasSpecified() {
  49. $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
  50. eval("
  51. class $className {
  52. public function somePublicMethod() {}
  53. protected function someProtectedMethod() {}
  54. private function somePrivateMethod() {}
  55. }"
  56. );
  57. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array('loadFromCache', 'saveToCache'));
  58. $methodNameFilter = new \TYPO3\FLOW3\AOP\Pointcut\PointcutMethodNameFilter('some.*', 'public');
  59. $methodNameFilter->injectReflectionService($mockReflectionService);
  60. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'somePublicMethod', $className, 1));
  61. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'someProtectedMethod', $className, 1));
  62. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePrivateMethod', $className, 1));
  63. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePublicMethod', NULL, 1));
  64. $methodNameFilter = new \TYPO3\FLOW3\AOP\Pointcut\PointcutMethodNameFilter('some.*', 'protected');
  65. $methodNameFilter->injectReflectionService($mockReflectionService);
  66. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePublicMethod', $className, 1));
  67. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someProtectedMethod', $className, 1));
  68. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'somePrivateMethod', $className, 1));
  69. $this->assertFalse($methodNameFilter->matches(__CLASS__, 'someProtectedMethod', NULL, 1));
  70. }
  71. /**
  72. * @test
  73. * @author Andreas Förthner <andreas.foerthner@netlogix.de>
  74. */
  75. public function matchesChecksTheAvailablityOfAnArgumentNameIfArgumentConstraintsHaveBeenConfigured() {
  76. $className = 'TestClass' . md5(uniqid(mt_rand(), TRUE));
  77. eval("
  78. class $className {
  79. public function somePublicMethod(\$arg1) {}
  80. public function someOtherPublicMethod(\$arg1, \$arg2 = 'default') {}
  81. public function someThirdMethod(\$arg1, \$arg2, \$arg3 = 'default') {}
  82. }"
  83. );
  84. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array('loadFromCache', 'saveToCache'));
  85. $mockSystemLogger = $this->getMock('TYPO3\FLOW3\Log\Logger');
  86. $mockSystemLogger->expects($this->once())->method('log')->with($this->equalTo(
  87. 'The argument "arg2" declared in pointcut does not exist in method ' . $className . '->somePublicMethod'
  88. ));
  89. $argumentConstraints = array(
  90. 'arg1' => array(
  91. 'operator' => '==',
  92. 'value' => 'someValue'
  93. ),
  94. 'arg2.some.sub.object' => array(
  95. 'operator' => '==',
  96. 'value' => 'someValue'
  97. )
  98. );
  99. $methodNameFilter = new \TYPO3\FLOW3\AOP\Pointcut\PointcutMethodNameFilter('some.*', null, $argumentConstraints);
  100. $methodNameFilter->injectReflectionService($mockReflectionService);
  101. $methodNameFilter->injectSystemLogger($mockSystemLogger);
  102. $methodNameFilter->matches(__CLASS__, 'somePublicMethod', $className, 1);
  103. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someOtherPublicMethod', $className, 1));
  104. $this->assertTrue($methodNameFilter->matches(__CLASS__, 'someThirdMethod', $className, 1));
  105. }
  106. /**
  107. * @test
  108. * @author Andreas Förthner <andreas.foerthner@netlogix.de>
  109. */
  110. public function getRuntimeEvaluationsReturnsTheMethodArgumentConstraintsDefinitions() {
  111. $argumentConstraints = array(
  112. 'arg2' => array(
  113. 'operator' => '==',
  114. 'value' => 'someValue'
  115. )
  116. );
  117. $expectedRuntimeEvaluations = array(
  118. 'methodArgumentConstraints' => $argumentConstraints
  119. );
  120. $methodNameFilter = new \TYPO3\FLOW3\AOP\Pointcut\PointcutMethodNameFilter('some.*', null, $argumentConstraints);
  121. $this->assertEquals($expectedRuntimeEvaluations, $methodNameFilter->getRuntimeEvaluationsDefinition(), 'The argument constraint definitions have not been returned as expected.');
  122. }
  123. }
  124. ?>