Skip to content

Commit 844526a

Browse files
committed
Readme, tests, rename namespace.
1 parent 7c8ba5e commit 844526a

8 files changed

Lines changed: 263 additions & 27 deletions

File tree

.github/workflows/tests.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Tests
2+
3+
on: [ push, pull_request ]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
if: "!contains(github.event.head_commit.message, 'ci skip')"
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
php: [ 7.3, 7.4, 8.0 ]
13+
laravel: [ 7.*, 8.* ]
14+
dependency-version: [ prefer-lowest, prefer-stable ]
15+
include:
16+
- laravel: 7.*
17+
testbench: 5.*
18+
19+
- laravel: 7.*
20+
dependency-version: prefer-lowest
21+
composer-version: --1
22+
23+
- laravel: 8.*
24+
testbench: 6.*
25+
26+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
27+
28+
steps:
29+
- name: Checkout code
30+
uses: actions/checkout@v2
31+
32+
- name: Cache dependencies
33+
uses: actions/cache@v2
34+
with:
35+
path: ~/.composer/cache/files
36+
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
37+
38+
- name: Setup PHP
39+
uses: shivammathur/setup-php@v2
40+
with:
41+
php-version: ${{ matrix.php }}
42+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
43+
coverage: none
44+
45+
- name: Install dependencies
46+
run: |
47+
composer self-update ${{ matrix.composer-version }}
48+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
49+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
50+
51+
- name: Execute tests
52+
run: vendor/bin/phpunit

.gitignore

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
# osx noise
2-
.DS_Store
3-
profile
4-
5-
# xcode noise
6-
build/*
7-
*.mode1
8-
*.mode1v3
9-
*.mode2v3
10-
*.perspective
11-
*.perspectivev3
12-
*.pbxuser
13-
*.xcworkspace
14-
xcuserdata
15-
16-
# svn & cvs
17-
.svn
18-
CVS
1+
/vendor
2+
.env
3+
.phpunit.result.cache
4+
composer.lock

.styleci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
preset: laravel
2+
3+
disabled:
4+
- concat_without_spaces
5+
- not_operator_with_successor_space
6+
- cast_spaces
7+
- trailing_comma_in_multiline_array
8+
- heredoc_to_nowdoc
9+
- phpdoc_summary
10+
11+
risky: false

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Laravel Torchlight Extension for Commonmark
2+
3+
[![Tests](https://github.com/torchlight-api/torchlight-commonmark/actions/workflows/tests.yml/badge.svg)](https://github.com/torchlight-api/torchlight-commonmark/actions/workflows/tests.yml) [![Latest Stable Version](https://poser.pugx.org/torchlight/commonmark/v)](//packagist.org/packages/torchlight/commonmark) [![Total Downloads](https://poser.pugx.org/torchlight/commonmark/downloads)](//packagist.org/packages/torchlight/commonmark) [![License](https://poser.pugx.org/torchlight/commonmark/license)](//packagist.org/packages/torchlight/commonmark)

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "torchlight/commonmark-php",
2+
"name": "torchlight/commonmark",
33
"description": "A Commonmark extension for Torchlight, the syntax highlighting API.",
44
"homepage": "https://github.com/hammerstonedev/torchlight-commonmark-php",
55
"type": "library",
@@ -12,20 +12,22 @@
1212
],
1313
"require": {
1414
"php": "^7.2|^8.0",
15-
"torchlight/laravel": "dev-main",
15+
"torchlight/laravel": "^0.2.1",
1616
"league/commonmark": "^1.5"
1717
},
1818
"require-dev": {
19+
"orchestra/testbench": "^5.0|^6.0",
20+
"mockery/mockery": "^1.3.3",
1921
"phpunit/phpunit": "^8.4"
2022
},
2123
"autoload": {
2224
"psr-4": {
23-
"Hammerstone\\Torchlight\\Commonmark\\": "src/"
25+
"Torchlight\\Commonmark\\": "src/"
2426
}
2527
},
2628
"autoload-dev": {
2729
"psr-4": {
28-
"Hammerstone\\Torchlight\\Commonmark\\Tests\\": "tests/"
30+
"Torchlight\\Commonmark\\Tests\\": "tests/"
2931
}
3032
},
3133
"extra": {

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Torchlight Commonmark Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<php>
23+
</php>
24+
</phpunit>

src/TorchlightExtension.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Hammerstone\Torchlight\Commonmark;
3+
namespace Torchlight\Commonmark;
44

5-
use Hammerstone\Torchlight\Block;
6-
use Hammerstone\Torchlight\Client;
5+
use Torchlight\Block;
6+
use Torchlight\Client;
77
use League\CommonMark\Block\Element\AbstractBlock;
88
use League\CommonMark\Block\Element\FencedCode;
99
use League\CommonMark\Block\Element\IndentedCode;
@@ -72,8 +72,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
7272
protected function makeTorchlightBlock($node)
7373
{
7474
return Block::make()
75-
->setLanguage($this->getLanguage($node))
76-
->setCode($node->getStringContent());
75+
->language($this->getLanguage($node))
76+
->code($node->getStringContent());
7777
}
7878

7979
protected function isCodeNode($node)

tests/CodeRendererTest.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace Torchlight\Commonmark\Tests;
4+
5+
use Illuminate\Support\Facades\Http;
6+
use Torchlight\Block;
7+
use Torchlight\Client;
8+
use Torchlight\Commonmark\TorchlightExtension;
9+
use League\CommonMark\DocParser;
10+
use League\CommonMark\Environment;
11+
use League\CommonMark\HtmlRenderer;
12+
use Orchestra\Testbench\TestCase;
13+
14+
class CodeRendererTest extends TestCase
15+
{
16+
17+
protected function getEnvironmentSetUp($app)
18+
{
19+
config()->set('torchlight.token', 'token');
20+
21+
$ids = [
22+
'block_id_1',
23+
'block_id_2',
24+
'block_id_3',
25+
];
26+
27+
Block::$generateIdsUsing = function () use (&$ids) {
28+
return array_shift($ids);
29+
};
30+
}
31+
32+
protected function render($markdown)
33+
{
34+
$environment = Environment::createCommonMarkEnvironment();
35+
$environment->addExtension(new TorchlightExtension);
36+
37+
$parser = new DocParser($environment);
38+
$htmlRenderer = new HtmlRenderer($environment);
39+
40+
$document = $parser->parse($markdown);
41+
42+
return $htmlRenderer->renderBlock($document);
43+
}
44+
45+
/** @test */
46+
public function it_highlights_code_blocks()
47+
{
48+
$markdown = <<<'EOT'
49+
before
50+
51+
```html
52+
<div>html</div>
53+
```
54+
after
55+
EOT;
56+
57+
$response = [
58+
"blocks" => [[
59+
"id" => "block_id_1",
60+
"wrapped" => "<pre><code>highlighted</code></pre>",
61+
]]
62+
];
63+
64+
Http::fake([
65+
'api.torchlight.dev/*' => Http::response($response, 200),
66+
]);
67+
68+
$html = $this->render($markdown);
69+
70+
$expected = <<<EOT
71+
<p>before</p>
72+
<pre><code>highlighted</code></pre>
73+
<p>after</p>
74+
75+
EOT;
76+
77+
$this->assertEquals($expected, $html);
78+
}
79+
80+
/** @test */
81+
public function gets_language_and_contents()
82+
{
83+
$markdown = <<<'EOT'
84+
before
85+
86+
```foobarlang
87+
<div>test</div>
88+
```
89+
after
90+
EOT;
91+
92+
Http::fake();
93+
94+
$this->render($markdown);
95+
96+
Http::assertSent(function ($request) {
97+
return $request['blocks'][0]['language'] === "foobarlang"
98+
&& $request['blocks'][0]['code'] === "<div>test</div>";
99+
100+
});
101+
}
102+
103+
/** @test */
104+
public function it_sends_one_request_only_and_matches_by_id()
105+
{
106+
$markdown = <<<'EOT'
107+
before
108+
109+
```php
110+
some php
111+
```
112+
113+
```ruby
114+
some ruby
115+
```
116+
117+
```js
118+
some js
119+
```
120+
after
121+
EOT;
122+
123+
$response = [
124+
"blocks" => [[
125+
"id" => "block_id_3",
126+
"wrapped" => "some js",
127+
], [
128+
"id" => "block_id_1",
129+
"wrapped" => "some php",
130+
], [
131+
"id" => "block_id_2",
132+
"wrapped" => "some ruby",
133+
]]
134+
];
135+
136+
Http::fake([
137+
'api.torchlight.dev/*' => Http::response($response, 200),
138+
]);
139+
140+
141+
$html = $this->render($markdown);
142+
143+
Http::assertSentCount(1);
144+
145+
$expected = <<<EOT
146+
<p>before</p>
147+
some php
148+
some ruby
149+
some js
150+
<p>after</p>
151+
152+
EOT;
153+
154+
$this->assertEquals($expected, $html);
155+
}
156+
157+
158+
}

0 commit comments

Comments
 (0)