File: SqlFormatterTest.php

package info (click to toggle)
php-sql-formatter 1.2.17%2Bdct1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 692 kB
  • sloc: php: 1,819; sql: 301; xml: 18; makefile: 17
file content (197 lines) | stat: -rw-r--r-- 5,505 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

declare(strict_types=1);

namespace Doctrine\SqlFormatter\Tests;

use Doctrine\SqlFormatter\CliHighlighter;
use Doctrine\SqlFormatter\HtmlHighlighter;
use Doctrine\SqlFormatter\NullHighlighter;
use Doctrine\SqlFormatter\SqlFormatter;
use Generator;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

use function assert;
use function count;
use function defined;
use function explode;
use function file_get_contents;
use function pack;
use function sprintf;
use function trim;

/**
 * @covers \Doctrine\SqlFormatter\SqlFormatter
 */
final class SqlFormatterTest extends TestCase
{
    /** @var string[] */
    private $sqlData;

    /** @var SqlFormatter */
    private $formatter;

    /** @var HtmlHighlighter */
    private $highlighter;

    protected function setUp(): void
    {
        // Force SqlFormatter to run in non-CLI mode for tests
        $this->highlighter = new HtmlHighlighter();

        $this->formatter = new SqlFormatter($this->highlighter);
    }

    /**
     * @dataProvider formatHighlightData
     */
    public function testFormatHighlight(string $sql, string $html): void
    {
        $this->assertEquals(trim($html), trim($this->formatter->format($sql)));
    }

    /**
     * @dataProvider formatData
     */
    public function testFormat(string $sql, string $html): void
    {
        $formatter = new SqlFormatter(new NullHighlighter());
        $this->assertEquals(trim($html), trim($formatter->format($sql)));
    }

    /**
     * @dataProvider highlightData
     */
    public function testHighlight(string $sql, string $html): void
    {
        $this->assertEquals(trim($html), trim($this->formatter->highlight($sql)));
    }

    public function testHighlightBinary(): void
    {
        $sql = 'SELECT "' . pack('H*', 'ed180e98a47a45b3bdd304b798bc5797') . '" AS BINARY';

        if (defined('ENT_IGNORE')) {
            // this is what gets written as string
            $binaryData = '&quot;' . pack('H*', '180e7a450457') . '&quot;';
        } else {
            $binaryData = '';
        }

        $html = '<pre style="color: black; background-color: white;">' .
            '<span style="font-weight:bold;">SELECT</span> <span style="color: blue;">' .
            $binaryData .
            '</span> <span style="font-weight:bold;">AS</span> <span style="color: #333;">BINARY</span></pre>';

        $this->assertEquals(trim($html), trim($this->formatter->highlight($sql)));
    }

    /**
     * @dataProvider highlightCliData
     */
    public function testCliHighlight(string $sql, string $html): void
    {
        $formatter = new SqlFormatter(new CliHighlighter());
        $this->assertEquals(trim($html), trim($formatter->format($sql)));
    }

    /**
     * @dataProvider compressData
     */
    public function testCompress(string $sql, string $html): void
    {
        $this->assertEquals(trim($html), trim($this->formatter->compress($sql)));
    }

    public function testUsePre(): void
    {
        $formatter = new SqlFormatter(new HtmlHighlighter([], false));
        $actual    = $formatter->highlight('test');
        $expected  = '<span style="color: #333;">test</span>';
        $this->assertEquals($actual, $expected);

        $formatter = new SqlFormatter(new HtmlHighlighter([], true));
        $actual    = $formatter->highlight('test');
        $expected  = '<pre style="color: black; background-color: white;">' .
            '<span style="color: #333;">test</span></pre>';
        $this->assertEquals($actual, $expected);
    }

    /**
     * @return Generator<mixed[]>
     */
    private function fileDataProvider(string $file): Generator
    {
        $contents = file_get_contents(__DIR__ . '/' . $file);
        assert($contents !== false);
        $formatHighlightData = explode("\n---\n", $contents);
        $sqlData             = $this->sqlData();
        if (count($formatHighlightData) !== count($sqlData)) {
            throw new UnexpectedValueException(sprintf(
                '"%s" (%d sections) and sql.sql (%d sections) should have the same number of sections',
                $file,
                count($formatHighlightData),
                count($sqlData)
            ));
        }

        foreach ($formatHighlightData as $i => $data) {
            yield [$sqlData[$i], $data];
        }
    }

    /**
     * @return Generator<mixed[]>
     */
    public function formatHighlightData(): Generator
    {
        return $this->fileDataProvider('format-highlight.html');
    }

    /**
     * @return Generator<mixed[]>
     */
    public function highlightCliData(): Generator
    {
        return $this->fileDataProvider('clihighlight.html');
    }

    /**
     * @return Generator<mixed[]>
     */
    public function formatData(): Generator
    {
        return $this->fileDataProvider('format.html');
    }

    /**
     * @return Generator<mixed[]>
     */
    public function compressData(): Generator
    {
        return $this->fileDataProvider('compress.html');
    }

    /**
     * @return Generator<mixed[]>
     */
    public function highlightData(): Generator
    {
        return $this->fileDataProvider('highlight.html');
    }

    /**
     * @return mixed[]
     */
    public function sqlData(): array
    {
        if (! $this->sqlData) {
            $contents = file_get_contents(__DIR__ . '/sql.sql');
            assert($contents !== false);
            $this->sqlData = explode("\n---\n", $contents);
        }

        return $this->sqlData;
    }
}