diff --git a/.gitignore b/.gitignore index f02ac67..06b22f1 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ phpunit.xml Thumbs.db .php_cs.cache .php-cs-fixer.cache +phpunit.log.xml diff --git a/composer.json b/composer.json index 9205715..643f7b2 100644 --- a/composer.json +++ b/composer.json @@ -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": [ @@ -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 diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..e4bf649 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,27 @@ + + + + + ./tests/Feature + + + + + src/ + + + + + + + + diff --git a/src/LaravelNovaMenuServiceProvider.php b/src/LaravelNovaMenuServiceProvider.php index 00a44e6..8ff82de 100644 --- a/src/LaravelNovaMenuServiceProvider.php +++ b/src/LaravelNovaMenuServiceProvider.php @@ -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'); diff --git a/src/Observers/ItemObserver.php b/src/Observers/ItemObserver.php index ba798f8..d4761d8 100644 --- a/src/Observers/ItemObserver.php +++ b/src/Observers/ItemObserver.php @@ -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); diff --git a/tests/Feature/ItemObserverTest.php b/tests/Feature/ItemObserverTest.php new file mode 100644 index 0000000..d520862 --- /dev/null +++ b/tests/Feature/ItemObserverTest.php @@ -0,0 +1,122 @@ +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 = '
test
'; + + $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; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..4c86c0d --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,54 @@ +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, + ]); + } +}