File size: 2,841 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
<?php

declare(strict_types=1);

namespace Mautic\FormBundle\Tests\Collection;

use Mautic\FormBundle\Collection\FieldCollection;
use Mautic\FormBundle\Crate\FieldCrate;
use Mautic\FormBundle\Exception\FieldNotFoundException;

final class FieldCollectionTest extends \PHPUnit\Framework\TestCase
{
    public function testToChoicesWithObjects(): void
    {
        $collection = new FieldCollection(
            [
                new FieldCrate('6', 'email', 'email', []),
                new FieldCrate('7', 'first_name', 'text', []),
            ]
        );

        $this->assertSame(
            [
                'email'      => '6',
                'first_name' => '7',
            ],
            $collection->toChoices()
        );
    }

    public function testToChoicesWithoutObjects(): void
    {
        $collection = new FieldCollection();

        $this->assertSame([], $collection->toChoices());
    }

    public function testGetFieldByKey(): void
    {
        $field6     = new FieldCrate('6', 'email', 'email', []);
        $field7     = new FieldCrate('7', 'first_name', 'text', []);
        $collection = new FieldCollection([$field6, $field7]);

        $this->assertSame($field6, $collection->getFieldByKey('6'));
        $this->assertSame($field7, $collection->getFieldByKey('7'));

        $this->expectException(FieldNotFoundException::class);
        $collection->getFieldByKey('8');
    }

    public function testRemoveFieldsWithKeysWithNoKeyToKeep(): void
    {
        $field6             = new FieldCrate('6', 'email', 'email', []);
        $field7             = new FieldCrate('7', 'first_name', 'text', []);
        $field8             = new FieldCrate('8', 'last_name', 'text', []);
        $originalCollection = new FieldCollection([$field6, $field7, $field8]);
        $resultCollection   = $originalCollection->removeFieldsWithKeys(['6', '8']);

        // It should return a clone of the original collection. Not mutation.
        $this->assertNotSame($originalCollection, $resultCollection);
        $this->assertCount(1, $resultCollection);
        $this->assertSame($field7, $resultCollection->getFieldByKey('7'));
    }

    public function testRemoveFieldsWithKeysWithKeyToKeep(): void
    {
        $field6             = new FieldCrate('6', 'email', 'email', []);
        $field7             = new FieldCrate('7', 'first_name', 'text', []);
        $field8             = new FieldCrate('8', 'last_name', 'text', []);
        $originalCollection = new FieldCollection([$field6, $field7, $field8]);
        $resultCollection   = $originalCollection->removeFieldsWithKeys(['6', '8'], '8');

        $this->assertCount(2, $resultCollection);
        $this->assertSame($field7, $resultCollection->getFieldByKey('7'));
        $this->assertSame($field8, $resultCollection->getFieldByKey('8'));
    }
}