MapReduceTest.php 3.1 KB

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