-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Add tests for
LazyCollection
constructor laziness (#36846)
* Move enumeration counting to trait * Add tests for `LazyCollection` constructor laziness
- Loading branch information
1 parent
b92d7b9
commit 79ffd4c
Showing
2 changed files
with
124 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Support\Concerns; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\LazyCollection; | ||
|
||
trait CountsEnumerations | ||
{ | ||
protected function makeGeneratorFunctionWithRecorder($numbers = 10) | ||
{ | ||
$recorder = new Collection(); | ||
|
||
$generatorFunction = function () use ($numbers, $recorder) { | ||
for ($i = 1; $i <= $numbers; $i++) { | ||
$recorder->push($i); | ||
|
||
yield $i; | ||
} | ||
}; | ||
|
||
return [$generatorFunction, $recorder]; | ||
} | ||
|
||
protected function assertDoesNotEnumerate(callable $executor) | ||
{ | ||
$this->assertEnumerates(0, $executor); | ||
} | ||
|
||
protected function assertDoesNotEnumerateCollection( | ||
LazyCollection $collection, | ||
callable $executor | ||
) { | ||
$this->assertEnumeratesCollection($collection, 0, $executor); | ||
} | ||
|
||
protected function assertEnumerates($count, callable $executor) | ||
{ | ||
$this->assertEnumeratesCollection( | ||
LazyCollection::times(100), | ||
$count, | ||
$executor | ||
); | ||
} | ||
|
||
protected function assertEnumeratesCollection( | ||
LazyCollection $collection, | ||
$count, | ||
callable $executor | ||
) { | ||
$enumerated = 0; | ||
|
||
$data = $this->countEnumerations($collection, $enumerated); | ||
|
||
$executor($data); | ||
|
||
$this->assertEnumerations($count, $enumerated); | ||
} | ||
|
||
protected function assertEnumeratesOnce(callable $executor) | ||
{ | ||
$this->assertEnumeratesCollectionOnce(LazyCollection::times(10), $executor); | ||
} | ||
|
||
protected function assertEnumeratesCollectionOnce( | ||
LazyCollection $collection, | ||
callable $executor | ||
) { | ||
$enumerated = 0; | ||
$count = $collection->count(); | ||
$collection = $this->countEnumerations($collection, $enumerated); | ||
|
||
$executor($collection); | ||
|
||
$this->assertEquals( | ||
$count, | ||
$enumerated, | ||
$count > $enumerated ? 'Failed to enumerate in full.' : 'Enumerated more than once.' | ||
); | ||
} | ||
|
||
protected function assertEnumerations($expected, $actual) | ||
{ | ||
$this->assertEquals( | ||
$expected, | ||
$actual, | ||
"Failed asserting that {$actual} items that were enumerated matches expected {$expected}." | ||
); | ||
} | ||
|
||
protected function countEnumerations(LazyCollection $collection, &$count) | ||
{ | ||
return $collection->tapEach(function () use (&$count) { | ||
$count++; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters