File: ProcessorTest.php

package info (click to toggle)
php-tijsverkoyen-css-to-inline-styles 2.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 340 kB
  • sloc: php: 1,105; makefile: 9
file content (92 lines) | stat: -rw-r--r-- 2,899 bytes parent folder | download | duplicates (2)
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
<?php

namespace TijsVerkoyen\CssToInlineStyles\Tests\Css\Rule;

use Symfony\Component\CssSelector\Node\Specificity;
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Processor;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\TestCase;

class ProcessorTest extends TestCase
{
    /**
     * @var Processor
     */
    protected $processor;

    /**
     * @before
     */
    #[Before()]
    protected function prepare(): void
    {
        $this->processor = new Processor();
    }

    public function testMostBasicRule(): void
    {
        $css = <<<EOF
            a {
                padding: 5px;
                display: block;
            }
EOF;

        $rules = $this->processor->convertToObjects($css, 1);

        $this->assertCount(1, $rules);
        $this->assertInstanceOf('TijsVerkoyen\CssToInlineStyles\Css\Rule\Rule', $rules[0]);
        $this->assertEquals('a', $rules[0]->getSelector());
        $this->assertCount(2, $rules[0]->getProperties());
        $this->assertEquals('padding', $rules[0]->getProperties()[0]->getName());
        $this->assertEquals('5px', $rules[0]->getProperties()[0]->getValue());
        $this->assertEquals('display', $rules[0]->getProperties()[1]->getName());
        $this->assertEquals('block', $rules[0]->getProperties()[1]->getValue());
        $this->assertEquals(1, $rules[0]->getOrder());
    }

    public function testMaintainOrderOfProperties(): void
    {
        $css = <<<EOF
            div {
                width: 200px;
                _width: 211px;
            }
EOF;
        $rules = $this->processor->convertToObjects($css, 1);

        $this->assertCount(1, $rules);
        $this->assertInstanceOf('TijsVerkoyen\CssToInlineStyles\Css\Rule\Rule', $rules[0]);
        $this->assertEquals('div', $rules[0]->getSelector());
        $this->assertCount(2, $rules[0]->getProperties());
        $this->assertEquals('width', $rules[0]->getProperties()[0]->getName());
        $this->assertEquals('200px', $rules[0]->getProperties()[0]->getValue());
        $this->assertEquals('_width', $rules[0]->getProperties()[1]->getName());
        $this->assertEquals('211px', $rules[0]->getProperties()[1]->getValue());
        $this->assertEquals(1, $rules[0]->getOrder());
    }

    public function testSingleIdSelector(): void
    {
        $this->assertEquals(
            new Specificity(1, 0, 0),
            $this->processor->calculateSpecificityBasedOnASelector('#foo')
        );
    }

    public function testSingleClassSelector(): void
    {
        $this->assertEquals(
            new Specificity(0, 1, 0),
            $this->processor->calculateSpecificityBasedOnASelector('.foo')
        );
    }

    public function testSingleElementSelector(): void
    {
        $this->assertEquals(
            new Specificity(0, 0, 1),
            $this->processor->calculateSpecificityBasedOnASelector('a')
        );
    }
}