raph007/phpunit-doctrine-collections-asserts

PHPUnit asserts for Doctrine Collections

v0.1.0 2020-09-06 11:28 UTC

This package is not auto-updated.

Last update: 2024-05-14 05:20:20 UTC


README

This extension allows asserting Doctrine Collections by given predicates in tests.

Installation

Install it via Composer:

composer require --dev raph007/phpunit-doctrine-collections-asserts

Usage

Use DoctrineCollectionsAsserts:

<?php

declare(strict_types=1);

namespace Raph\PHPUnitExtensions\DoctrineCollectionsAsserts\Tests;

use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
use Raph\PHPUnitExtensions\DoctrineCollectionsAsserts\DoctrineCollectionsAsserts;

class DoctrineCollectionsAssertsTest extends TestCase
{
    use DoctrineCollectionsAsserts;

    public function testAssertCollectionItemExistsThatSatisfiesPredicate(): void
    {
        $collection = new ArrayCollection(['key1' => 'value1', 'key2' => 'value2']);

        self::assertCollectionItemExistsThatSatisfiesPredicate(
            $collection,
            function (string $key, string $value) {
                return $key === 'key1' && $value === 'value1';
            }
        );
    }

    public function testAssertWholeCollectionSatisfiesPredicate(): void
        {
            $collection = new ArrayCollection([0, 8, 5, 3]);
    
            self::assertWholeCollectionSatisfiesPredicate(
                $collection,
                function (int $key, int $value) {
                    return $value >= 0;
                }
            );
        }
}