File size: 1,342 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace Mautic\FormBundle\Tests\Collector;

use Mautic\FormBundle\Collection\ObjectCollection;
use Mautic\FormBundle\Collector\ObjectCollector;
use Mautic\FormBundle\Event\ObjectCollectEvent;
use PHPUnit\Framework\Assert;
use Symfony\Component\EventDispatcher\EventDispatcher;

final class ObjectCollectorTest extends \PHPUnit\Framework\TestCase
{
    public function testBuildCollectionForNoObject(): void
    {
        $dispatcher                               = new class() extends EventDispatcher {
            public int $dispatchMethodCallCounter = 0;

            public function dispatch(object $event, string $eventName = null): object
            {
                ++$this->dispatchMethodCallCounter;

                Assert::assertInstanceOf(ObjectCollectEvent::class, $event);

                return new ObjectCollection();
            }
        };

        $objectCollector  = new ObjectCollector($dispatcher);
        $objectCollection = $objectCollector->getObjects();

        // Calling for the second time to ensure it's cached and the dispatcher is called only once.
        $objectCollection = $objectCollector->getObjects();

        Assert::assertInstanceOf(ObjectCollection::class, $objectCollection);
        Assert::assertEquals(1, $dispatcher->dispatchMethodCallCounter);
    }
}