Completed
Push — 3.x ( 846f8c...b16427 )
by Grégoire
02:51
created

UnicodeStringTest::testAscii()   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 0
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\Twig\Extension;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Twig\Extension\UnicodeString;
18
use Symfony\Component\String\Exception\InvalidArgumentException;
19
20
/**
21
 * @author Javier Spagnoletti <[email protected]>
22
 */
23
final class UnicodeStringTest extends TestCase
24
{
25
    public function testCreateFromStringWithInvalidUtf8Input(): void
26
    {
27
        $this->expectException(InvalidArgumentException::class);
28
29
        static::createFromString("\xE9");
0 ignored issues
show
Comprehensibility introduced by
Since Sonata\AdminBundle\Tests...nsion\UnicodeStringTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
30
    }
31
32
    public function testAscii(): void
33
    {
34
        $s = static::createFromString('Dieser Wert sollte größer oder gleich');
0 ignored issues
show
Comprehensibility introduced by
Since Sonata\AdminBundle\Tests...nsion\UnicodeStringTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
35
        $this->assertSame('Dieser Wert sollte grosser oder gleich', (string) $s->ascii());
0 ignored issues
show
Documentation Bug introduced by
The method ascii does not exist on object<Sonata\AdminBundl...xtension\UnicodeString>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
36
        $this->assertSame('Dieser Wert sollte groesser oder gleich', (string) $s->ascii(['de-ASCII']));
0 ignored issues
show
Documentation Bug introduced by
The method ascii does not exist on object<Sonata\AdminBundl...xtension\UnicodeString>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
    }
38
39
    /**
40
     * @dataProvider provideTruncate
41
     */
42
    public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $preserve = false): void
43
    {
44
        $instance = static::createFromString($origin)->truncate($length, $ellipsis, $preserve);
0 ignored issues
show
Comprehensibility introduced by
Since Sonata\AdminBundle\Tests...nsion\UnicodeStringTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
45
46
        $this->assertSame((string) static::createFromString($expected), (string) $instance);
0 ignored issues
show
Comprehensibility introduced by
Since Sonata\AdminBundle\Tests...nsion\UnicodeStringTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
47
    }
48
49
    public static function provideTruncate(): iterable
50
    {
51
        yield from [
52
            ['', '', 3, ''],
53
            ['', 'foo', 0, '...'],
54
            ['foo', 'foo', 0, '...', true],
55
            ['fo', 'foobar', 2, ''],
56
            ['foobar', 'foobar', 10, ''],
57
            ['foobar', 'foobar', 10, '...', true],
58
            ['foo', 'foo', 3, '...'],
59
            ['fo', 'foobar', 2, '...'],
60
            ['...', 'foobar', 3, '...'],
61
            ['fo...', 'foobar', 5, '...'],
62
            ['foobar...', 'foobar foo', 6, '...', true],
63
            ['foobar...', 'foobar foo', 7, '...', true],
64
            ['foobar foo...', 'foobar foo a', 10, '...', true],
65
        ];
66
    }
67
68
    /**
69
     * @dataProvider wordwrapProvider
70
     */
71
    public function testWordwrap(string $expected, string $actual, int $length, string $break, bool $cut = false): void
72
    {
73
        $instance = static::createFromString($actual);
0 ignored issues
show
Comprehensibility introduced by
Since Sonata\AdminBundle\Tests...nsion\UnicodeStringTest is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
74
        $actual = $instance->wordwrap($length, $break, $cut);
0 ignored issues
show
Documentation Bug introduced by
The method wordwrap does not exist on object<Sonata\AdminBundl...xtension\UnicodeString>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
76
        $this->assertSame((string) $expected, (string) $actual);
77
    }
78
79
    public function wordwrapProvider(): iterable
80
    {
81
        yield from [
82
            [
83
                'Lo-re-m-Ip-su-m',
84
                'Lorem Ipsum',
85
                2,
86
                '-',
87
                true,
88
            ],
89
            [
90
                'Lorem-Ipsum',
91
                'Lorem Ipsum',
92
                2,
93
                '-',
94
            ],
95
            [
96
                'Lor-em-Ips-um',
97
                'Lorem Ipsum',
98
                3,
99
                '-',
100
                true,
101
            ],
102
            [
103
                'L-o-r-e-m-I-p-s-u-m',
104
                'Lorem Ipsum',
105
                1,
106
                '-',
107
                true,
108
            ],
109
        ];
110
    }
111
112
    private static function createFromString(string $string): UnicodeString
113
    {
114
        return new UnicodeString($string);
115
    }
116
}
117