MapReduceTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Collection\Iterator;
  15. use ArrayIterator;
  16. use Cake\Collection\Iterator\MapReduce;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests MapReduce class
  20. *
  21. */
  22. class MapReduceTest extends TestCase
  23. {
  24. /**
  25. * Tests the creation of an inversed index of words to documents using
  26. * MapReduce
  27. *
  28. * @return void
  29. */
  30. public function testInvertedIndexCreation()
  31. {
  32. $data = [
  33. 'document_1' => 'Dogs are the most amazing animal in history',
  34. 'document_2' => 'History is not only amazing but boring',
  35. 'document_3' => 'One thing that is not boring is dogs'
  36. ];
  37. $mapper = function ($row, $document, $mr) {
  38. $words = array_map('strtolower', explode(' ', $row));
  39. foreach ($words as $word) {
  40. $mr->emitIntermediate($document, $word);
  41. }
  42. };
  43. $reducer = function ($documents, $word, $mr) {
  44. $mr->emit(array_unique($documents), $word);
  45. };
  46. $results = new MapReduce(new ArrayIterator($data), $mapper, $reducer);
  47. $expected = [
  48. 'dogs' => ['document_1', 'document_3'],
  49. 'are' => ['document_1'],
  50. 'the' => ['document_1'],
  51. 'most' => ['document_1'],
  52. 'amazing' => ['document_1', 'document_2'],
  53. 'animal' => ['document_1'],
  54. 'in' => ['document_1'],
  55. 'history' => ['document_1', 'document_2'],
  56. 'is' => ['document_2', 'document_3'],
  57. 'not' => ['document_2', 'document_3'],
  58. 'only' => ['document_2'],
  59. 'but' => ['document_2'],
  60. 'boring' => ['document_2', 'document_3'],
  61. 'one' => ['document_3'],
  62. 'thing' => ['document_3'],
  63. 'that' => ['document_3']
  64. ];
  65. $this->assertEquals($expected, iterator_to_array($results));
  66. }
  67. /**
  68. * Tests that it is possible to use the emit function directly in the mapper
  69. *
  70. * @return void
  71. */
  72. public function testEmitFinalInMapper()
  73. {
  74. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  75. $mapper = function ($row, $key, $mr) {
  76. foreach ($row as $number) {
  77. $mr->emit($number);
  78. }
  79. };
  80. $results = new MapReduce(new ArrayIterator($data), $mapper);
  81. $expected = ['one', 'two', 'three', 'four'];
  82. $this->assertEquals($expected, iterator_to_array($results));
  83. }
  84. /**
  85. * Tests that a reducer is required when there are intermediate results
  86. *
  87. * @expectedException \LogicException
  88. * @return void
  89. */
  90. public function testReducerRequired()
  91. {
  92. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  93. $mapper = function ($row, $key, $mr) {
  94. foreach ($row as $number) {
  95. $mr->emitIntermediate('a', $number);
  96. }
  97. };
  98. $results = new MapReduce(new ArrayIterator($data), $mapper);
  99. iterator_to_array($results);
  100. }
  101. }