Skip to content

Commit

Permalink
Add unit tests (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyyb authored Sep 6, 2021
1 parent b2db373 commit 3b41749
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ phpunit.xml
Thumbs.db
.php_cs.cache
.php-cs-fixer.cache
phpunit.log.xml
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@
"optimistdigital/nova-locale-field": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0"
"friendsofphp/php-cs-fixer": "^3.0",
"orchestra/testbench": "^6.20.1",
"phpunit/phpunit": "^9.3.3"
},
"autoload": {
"psr-4": {
"Novius\\LaravelNovaMenu\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Novius\\LaravelNovaMenu\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -42,7 +49,8 @@
"scripts": {
"lint": [
"php-cs-fixer fix --config .php-cs-fixer.php -vv --diff --allow-risky=yes --dry-run"
]
],
"test": "vendor/bin/phpunit --verbose --log-junit phpunit.log.xml"
},
"config": {
"sort-packages": true
Expand Down
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
<server name="APP_DEBUG" value="false"/>
<server name="APP_KEY" value="01234567890123456789012345678932"/>
</php>
</phpunit>
15 changes: 10 additions & 5 deletions src/LaravelNovaMenuServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ public function boot()
{
$this->app->booted(function () {
$this->routes();
Nova::resources(config('laravel-nova-menu.resources', []));
});

Nova::serving(function (ServingNova $event) {
Nova::script('laravel-nova-menu', __DIR__.'/../dist/js/tool.js');
Nova::style('laravel-nova-menu', __DIR__.'/../dist/css/tool.css');
if (!$this->app->runningUnitTests()) {
Nova::resources(config('laravel-nova-menu.resources', []));
}
});

if (!$this->app->runningUnitTests()) {
Nova::serving(function (ServingNova $event) {
Nova::script('laravel-nova-menu', __DIR__.'/../dist/js/tool.js');
Nova::style('laravel-nova-menu', __DIR__.'/../dist/css/tool.css');
});
}

$packageDir = dirname(__DIR__);

$this->publishes([$packageDir.'/config' => config_path()], 'config');
Expand Down
2 changes: 2 additions & 0 deletions src/Observers/ItemObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ protected function cleanModel(MenuItem $item)

if (request()->get('link_type') === MenuItem::TYPE_EMPTY) {
$item->{menuItem::linkTypesAttributes()[MenuItem::TYPE_EMPTY]} = 1;
} else {
$item->{menuItem::linkTypesAttributes()[MenuItem::TYPE_EMPTY]} = 0;
}

unset($item->link_type);
Expand Down
122 changes: 122 additions & 0 deletions tests/Feature/ItemObserverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Novius\LaravelNovaMenu\Tests\Feature;

use Novius\LaravelNovaMenu\Models\Menu;
use Novius\LaravelNovaMenu\Models\MenuItem;
use Novius\LaravelNovaMenu\Tests\TestCase;

class ItemObserverTest extends TestCase
{
protected Menu $menu;

public function setUp(): void
{
parent::setUp();

$this->menu = $this->createMenu();
}

/** @test */
public function createExternalLinkTest()
{
request()->merge([
'link_type' => MenuItem::TYPE_EXTERNAL_LINK,
]);
$linkValue = 'https://www.novius.fr';

$link = new MenuItem();
$link->name = 'Test external';
$link->menu_id = $this->menu->id;
$link->external_link = $linkValue;
$link->internal_link = 'should_be_null_after_saved';
$link->html = 'should_be_null_after_saved';
$link->is_empty_link = 1;
$link->save();

$this->assertNull($link->internal_link);
$this->assertNull($link->html);
$this->assertEquals(0, $link->is_empty_link);
$this->assertEquals($link->external_link, $linkValue);
}

/** @test */
public function createInternalLinkTest()
{
request()->merge([
'link_type' => MenuItem::TYPE_INTERNAL_LINK,
]);
$linkValue = 'linkable_route:contact';

$link = new MenuItem();
$link->name = 'Test internal';
$link->menu_id = $this->menu->id;
$link->external_link = 'should_be_null_after_saved';
$link->is_empty_link = 1;
$link->html = 'should_be_null_after_saved';
$link->internal_link = $linkValue;
$link->save();

$this->assertNull($link->external_link);
$this->assertNull($link->html);
$this->assertEquals(0, $link->is_empty_link);
$this->assertEquals($link->internal_link, $linkValue);
}

/** @test */
public function createEmptyLinkTest()
{
request()->merge([
'link_type' => MenuItem::TYPE_EMPTY,
]);

$link = new MenuItem();
$link->name = 'Test empty link';
$link->menu_id = $this->menu->id;
$link->is_empty_link = 1;
$link->external_link = 'should_be_null_after_saved';
$link->html = 'should_be_null_after_saved';
$link->internal_link = 'should_be_null_after_saved';
$link->save();

$this->assertNull($link->external_link);
$this->assertNull($link->internal_link);
$this->assertNull($link->html);
$this->assertEquals(1, $link->is_empty_link);
}

/** @test */
public function createHtmlLinkTest()
{
request()->merge([
'link_type' => MenuItem::TYPE_HTML,
]);

$html = '<div>test</div>';

$link = new MenuItem();
$link->name = 'Test html link';
$link->menu_id = $this->menu->id;
$link->html = $html;
$link->is_empty_link = 1;
$link->external_link = 'should_be_null_after_saved';
$link->internal_link = 'should_be_null_after_saved';
$link->save();

$this->assertNull($link->external_link);
$this->assertNull($link->internal_link);
$this->assertEquals(0, $link->is_empty_link);
$this->assertEquals($link->html, $html);
}

protected function createMenu(): Menu
{
$menu = new Menu();
$menu->name = 'Test menu';
if (!$menu->save()) {
throw new \Exception('Unable to save menu.');
}

return $menu;
}
}
54 changes: 54 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Novius\LaravelNovaMenu\Tests;

use Illuminate\Support\Facades\Artisan;
use Novius\LaravelNovaMenu\LaravelNovaMenuServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
{
public function setUp(): void
{
parent::setUp();

Artisan::call('migrate');
}

/**
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [
LaravelNovaMenuServiceProvider::class,
];
}

/**
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

$app['config']->set('sluggable', [
'onUpdate' => false,
'separator' => '-',
'method' => null,
'maxLength' => null,
'maxLengthKeepWords' => true,
'slugEngineOptions' => [],
'reserved' => null,
'unique' => true,
'includeTrashed' => false,
]);
}
}

0 comments on commit 3b41749

Please sign in to comment.