<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\TicketBundle\Tests\Entity;

use Chill\TicketBundle\Entity\Motive;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 *
 * @covers \Chill\TicketBundle\Entity\Motive::getWithDescendants
 */
final class MotiveTest extends TestCase
{
    public function testGetDescendantsOnLeafReturnsSelfOnly(): void
    {
        $leaf = new Motive();
        $leaf->setLabel(['fr' => 'Feuille']);

        $collection = $leaf->getDescendants();

        self::assertCount(1, $collection);
        self::assertSame($leaf, $collection->first());
        self::assertContains($leaf, $collection->toArray());
    }

    public function testGetWithDescendantsReturnsSelfAndAllDescendants(): void
    {
        $parent = new Motive();
        $parent->setLabel(['fr' => 'Parent']);

        $childA = new Motive();
        $childA->setLabel(['fr' => 'Enfant A']);
        $childA->setParent($parent);

        $childB = new Motive();
        $childB->setLabel(['fr' => 'Enfant B']);
        $childB->setParent($parent);

        $grandChildA1 = new Motive();
        $grandChildA1->setLabel(['fr' => 'Petit-enfant A1']);
        $grandChildA1->setParent($childA);

        $descendants = $parent->getDescendants();
        $asArray = $descendants->toArray();

        // It should contain the parent itself, both children and the grand child
        self::assertCount(4, $descendants, 'Expected parent + 2 children + 1 grandchild');
        self::assertContains($parent, $asArray);
        self::assertContains($childA, $asArray);
        self::assertContains($childB, $asArray);
        self::assertContains($grandChildA1, $asArray);
    }
}
