1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
<?php
// phpcs:disable
declare(strict_types=1);
namespace Ramsey\Collection\Test\types;
use Ramsey\Collection\Collection;
use Ramsey\Collection\Test\Mock\Person;
use function PHPStan\Testing\assertType;
$jane = new Person('Jane');
$john = new Person('John');
$janice = new Person('Janice');
$persons = new Collection(Person::class, [$jane, $john]);
$morePersons = new Collection(Person::class, [$john, $janice]);
assertType('Ramsey\Collection\Collection<Ramsey\Collection\Test\Mock\Person>', $persons);
assertType('Ramsey\Collection\Collection<Ramsey\Collection\Test\Mock\Person>', $morePersons);
assertType(Person::class, $persons[0]);
assertType('array<int, mixed>', $persons->column('name'));
assertType(Person::class, $persons->first());
assertType(Person::class, $persons->last());
assertType(Person::class, $persons->offsetGet(0));
assertType('array<Ramsey\Collection\Test\Mock\Person>', $persons->toArray());
assertType('array<Ramsey\Collection\Test\Mock\Person>', $persons->__serialize());
assertType('Traversable<(int|string), Ramsey\Collection\Test\Mock\Person>', $persons->getIterator());
foreach ($persons as $person) {
assertType(Person::class, $person);
}
assertType(
'Ramsey\Collection\CollectionInterface<Ramsey\Collection\Test\Mock\Person>',
$persons->sort(),
);
assertType(
'Ramsey\Collection\CollectionInterface<Ramsey\Collection\Test\Mock\Person>',
$persons->filter(fn (Person $person): bool => $person->name === 'Jane'),
);
assertType(
'Ramsey\Collection\CollectionInterface<Ramsey\Collection\Test\Mock\Person>',
$persons->where('name', 'Jane'),
);
assertType(
'Ramsey\Collection\CollectionInterface<string>',
$persons->map(fn (Person $person): string => $person->name),
);
assertType(
'Ramsey\Collection\CollectionInterface<bool>',
$persons->map(fn (Person $person): bool => isset($person->name)),
);
assertType(
'string',
$persons->reduce(fn (string $name, Person $person): string => "$name, $person->name", ''),
);
assertType(
'bool',
$persons->reduce(fn (bool $carry, Person $person): bool => $carry && isset($person->name), true),
);
assertType(
'Ramsey\Collection\CollectionInterface<Ramsey\Collection\Test\Mock\Person>',
$persons->diff($morePersons),
);
assertType(
'Ramsey\Collection\CollectionInterface<Ramsey\Collection\Test\Mock\Person>',
$persons->intersect($morePersons),
);
assertType(
'Ramsey\Collection\CollectionInterface<Ramsey\Collection\Test\Mock\Person>',
$persons->merge($morePersons),
);
|