MapReduceTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @license https://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. class MapReduceTest extends TestCase
  22. {
  23. /**
  24. * Tests the creation of an inversed index of words to documents using
  25. * MapReduce
  26. *
  27. * @return void
  28. */
  29. public function testInvertedIndexCreation()
  30. {
  31. $data = [
  32. 'document_1' => 'Dogs are the most amazing animal in history',
  33. 'document_2' => 'History is not only amazing but boring',
  34. 'document_3' => 'One thing that is not boring is dogs',
  35. ];
  36. $mapper = function ($row, $document, $mr) {
  37. $words = array_map('strtolower', explode(' ', $row));
  38. foreach ($words as $word) {
  39. $mr->emitIntermediate($document, $word);
  40. }
  41. };
  42. $reducer = function ($documents, $word, $mr) {
  43. $mr->emit(array_unique($documents), $word);
  44. };
  45. $results = new MapReduce(new ArrayIterator($data), $mapper, $reducer);
  46. $expected = [
  47. 'dogs' => ['document_1', 'document_3'],
  48. 'are' => ['document_1'],
  49. 'the' => ['document_1'],
  50. 'most' => ['document_1'],
  51. 'amazing' => ['document_1', 'document_2'],
  52. 'animal' => ['document_1'],
  53. 'in' => ['document_1'],
  54. 'history' => ['document_1', 'document_2'],
  55. 'is' => ['document_2', 'document_3'],
  56. 'not' => ['document_2', 'document_3'],
  57. 'only' => ['document_2'],
  58. 'but' => ['document_2'],
  59. 'boring' => ['document_2', 'document_3'],
  60. 'one' => ['document_3'],
  61. 'thing' => ['document_3'],
  62. 'that' => ['document_3'],
  63. ];
  64. $this->assertEquals($expected, iterator_to_array($results));
  65. }
  66. /**
  67. * Tests that it is possible to use the emit function directly in the mapper
  68. *
  69. * @return void
  70. */
  71. public function testEmitFinalInMapper()
  72. {
  73. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  74. $mapper = function ($row, $key, $mr) {
  75. foreach ($row as $number) {
  76. $mr->emit($number);
  77. }
  78. };
  79. $results = new MapReduce(new ArrayIterator($data), $mapper);
  80. $expected = ['one', 'two', 'three', 'four'];
  81. $this->assertEquals($expected, iterator_to_array($results));
  82. }
  83. /**
  84. * Tests that a reducer is required when there are intermediate results
  85. *
  86. * @return void
  87. */
  88. public function testReducerRequired()
  89. {
  90. $this->expectException(\LogicException::class);
  91. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  92. $mapper = function ($row, $key, $mr) {
  93. foreach ($row as $number) {
  94. $mr->emitIntermediate('a', $number);
  95. }
  96. };
  97. $results = new MapReduce(new ArrayIterator($data), $mapper);
  98. iterator_to_array($results);
  99. }
  100. }