Skip to content

Commit

Permalink
tests ~ wip
Browse files Browse the repository at this point in the history
  • Loading branch information
megasteve19 committed Jan 10, 2023
1 parent e0e88a0 commit 1e872d5
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Parser
*
* @var array<string, Provider>
*/
protected static array $providers;
public static array $providers;

/**
* Converted input JSON.
Expand Down
15 changes: 14 additions & 1 deletion tests/Datasets/Samples.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<?php

dataset('sample', [
'sample' => file_get_contents(__DIR__ . '/../samples/sample.json'),
'sample' => file_get_contents(__DIR__ . '/sample.json'),
]);

dataset('sampleBroken', [
'sample' => file_get_contents(__DIR__ . '/sample-broken.json'),
]);

dataset('sampleUnknownProvider', [
'sample' => file_get_contents(__DIR__ . '/sample-unknown-provider.json'),
]);

dataset('sampleUnmatchingSchema', [
'sample' => file_get_contents(__DIR__ . '/sample-unmatching-schema.json'),
]);

12 changes: 12 additions & 0 deletions tests/Datasets/sample-broken.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
We have to make this JSON broken, isn't it? I wonder how...

{
"time": 1672852569662,
"blocks": [
{
"type": "delimiter",
"data": {}
}
],
"version": "2.26.4"
}
10 changes: 10 additions & 0 deletions tests/Datasets/sample-unknown-provider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"time": 1672852569662,
"blocks": [
{
"type": "foo",
"data": {}
}
],
"version": "2.26.4"
}
4 changes: 4 additions & 0 deletions tests/Datasets/sample-unmatching-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"time": 1672852569662,
"version": "2.26.4"
}
File renamed without changes.
15 changes: 13 additions & 2 deletions tests/EditorPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@
})->with('sample');

test('Can be rendered by casting to string', function($sample) {
expect((string) EditorPhp::make($sample)->render())->toBeString();
expect((string) EditorPhp::make($sample))->toBeString();
})->with('sample');

test('Test', fn() => expect(false)->toBeTrue());
test('Throws exception on broken input', function($sample) {
EditorPhp::make($sample);
})->with('sampleBroken')->throws(Exception::class);

test('Throws exception on unknown block provider', function($sample) {
EditorPhp::make($sample);
})->with('sampleUnknownProvider')->throws(Exception::class);

test('Throws exception on unmatching schema', function($sample) {
EditorPhp::make($sample);
})->with('sampleUnmatchingSchema')->throws(Exception::class);

68 changes: 0 additions & 68 deletions tests/Laravel/EditorPhpTest.php

This file was deleted.

65 changes: 65 additions & 0 deletions tests/LaravelEditorPhpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use BumpCore\EditorPhp\Casts\EditorPhpCast;
use BumpCore\EditorPhp\EditorPhp;
use BumpCore\EditorPhp\Tests\LaravelTestCase;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;

use function Orchestra\Testbench\artisan;

uses(LaravelTestCase::class);

test('Can be rendered in Laravel environment', function($sample) {
expect(EditorPhp::make($sample)->render())->toBeString();
})->with('sample');

test('Can model casting works with json', function($sample) {
$model = new class() extends Model
{
protected $fillable = [
'content',
];

protected $casts = [
'content' => EditorPhpCast::class,
];
};

$model->content = $sample;

expect($model->content)->toBeInstanceOf(EditorPhp::class);
})->with('sample');

test('Can model casting works with editor.php instance', function($sample) {
$model = new class() extends Model
{
protected $fillable = [
'content',
];

protected $casts = [
'content' => EditorPhpCast::class,
];
};

$model->content = EditorPhp::make($sample);

expect($model->content)->toBeInstanceOf(EditorPhp::class);
})->with('sample');

test('Can `make:block` command creates block provider', function() {
expect(artisan($this, 'make:block FooBlock'))->toEqual(Command::SUCCESS);
});

test('Can render on response', function() {
$response = $this->get('/editor', ['Accept' => 'text/html'])->getContent();

expect($response)->not()->toBeJson();
});

test('Can encode to json on response', function() {
$response = $this->get('/editor', ['Accept' => 'application/json'])->getContent();

expect($response)->toBeJson();
});
28 changes: 21 additions & 7 deletions tests/LaravelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@

namespace BumpCore\EditorPhp\Tests;

use BumpCore\EditorPhp\EditorPhp;
use BumpCore\EditorPhp\EditorPhpServiceProvider;
use Illuminate\Console\Command;
use Orchestra\Testbench\TestCase;

class LaravelTestCase extends TestCase
{
protected function getPackageProviders($app)
{
return [
EditorPhpServiceProvider::class,
];
}
protected function getPackageProviders($app)
{
return [
EditorPhpServiceProvider::class,
];
}

/**
* Define routes setup.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
protected function defineRoutes($router)
{
$router->get('/editor', function() {
return EditorPhp::make(file_get_contents(__DIR__ . '/Datasets/sample.json'));
});
}
}
47 changes: 47 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Parser;

test('Can not register un implemented provider', function() {
$provider = new class()
{
public function rules(): array
{
return [];
}

public function render(Data $data): string
{
return $data('foo');
}
};

Parser::register([
$provider::class,
]);
})->throws(Exception::class);

test('Can explicitly define provider type', function() {
$provider = new class() implements Provider
{
public $type = 'bar';

public function rules(): array
{
return [];
}

public function render(Data $data): string
{
return $data('foo');
}
};

Parser::register([
$provider,
]);

expect(Parser::$providers)->toHaveKeys([$provider->type]);
});
3 changes: 0 additions & 3 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use BumpCore\EditorPhp\EditorPhp;
use BumpCore\EditorPhp\Tests\LaravelTestCase;

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -67,5 +66,3 @@ function something()
\BumpCore\EditorPhp\Blocks\Warning::class,
]);
})->in('.');

uses(LaravelTestCase::class)->in('Laravel');

0 comments on commit 1e872d5

Please sign in to comment.