Completed
Pull Request — 3.x (#5937)
by Peter
05:42 queued 01:36
created

BooleanToStringTransformerTest::testTransform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Form\DataTransformer;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Form\DataTransformer\BooleanToStringTransformer;
18
19
/**
20
 * @author Peter Gribanov <[email protected]>
21
 */
22
final class BooleanToStringTransformerTest extends TestCase
23
{
24
    public function provideTransform(): array
25
    {
26
        return [
27
            [null, null, '1'],
28
            [false, null, '1'],
29
            [true, '1', '1'],
30
            [true, 'true', 'true'],
31
            [true, 'yes', 'yes'],
32
            [true, 'on', 'on'],
33
        ];
34
    }
35
36
    /**
37
     * @dataProvider provideTransform
38
     */
39
    public function testTransform($value, ?string $expected, string $trueValue): void
40
    {
41
        $transformer = new BooleanToStringTransformer($trueValue);
42
43
        $this->assertSame($expected, $transformer->transform($value));
44
    }
45
46
    public function provideReverseTransform(): array
47
    {
48
        return [
49
            [null, false],
50
            [true, true],
51
            [1, true],
52
            ['1', true],
53
            ['true', true],
54
            ['yes', true],
55
            ['on', true],
56
            [false, false],
57
            [0, false],
58
            ['0', false],
59
            ['false', false],
60
            ['no', false],
61
            ['off', false],
62
            ['', false],
63
            // invalid values
64
            ['foo', false],
65
            [new \DateTime(), false],
66
            [PHP_INT_MAX, false],
67
        ];
68
    }
69
70
    /**
71
     * @dataProvider provideReverseTransform
72
     */
73
    public function testReverseTransform($value, bool $expected): void
74
    {
75
        $transformer = new BooleanToStringTransformer('1');
76
77
        $this->assertSame($expected, $transformer->reverseTransform($value));
78
    }
79
}
80