| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- declare(strict_types=1);
-
- use PHPUnit\Framework\TestCase;
-
- require_once __DIR__ . '/../../php/markdown.php';
- require_once __DIR__ . '/../../php/spreadsheet.php';
-
- final class SpreadsheetMarkdownIntegrationTests extends TestCase {
- private ?Markdown $parser;
-
- protected function setUp(): void {
- $this->parser = new Markdown(array_merge(Markdown::allReaders(), [ new MDSpreadsheetReader() ]));
- }
-
- private function md(string $markdown): string {
- return $this->normalizeWhitespace($this->parser->toHTML($markdown));
- }
-
- private function normalizeWhitespace(string $str): string {
- $str = mb_eregi_replace('\\s+', ' ', $str);
- $str = mb_eregi_replace('>\\s+<', '><', $str);
- return trim($str);
- }
-
- public function test_integration() {
- $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B |";
- $expected = '<table><thead>' .
- '<tr><th>A</th><th>B</th><th>C</th></tr>' .
- '</thead><tbody><tr>' .
- '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
- '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
- '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td>' .
- '</tr></tbody></table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_bailOut() {
- // If no formulas found table isn't modified
- $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | A*B |";
- $expected = '<table><thead>' .
- '<tr><th>A</th><th>B</th><th>C</th></tr>' .
- '</thead><tbody><tr>' .
- '<td>3</td>' .
- '<td>4</td>' .
- '<td>A*B</td>' .
- '</tr></tbody></table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_observedError1() {
- // Saw "Uncaught Error: columnIndex must be number, got string" from this
- $markdown = "| Unit Price | Qty | Subtotal |\n| ---: | ---: | ---: |\n| $1.23 | 2 | =A*B FILL |\n| $4.99 | 6 | |\n| Total | | =SUM(C:C) |";
- $expected = '<table><thead><tr>' .
- '<th style="text-align: right;">Unit Price</th>' .
- '<th style="text-align: right;">Qty</th>' .
- '<th>Subtotal</th>' .
- '</tr></thead><tbody><tr>' .
- '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="1.23" data-string-value="1.23">$1.23</td>' .
- '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="2" data-string-value="2">2</td>' .
- '<td class="calculated spreadsheet-type-currency" data-numeric-value="2.46" data-string-value="2.46">$2.46</td>' .
- '</tr><tr>' .
- '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="4.99" data-string-value="4.99">$4.99</td>' .
- '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="6" data-string-value="6">6</td>' .
- '<td class="calculated spreadsheet-type-currency" data-numeric-value="29.94" data-string-value="29.94">$29.94</td>' .
- '</tr><tr>' .
- '<td class="spreadsheet-type-string" style="text-align: right;" data-string-value="Total">Total</td>' .
- '<td class="spreadsheet-type-blank" style="text-align: right;" data-numeric-value="0" data-string-value=""></td>' .
- '<td class="calculated spreadsheet-type-currency" data-numeric-value="32.4" data-string-value="32.4">$32.40</td>' .
- '</tr></tbody></table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_observedError2() {
- // A FILLed difference formula in row 2 of `=B2-B1` threw an exception
- // due to a transposed address to the row above of `=B1-B0`.
- $markdown = "| TB | Price | $/TB | Incr |\n| --- | --- | --- | --- |\n| 2 | $99.99 | =B/A FILL | |\n| 4 | $186.41 | | =B2-B1 FILL |\n| 6 | $209.99 | | |";
- $actual = $this->md($markdown);
- $this->assertTrue(true);
- }
-
- public function test_styledValue() {
- $markdown = "| A | B | C |\n| --- | --- | --- |\n| **3** | _4_ | =A*B |";
- $expected = '<table><thead>' .
- '<tr><th>A</th><th>B</th><th>C</th></tr>' .
- '</thead><tbody><tr>' .
- '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3"><strong>3</strong></td>' .
- '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4"><em>4</em></td>' .
- '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td>' .
- '</tr></tbody></table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_styledFormula() {
- $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | **=A*B** |";
- $expected = '<table><thead>' .
- '<tr><th>A</th><th>B</th><th>C</th></tr>' .
- '</thead><tbody><tr>' .
- '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
- '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
- '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12"><strong>12</strong></td>' .
- '</tr></tbody></table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_styledFormula_multiple() {
- $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | ~~**=A*B**~~ |";
- $expected = '<table><thead>' .
- '<tr><th>A</th><th>B</th><th>C</th></tr>' .
- '</thead><tbody><tr>' .
- '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
- '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
- '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12"><s><strong>12</strong></s></td>' .
- '</tr></tbody></table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_preservedOperators() {
- // Regular markdown will want to convert *B* into emphasized B
- $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B*4 |";
- $expected = '<table><thead>' .
- '<tr><th>A</th><th>B</th><th>C</th></tr>' .
- '</thead><tbody><tr>' .
- '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
- '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
- '<td class="calculated spreadsheet-type-number" data-numeric-value="48" data-string-value="48">48</td>' .
- '</tr></tbody></table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
- }
- ?>
|