Open
Description
opened on Dec 11, 2023
Description
Original Thread: https://discourse.cakephp.org/t/extended-collection-class-not-behaving-the-same-as-base-collection-class/11734
As can be seen in the mentioned forum link (thanks @Zuluru) if one extends a collection it behaves differently (in this specific case it causes an infinite loop)
I'll just copy/paste the example in here:
<?php
namespace App\Test\TestCase;
use Cake\Collection\Collection;
use PHPUnit\Framework\TestCase;
class MyCollection extends Collection { }
class CollectionTest extends TestCase
{
public function testIterators(): void {
$records = [
['field' => true],
['field' => false],
['field' => true],
];
$collection = new MyCollection($records);
$count = 0;
foreach ($collection as $investment) {
$count ++;
if ($count > 20) {
break;
}
if ($collection->every(fn ($record) => $record['field'] === true)) {
// Nothing, really, just need to execute the every() call
}
}
$this->assertEquals(3, $count);
}
}
if you change new MyCollection
to new Collection
it works just fine.
My guess is related to this line: https://github.com/cakephp/cakephp/blob/5.x/src/Collection/CollectionTrait.php#L937
But I don't use collections and have not yet doven into this black magic.
CakePHP Version
5.0.3
PHP Version
8.2
Activity