/tests/Jackalope/NodePathIteratorTest.php

https://github.com/wjzijderveld/jackalope · PHP · 176 lines · 133 code · 24 blank · 19 comment · 3 complexity · cd0dc577cb40a3c94420f6c64fecf297 MD5 · raw file

  1. <?php
  2. namespace Jackalope;
  3. class NodePathIteratorTest extends TestCase
  4. {
  5. /**
  6. * @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  7. */
  8. private $objectManager;
  9. public function setUp()
  10. {
  11. $this->objectManager = $this->getObjectManagerMock();
  12. }
  13. public function provideIterator()
  14. {
  15. return array(
  16. array(array('/foo1'), 'Node', 'nt:foo', 2),
  17. array(array('/foo1', '/foo2', '/foo3', '/foo4'), 'Node', 'nt:foo', 2),
  18. array(array('/foo1', '/foo2', '/foo3', '/foo4', '/foo5'), 'Node', 'nt:foo', 2),
  19. array(array('/foo1', '/foo2', '/foo3', '/foo4'), 'Node', 'nt:foo', 10),
  20. );
  21. }
  22. /**
  23. * @dataProvider provideIterator
  24. */
  25. public function testIterator($paths, $class, $filter, $batchSize)
  26. {
  27. $me = $this;
  28. $nbBatches = (integer) ceil(count($paths) / $batchSize);
  29. $this->objectManager->expects($this->exactly($nbBatches))
  30. ->method('getNodesByPathAsArray')
  31. ->will($this->returnCallback(function (
  32. $cPaths, $cClass, $cFilter
  33. ) use (
  34. $me, $class, $filter, $batchSize
  35. ) {
  36. $me->assertLessThanOrEqual($batchSize, count($cPaths));
  37. $nodes = array();
  38. $me->assertEquals($class, $cClass);
  39. $me->assertEquals($filter, $cFilter);
  40. foreach ($cPaths as $cPath) {
  41. $nodes[$cPath] = $me->getNodeMock();
  42. }
  43. return $nodes;
  44. }));
  45. $nodes = new NodePathIterator($this->objectManager, $paths, $class, $filter, $batchSize);
  46. foreach ($nodes as $node) {
  47. $this->assertInstanceOf('Jackalope\Node', $node);
  48. }
  49. }
  50. public function provideArrayAccess()
  51. {
  52. return array(
  53. // 1st target, batch size 2, 1 fetch
  54. array(
  55. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  56. 2,
  57. array('nb_fetches' => 1, 'target' => 'p1'),
  58. ),
  59. // 3rd target, batch size 2, 2 fetches
  60. array(
  61. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  62. 2,
  63. array('nb_fetches' => 2, 'target' => 'p3'),
  64. ),
  65. // 3rd target, batch size 1, 3 fetches
  66. array(
  67. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  68. 1,
  69. array('nb_fetches' => 3, 'target' => 'p3'),
  70. ),
  71. // test 0 paths
  72. array(
  73. array(),
  74. 2,
  75. array('nb_fetches' => 0),
  76. ),
  77. // test partial iteration
  78. array(
  79. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  80. 2,
  81. array('nb_fetches' => 2, 'target' => 'p4', 'iterate_result' => 3)
  82. ),
  83. array(
  84. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  85. 2,
  86. array('nb_fetches' => 4, 'target' => 'p4', 'iterate_result' => 8)
  87. ),
  88. // multiple targets
  89. array(
  90. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  91. 2,
  92. array('nb_fetches' => 3, 'target' => array('p1', 'p2', 'p3', 'p4', 'p5'))
  93. ),
  94. array(
  95. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  96. 2,
  97. array('nb_fetches' => 4, 'target' => array('p8', 'p1'))
  98. ),
  99. array(
  100. array('p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8'),
  101. 100,
  102. array('nb_fetches' => 1, 'target' => array('p8', 'p1'))
  103. ),
  104. );
  105. }
  106. /**
  107. * @dataProvider provideArrayAccess
  108. */
  109. public function testArrayAccess($paths, $batchSize, $options)
  110. {
  111. $options = array_merge(array(
  112. // number of times we expect to call the getNodesByArray method
  113. 'nb_fetches' => null,
  114. // node(s) paths we want to extract
  115. 'target' => null,
  116. // if specified, iterate the RS this many times
  117. 'iterate_result' => null,
  118. ), $options);
  119. $nbFetches = $options['nb_fetches'];
  120. $targets = (array) $options['target'];
  121. $iterateResult = $options['iterate_result'];
  122. $nodes = array();
  123. foreach ($paths as $path) {
  124. $node = $this->getNodeMock();
  125. $nodes[$path] = $node;
  126. }
  127. $this->objectManager->expects($this->exactly($nbFetches))
  128. ->method('getNodesByPathAsArray')
  129. ->will($this->returnCallback(function ($paths) use ($nodes) {
  130. $ret = array();
  131. foreach ($paths as $path) {
  132. $ret[$path] = $nodes[$path];
  133. }
  134. return $ret;
  135. }));
  136. $nodes = new NodePathIterator($this->objectManager, $paths, null, null, $batchSize);
  137. if ($iterateResult) {
  138. for ($i = 0; $i < $iterateResult; $i++) {
  139. // if its not valid its at the end of the stack ... probably
  140. if (false === $nodes->valid()) {
  141. continue;
  142. }
  143. $nodes->current($nodes);
  144. $nodes->next($nodes);
  145. }
  146. }
  147. $res = array();
  148. foreach ($targets as $target) {
  149. $res[$target] = $nodes[$target];
  150. }
  151. }
  152. }