MapReduceTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Collection\Iterator;
  16. use ArrayIterator;
  17. use Cake\Collection\Iterator\MapReduce;
  18. use Cake\TestSuite\TestCase;
  19. use LogicException;
  20. /**
  21. * Tests MapReduce class
  22. */
  23. class MapReduceTest extends TestCase
  24. {
  25. /**
  26. * Tests the creation of an inversed index of words to documents using
  27. * MapReduce
  28. */
  29. public function testInvertedIndexCreation(): void
  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): void {
  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): void {
  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. public function testEmitFinalInMapper(): void
  70. {
  71. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  72. $mapper = function ($row, $key, $mr): void {
  73. foreach ($row as $number) {
  74. $mr->emit($number);
  75. }
  76. };
  77. $results = new MapReduce(new ArrayIterator($data), $mapper);
  78. $expected = ['one', 'two', 'three', 'four'];
  79. $this->assertEquals($expected, iterator_to_array($results));
  80. }
  81. /**
  82. * Tests that a reducer is required when there are intermediate results
  83. */
  84. public function testReducerRequired(): void
  85. {
  86. $this->expectException(LogicException::class);
  87. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  88. $mapper = function ($row, $key, $mr): void {
  89. foreach ($row as $number) {
  90. $mr->emitIntermediate('a', $number);
  91. }
  92. };
  93. $results = new MapReduce(new ArrayIterator($data), $mapper);
  94. iterator_to_array($results);
  95. }
  96. }