Completed
Push — master ( 22be11...a8965a )
by Claudio
16s queued 12s
created

TableGuesserTest::testValidTableData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 31
rs 9.6
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Flagbit\Bundle\TableAttributeBundle\Tests\Validator\ConstraintGuesser;
4
5
use Akeneo\Pim\Structure\Component\Model\AttributeInterface;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Flagbit\Bundle\TableAttributeBundle\Entity\AttributeOption;
8
use Flagbit\Bundle\TableAttributeBundle\Validator\Constraints\Table;
9
use Akeneo\Pim\Structure\Component\Model\Attribute;
10
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
11
12
class TableGuesserTest extends KernelTestCase
13
{
14
    public function testTableGuesserIsTaggedCorrectly(): void
15
    {
16
        self::bootKernel();
17
        $container = self::$container;
18
19
        $chainedAttributeConstraintGuesser = $container->get('pim_catalog.validator.constraint_guesser.chained_attribute');
20
21
        $attribute = new Attribute();
22
        $attribute->setType('flagbit_catalog_table');
23
24
        $constraintGuesser = $chainedAttributeConstraintGuesser->guessConstraints($attribute);
25
26
        self::assertCount(1, $constraintGuesser);
27
        self::assertInstanceOf(Table::class, $constraintGuesser[0]);
28
    }
29
30
    /**
31
     * @dataProvider provideValidTableValues
32
     */
33
    public function testValidTableData(string $tableValue): void
34
    {
35
        self::bootKernel();
36
        $container = self::$container;
37
38
        $guesser = $container->get('flagbit_table_attribute.validator.constraint_guesser.table');
39
        $validator = $container->get('validator');
40
41
        $jsonConfig = [
42
            'Type' => 'int',
43
            'Range' => ['max' => 10, 'min' => 1]
44
        ];
45
46
        $option1 = $this->createMock(AttributeOption::class);
47
        $option1->method('getConstraints')->willReturn($jsonConfig);
48
        $option1->method('getCode')->willReturn('foo');
49
        $option2 = $this->createMock(AttributeOption::class);
50
        // No constraints configured for "bar"
51
        $option2->method('getConstraints')->willReturn([]);
52
        $option2->method('getCode')->willReturn('bar');
53
        $collection = new ArrayCollection([$option1, $option2]);
54
55
        $attribute = $this->createMock(AttributeInterface::class);
56
        $attribute->method('getType')->willReturn('flagbit_catalog_table');
57
        $attribute->method('getOptions')->willReturn($collection);
58
59
        $constraints = $guesser->guessConstraints($attribute);
60
61
        $violations = $validator->validate($tableValue, $constraints);
62
63
        self::assertCount(0, $violations);
64
    }
65
66
    public function provideValidTableValues(): array
67
    {
68
        return [
69
            'complete' => ['[{"foo": 5, "bar": "text"},{"foo": 10, "bar": "text2"}]'],
70
            'missing bar field' => ['[{"foo": 5}]'],
71
            'completely empty' => ['[]'],
72
        ];
73
    }
74
}
75