Skip to content

Commit 12eb487

Browse files
Merge pull request alaminfirdows#8 from alaminfirdows/tests
test: introduce unit-testing
2 parents a2a2643 + dfe055b commit 12eb487

10 files changed

+141
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/public/storage
44
/storage/*.key
55
/vendor
6+
/.phpunit.cache
67
composer.lock
78
.env
89
.env.backup

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"AlAminFirdows\\LaravelEditorJs\\Tests\\": "tests/"
2929
}
3030
},
31+
"scripts": {
32+
"test": "./vendor/bin/phpunit"
33+
},
3134
"extra": {
3235
"laravel": {
3336
"providers": [

phpunit.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" bootstrap="vendor/autoload.php" cacheResultFile=".phpunit.cache/test-results" colors="true">
3+
<testsuites>
4+
<testsuite name="Unit">
5+
<directory suffix="Test.php">./tests/Unit</directory>
6+
</testsuite>
7+
<testsuite name="Feature">
8+
<directory suffix="Test.php">./tests/Feature</directory>
9+
</testsuite>
10+
</testsuites>
11+
12+
<coverage cacheDirectory=".phpunit.cache/code-coverage" processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">src</directory>
15+
</include>
16+
</coverage>
17+
</phpunit>
+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
<p>
2-
{{ $data['text'] }}
3-
</p>
1+
<p>{{ $data['text'] }}</p>

src/LaravelEditorJs.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use EditorJS\EditorJS;
66
use EditorJS\EditorJSException;
7-
use Illuminate\Support\Str;
7+
use Exception;
88
use Illuminate\Support\Facades\View;
9+
use Illuminate\Support\Str;
910

1011
class LaravelEditorJs
1112
{
@@ -15,7 +16,7 @@ class LaravelEditorJs
1516
* @param string $data
1617
* @return string
1718
*/
18-
public function render(string $data) : string
19+
public function render(string $data): string
1920
{
2021
try {
2122
$configJson = json_encode(config('laravel_editorjs.config') ?: []);
@@ -28,7 +29,7 @@ public function render(string $data) : string
2829

2930
$viewName = "laravel_editorjs::blocks." . Str::snake($block['type'], '-');
3031

31-
if (! View::exists($viewName)) {
32+
if (!View::exists($viewName)) {
3233
$viewName = 'laravel_editorjs::blocks.not-found';
3334
}
3435

@@ -40,7 +41,7 @@ public function render(string $data) : string
4041

4142
return implode($renderedBlocks);
4243
} catch (EditorJSException $e) {
43-
throw new \Exception($e->getMessage());
44+
throw new Exception($e->getMessage());
4445
}
4546
}
4647
}

src/LaravelEditorJsServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ public function boot()
3939
], 'laravel_editorjs-views');
4040
}
4141
}
42-
}
42+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace AlAminFirdows\LaravelEditorJs\Tests\Feature\Blocks;
4+
5+
use AlAminFirdows\LaravelEditorJs\Tests\TestCase;
6+
7+
class ListBlockTest extends TestCase
8+
{
9+
protected function getBlockData()
10+
{
11+
return [
12+
'type' => 'list',
13+
'data' => [
14+
'type' => 'unordered',
15+
'items' => [
16+
'Hello',
17+
'World',
18+
],
19+
],
20+
];
21+
}
22+
23+
protected function getBlockHtml()
24+
{
25+
return "<ul> <li>Hello</li> <li>World</li> </ul>";
26+
}
27+
28+
/** @test */
29+
public function render_paragraph_block_test(): void
30+
{
31+
$this->assertEquals(
32+
$this->renderBlocks($this->getEditorData([$this->getBlockData()])),
33+
$this->getBlockHtml()
34+
);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace AlAminFirdows\LaravelEditorJs\Tests\Feature\Blocks;
4+
5+
use AlAminFirdows\LaravelEditorJs\Tests\TestCase;
6+
7+
class ParagraphBlockTest extends TestCase
8+
{
9+
protected function getBlockData()
10+
{
11+
return [
12+
'type' => 'paragraph',
13+
'data' => [
14+
'text' => 'Hello world!',
15+
],
16+
];
17+
}
18+
19+
protected function getBlockHtml()
20+
{
21+
return "<p>Hello world!</p>";
22+
}
23+
24+
/** @test */
25+
public function render_paragraph_block_test(): void
26+
{
27+
$this->assertEquals(
28+
$this->renderBlocks($this->getEditorData([$this->getBlockData()])),
29+
$this->getBlockHtml()
30+
);
31+
}
32+
}

tests/TestCase.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace AlAminFirdows\LaravelEditorJs\Tests;
4+
5+
use AlAminFirdows\LaravelEditorJs\Facades\LaravelEditorJs;
6+
use AlAminFirdows\LaravelEditorJs\LaravelEditorJsServiceProvider;
7+
8+
abstract class TestCase extends \Orchestra\Testbench\TestCase
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [
13+
LaravelEditorJsServiceProvider::class,
14+
];
15+
}
16+
17+
protected function getEditorData(array $blocks)
18+
{
19+
return json_encode([
20+
'time' => time(),
21+
'blocks' => $blocks,
22+
'version' => '2.28.0',
23+
]);
24+
}
25+
26+
protected function renderBlocks(string $content)
27+
{
28+
return preg_replace('/\R+/', '', LaravelEditorJs::render($content));
29+
}
30+
}

tests/Unit/RenderTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace AlAminFirdows\LaravelEditorJs\Tests\Unit;
4+
5+
use AlAminFirdows\LaravelEditorJs\Tests\TestCase;
6+
7+
class RenderTest extends TestCase
8+
{
9+
10+
/** @test */
11+
public function render_test():void
12+
{
13+
$this->assertTrue(true);
14+
}
15+
}

0 commit comments

Comments
 (0)