SupportLazyCollectionTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\LazyCollection;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. class SupportLazyCollectionTest extends TestCase
  9. {
  10. public function testCanCreateEmptyCollection()
  11. {
  12. $this->assertSame([], LazyCollection::make()->all());
  13. $this->assertSame([], LazyCollection::empty()->all());
  14. }
  15. public function testCanCreateCollectionFromArray()
  16. {
  17. $array = [1, 2, 3];
  18. $data = LazyCollection::make($array);
  19. $this->assertSame($array, $data->all());
  20. $array = ['a' => 1, 'b' => 2, 'c' => 3];
  21. $data = LazyCollection::make($array);
  22. $this->assertSame($array, $data->all());
  23. }
  24. public function testCanCreateCollectionFromArrayable()
  25. {
  26. $array = [1, 2, 3];
  27. $data = LazyCollection::make(Collection::make($array));
  28. $this->assertSame($array, $data->all());
  29. $array = ['a' => 1, 'b' => 2, 'c' => 3];
  30. $data = LazyCollection::make(Collection::make($array));
  31. $this->assertSame($array, $data->all());
  32. }
  33. public function testCanCreateCollectionFromClosure()
  34. {
  35. $data = LazyCollection::make(function () {
  36. yield 1;
  37. yield 2;
  38. yield 3;
  39. });
  40. $this->assertSame([1, 2, 3], $data->all());
  41. $data = LazyCollection::make(function () {
  42. yield 'a' => 1;
  43. yield 'b' => 2;
  44. yield 'c' => 3;
  45. });
  46. $this->assertSame([
  47. 'a' => 1,
  48. 'b' => 2,
  49. 'c' => 3,
  50. ], $data->all());
  51. }
  52. public function testEager()
  53. {
  54. $source = [1, 2, 3, 4, 5];
  55. $data = LazyCollection::make(function () use (&$source) {
  56. yield from $source;
  57. })->eager();
  58. $source[] = 6;
  59. $this->assertSame([1, 2, 3, 4, 5], $data->all());
  60. }
  61. public function testRemember()
  62. {
  63. $source = [1, 2, 3, 4];
  64. $collection = LazyCollection::make(function () use (&$source) {
  65. yield from $source;
  66. })->remember();
  67. $this->assertSame([1, 2, 3, 4], $collection->all());
  68. $source = [];
  69. $this->assertSame([1, 2, 3, 4], $collection->all());
  70. }
  71. public function testRememberWithTwoRunners()
  72. {
  73. $source = [1, 2, 3, 4];
  74. $collection = LazyCollection::make(function () use (&$source) {
  75. yield from $source;
  76. })->remember();
  77. $a = $collection->getIterator();
  78. $b = $collection->getIterator();
  79. $this->assertEquals(1, $a->current());
  80. $this->assertEquals(1, $b->current());
  81. $b->next();
  82. $this->assertEquals(1, $a->current());
  83. $this->assertEquals(2, $b->current());
  84. $b->next();
  85. $this->assertEquals(1, $a->current());
  86. $this->assertEquals(3, $b->current());
  87. $a->next();
  88. $this->assertEquals(2, $a->current());
  89. $this->assertEquals(3, $b->current());
  90. $a->next();
  91. $this->assertEquals(3, $a->current());
  92. $this->assertEquals(3, $b->current());
  93. $a->next();
  94. $this->assertEquals(4, $a->current());
  95. $this->assertEquals(3, $b->current());
  96. $b->next();
  97. $this->assertEquals(4, $a->current());
  98. $this->assertEquals(4, $b->current());
  99. }
  100. public function testRememberWithDuplicateKeys()
  101. {
  102. $collection = LazyCollection::make(function () {
  103. yield 'key' => 1;
  104. yield 'key' => 2;
  105. })->remember();
  106. $results = $collection->map(function ($value, $key) {
  107. return [$key, $value];
  108. })->values()->all();
  109. $this->assertSame([['key', 1], ['key', 2]], $results);
  110. }
  111. public function testTakeUntilTimeout()
  112. {
  113. $timeout = Carbon::now();
  114. $mock = m::mock(LazyCollection::class.'[now]');
  115. $results = $mock
  116. ->times(10)
  117. ->pipe(function ($collection) use ($mock, $timeout) {
  118. tap($collection)
  119. ->mockery_init($mock->mockery_getContainer())
  120. ->shouldAllowMockingProtectedMethods()
  121. ->shouldReceive('now')
  122. ->times(3)
  123. ->andReturn(
  124. (clone $timeout)->sub(2, 'minute')->getTimestamp(),
  125. (clone $timeout)->sub(1, 'minute')->getTimestamp(),
  126. $timeout->getTimestamp()
  127. );
  128. return $collection;
  129. })
  130. ->takeUntilTimeout($timeout)
  131. ->all();
  132. $this->assertSame([1, 2], $results);
  133. m::close();
  134. }
  135. public function testTapEach()
  136. {
  137. $data = LazyCollection::times(10);
  138. $tapped = [];
  139. $data = $data->tapEach(function ($value, $key) use (&$tapped) {
  140. $tapped[$key] = $value;
  141. });
  142. $this->assertEmpty($tapped);
  143. $data = $data->take(5)->all();
  144. $this->assertSame([1, 2, 3, 4, 5], $data);
  145. $this->assertSame([1, 2, 3, 4, 5], $tapped);
  146. }
  147. public function testUniqueDoubleEnumeration()
  148. {
  149. $data = LazyCollection::times(2)->unique();
  150. $data->all();
  151. $this->assertSame([1, 2], $data->all());
  152. }
  153. }