PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Unit/Persistence/Aspect/PersistenceMagicAspectTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 129 lines | 75 code | 24 blank | 30 comment | 0 complexity | 38f4e0151136e7538f21df1410e92106 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Persistence\Aspect;
  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 PersistenceMagicAspect
  14. *
  15. */
  16. class PersistenceMagicAspectTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  17. /**
  18. * @test
  19. * @return void
  20. */
  21. public function cloneObjectMarksTheObjectAsCloned() {
  22. $object = new \stdClass();
  23. $mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');
  24. $mockJoinPoint->expects($this->any())->method('getProxy')->will($this->returnValue($object));
  25. $aspect = new \TYPO3\FLOW3\Persistence\Aspect\PersistenceMagicAspect();
  26. $aspect->cloneObject($mockJoinPoint);
  27. $this->assertTrue($object->FLOW3_Persistence_clone);
  28. }
  29. /**
  30. * @test
  31. * @return void
  32. */
  33. public function generateUuidGeneratesUuidAndRegistersProxyAsNewObject() {
  34. $className = 'Class' . md5(uniqid(mt_rand(), TRUE));
  35. eval('class ' . $className . ' implements \TYPO3\FLOW3\Persistence\Aspect\PersistenceMagicInterface { public $FLOW3_Persistence_Identifier = NULL; }');
  36. $object = new $className();
  37. $mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');
  38. $mockJoinPoint->expects($this->atLeastOnce())->method('getProxy')->will($this->returnValue($object));
  39. $mockPersistenceManager = $this->getMock('TYPO3\FLOW3\Persistence\PersistenceManagerInterface');
  40. $mockPersistenceManager->expects($this->atLeastOnce())->method('registerNewObject')->with($object);
  41. $aspect = $this->getAccessibleMock('TYPO3\FLOW3\Persistence\Aspect\PersistenceMagicAspect', array('dummy'), array());
  42. $aspect->_set('persistenceManager', $mockPersistenceManager);
  43. $aspect->generateUuid($mockJoinPoint);
  44. $this->assertEquals(36, strlen($object->FLOW3_Persistence_Identifier));
  45. }
  46. /**
  47. * @test
  48. */
  49. public function generateValueHashUsesIdentifierSubObjects() {
  50. $subObject1 = new \stdClass();
  51. $subObject1->FLOW3_Persistence_Identifier = 'uuid';
  52. $subObject2 = new \stdClass();
  53. $subObject2->FLOW3_Persistence_Identifier = 'hash';
  54. $methodArguments = array(
  55. 'foo' => $subObject1,
  56. 'bar' => $subObject2
  57. );
  58. $className = 'Class' . md5(uniqid(mt_rand(), TRUE));
  59. eval('class ' . $className . ' { public $foo; public $bar; }');
  60. $object = new $className();
  61. $mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');
  62. $mockJoinPoint->expects($this->atLeastOnce())->method('getProxy')->will($this->returnValue($object));
  63. $mockJoinPoint->expects($this->atLeastOnce())->method('getMethodArguments')->will($this->returnValue($methodArguments));
  64. $aspect = new \TYPO3\FLOW3\Persistence\Aspect\PersistenceMagicAspect();
  65. $aspect->generateValueHash($mockJoinPoint);
  66. $this->assertEquals(sha1($className . 'uuidhash'), $object->FLOW3_Persistence_Identifier);
  67. }
  68. /**
  69. * @test
  70. */
  71. public function generateValueHashUsesExistingPersistenceIdentifierForNestedConstructorCalls() {
  72. $methodArguments = array(
  73. 'foo' => 'bar'
  74. );
  75. $className = 'Class' . md5(uniqid(mt_rand(), TRUE));
  76. eval('class ' . $className . ' { public $foo; public $bar; }');
  77. $object = new $className();
  78. $object->FLOW3_Persistence_Identifier = 'existinguuidhash';
  79. $mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');
  80. $mockJoinPoint->expects($this->atLeastOnce())->method('getProxy')->will($this->returnValue($object));
  81. $mockJoinPoint->expects($this->atLeastOnce())->method('getMethodArguments')->will($this->returnValue($methodArguments));
  82. $aspect = new \TYPO3\FLOW3\Persistence\Aspect\PersistenceMagicAspect();
  83. $aspect->generateValueHash($mockJoinPoint);
  84. $this->assertEquals(sha1($className . 'existinguuidhash' . 'bar'), $object->FLOW3_Persistence_Identifier);
  85. }
  86. /**
  87. * @test
  88. */
  89. public function generateValueHashUsesTimestampOfDateTime() {
  90. $date = new \DateTime();
  91. $methodArguments = array(
  92. 'foo' => new \DateTime()
  93. );
  94. $className = 'Class' . md5(uniqid(mt_rand(), TRUE));
  95. eval('class ' . $className . ' { }');
  96. $object = new $className();
  97. $mockJoinPoint = $this->getMock('TYPO3\FLOW3\Aop\JoinPointInterface');
  98. $mockJoinPoint->expects($this->atLeastOnce())->method('getProxy')->will($this->returnValue($object));
  99. $mockJoinPoint->expects($this->atLeastOnce())->method('getMethodArguments')->will($this->returnValue($methodArguments));
  100. $aspect = new \TYPO3\FLOW3\Persistence\Aspect\PersistenceMagicAspect();
  101. $aspect->generateValueHash($mockJoinPoint);
  102. $this->assertEquals(sha1($className . $date->getTimestamp()), $object->FLOW3_Persistence_Identifier);
  103. }
  104. }
  105. ?>