File size: 3,115 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
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
86
87
88
89
90
91
92
93
94
<?php

declare(strict_types=1);

namespace Mautic\FormBundle\Tests\Collector;

use Mautic\CacheBundle\Cache\CacheProviderInterface;
use Mautic\FormBundle\Collector\AlreadyMappedFieldCollector;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Cache\CacheItem;

final class AlreadyMappedFieldCollectorTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var MockObject&CacheProviderInterface
     */
    private MockObject $cacheProvider;

    private AlreadyMappedFieldCollector $collector;

    protected function setup(): void
    {
        parent::setUp();

        $this->cacheProvider = $this->createMock(CacheProviderInterface::class);
        $this->collector     = new AlreadyMappedFieldCollector($this->cacheProvider);
    }

    public function testWorkflow(): void
    {
        $createCacheItem = \Closure::bind(
            function () {
                $item             = new CacheItem();
                $item->isHit      = false;
                $item->isTaggable = true;

                return $item;
            },
            $this,
            CacheItem::class
        );
        $cacheItem = $createCacheItem();

        $formId    = '3';
        $object    = 'contact';

        $this->cacheProvider->method('getItem')
            ->with('mautic.form.3.object.contact.fields.mapped')
            ->willReturn($cacheItem);

        $this->cacheProvider->expects($this->exactly(4))
            ->method('save')
            ->with($cacheItem);

        // Ensure we get an empty array at the beginning.
        $this->assertNull($cacheItem->get());
        $this->assertSame([], $this->collector->getFields($formId, $object));

        // Add a mapped field.
        $this->collector->addField('3', 'contact', '44');
        $this->assertSame(['44'], $this->collector->getFields($formId, $object));

        // The field with key 44 should be added to the cache item.
        $this->assertSame('["44"]', $cacheItem->get());

        // Add another mapped field.
        $this->collector->addField('3', 'contact', '55');

        // The field with key 55 should be added to the cache item.
        $this->assertSame('["44","55"]', $cacheItem->get());
        $this->assertSame(['44', '55'], $this->collector->getFields($formId, $object));

        // Remove an exsting field.
        $this->collector->removeField('3', 'contact', '44');

        // The field with key 44 should be removed from the cache item.
        $this->assertSame('["55"]', $cacheItem->get());
        $this->assertSame(['55'], $this->collector->getFields($formId, $object));

        // Remove a not exsting field.
        $this->collector->removeField('3', 'contact', '44');

        // Still the same result after removing a field that did not exist.
        $this->assertSame('["55"]', $cacheItem->get());
        $this->assertSame(['55'], $this->collector->getFields($formId, $object));

        $this->cacheProvider->expects($this->once())
            ->method('invalidateTags')
            ->with(['mautic.form.3.fields.mapped']);

        $this->collector->removeAllForForm($formId);
    }
}