MapReduceTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. * Tests the creation of an inversed index of words to documents using
  25. * MapReduce
  26. *
  27. * @return void
  28. */
  29. public function testInvertedIndexCreation() {
  30. $data = [
  31. 'document_1' => 'Dogs are the most amazing animal in history',
  32. 'document_2' => 'History is not only amazing but boring',
  33. 'document_3' => 'One thing that is not boring is dogs'
  34. ];
  35. $mapper = function($row, $document, $mr) {
  36. $words = array_map('strtolower', explode(' ', $row));
  37. foreach ($words as $word) {
  38. $mr->emitIntermediate($document, $word);
  39. }
  40. };
  41. $reducer = function($documents, $word, $mr) {
  42. $mr->emit(array_unique($documents), $word);
  43. };
  44. $results = new MapReduce(new ArrayIterator($data), $mapper, $reducer);
  45. $expected = [
  46. 'dogs' => ['document_1', 'document_3'],
  47. 'are' => ['document_1'],
  48. 'the' => ['document_1'],
  49. 'most' => ['document_1'],
  50. 'amazing' => ['document_1', 'document_2'],
  51. 'animal' => ['document_1'],
  52. 'in' => ['document_1'],
  53. 'history' => ['document_1', 'document_2'],
  54. 'is' => ['document_2', 'document_3'],
  55. 'not' => ['document_2', 'document_3'],
  56. 'only' => ['document_2'],
  57. 'but' => ['document_2'],
  58. 'boring' => ['document_2', 'document_3'],
  59. 'one' => ['document_3'],
  60. 'thing' => ['document_3'],
  61. 'that' => ['document_3']
  62. ];
  63. $this->assertEquals($expected, iterator_to_array($results));
  64. }
  65. /**
  66. * Tests that it is possible to use the emit function directly in the mapper
  67. *
  68. * @return void
  69. */
  70. public function testEmitFinalInMapper() {
  71. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  72. $mapper = function ($row, $key, $mr) {
  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. * @expectedException \LogicException
  85. * @return void
  86. */
  87. public function testReducerRequired() {
  88. $data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
  89. $mapper = function ($row, $key, $mr) {
  90. foreach ($row as $number) {
  91. $mr->emitIntermediate('a', $number);
  92. }
  93. };
  94. $results = new MapReduce(new ArrayIterator($data), $mapper);
  95. iterator_to_array($results);
  96. }
  97. }