|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Nwidart\Modules\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Artisan; |
| 6 | +use Illuminate\Support\Facades\Blade; |
| 7 | +use Illuminate\Support\Facades\Log; |
| 8 | +use Nwidart\Modules\Contracts\RepositoryInterface; |
| 9 | +use Nwidart\Modules\Laravel\Module; |
| 10 | + |
| 11 | +class ModuleHelperTest extends BaseTestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Illuminate\Filesystem\Filesystem |
| 15 | + */ |
| 16 | + private $finder; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + private $modulePath; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + $this->finder = $this->app['files']; |
| 27 | + $this->createModule(); |
| 28 | + $this->modulePath = $this->getModuleAppPath(); |
| 29 | + } |
| 30 | + |
| 31 | + protected function tearDown(): void |
| 32 | + { |
| 33 | + $this->app[RepositoryInterface::class]->delete('Blog'); |
| 34 | + parent::tearDown(); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_module_returns_true_when_found() |
| 38 | + { |
| 39 | + $this->assertTrue(module('Blog')); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_module_returns_false_and_log_error_when_not_found() |
| 43 | + { |
| 44 | + Log::shouldReceive('error')->once()->with("Module 'Blogs' not found."); |
| 45 | + |
| 46 | + $this->assertFalse(module('Blogs')); |
| 47 | + } |
| 48 | + |
| 49 | + public function test_module_returns_false_and_log_error_when_not_found_and_instance_parameter_is_true() |
| 50 | + { |
| 51 | + Log::shouldReceive('error')->once()->with("Module 'Blogs' not found."); |
| 52 | + |
| 53 | + $this->assertFalse(module('Blogs', true)); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_module_returns_instance_when_instance_parameter_is_true() |
| 57 | + { |
| 58 | + $module = module('Blog', true); |
| 59 | + |
| 60 | + $this->assertInstanceOf(Module::class, $module); |
| 61 | + $this->assertEquals('Blog', $module->getName()); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_module_returns_false_when_disabled() |
| 65 | + { |
| 66 | + Artisan::call('module:disable Blog'); |
| 67 | + |
| 68 | + $this->assertFalse(module('Blog')); |
| 69 | + } |
| 70 | + |
| 71 | + public function test_module_returns_instance_when_disabled_and_instance_parameter_is_true() |
| 72 | + { |
| 73 | + Artisan::call('module:disable Blog'); |
| 74 | + |
| 75 | + $module = module('Blog', true); |
| 76 | + |
| 77 | + $this->assertInstanceOf(Module::class, $module); |
| 78 | + $this->assertEquals('Blog', $module->getName()); |
| 79 | + } |
| 80 | + |
| 81 | + public function test_module_directive_renders_content_when_module_is_enabled() |
| 82 | + { |
| 83 | + $blade = "@module('Blog') Enabled @endmodule"; |
| 84 | + |
| 85 | + $this->assertStringContainsString('Enabled', Blade::render($blade)); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_module_directive_does_not_render_content_when_module_is_disabled() |
| 89 | + { |
| 90 | + Artisan::call('module:disable Blog'); |
| 91 | + |
| 92 | + $blade = "@module('Blog') Enabled @endmodule"; |
| 93 | + |
| 94 | + $this->assertStringNotContainsString('Enabled', Blade::render($blade)); |
| 95 | + } |
| 96 | +} |
0 commit comments