From bc03546c189834db7d77a01cd60ca2855c182ddb Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 6 Feb 2024 00:13:34 +0545 Subject: [PATCH 1/8] test: Orchestra Testbench added in require dev --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3e6263c..66aba27 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "cviebrock/eloquent-sluggable": "^10.0" }, "require-dev": { - "phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0" + "phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0", + "orchestra/testbench": "^8.19" }, "extra": { "laravel": { From 9c2f323c67af7b5d24447e57b0348cc07b8304f0 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 6 Feb 2024 00:14:29 +0545 Subject: [PATCH 2/8] test: Autoload Namespace fixed --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 66aba27..fffe491 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "autoload-dev": { "psr-4": { - "Tests\\": "tests/" + "AchyutN\\LaravelHelpers\\Tests\\": "tests/" } }, "authors": [ From 326be989daf6488c3bdd3e24b90ee52df966b9ad Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 6 Feb 2024 00:24:53 +0545 Subject: [PATCH 3/8] test: BaseTestCase extended in test --- tests/BaseTestCase.php | 39 +++++++++++++++++++++++++++++++++++++ tests/NepaliHelpersTest.php | 6 ++---- 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 tests/BaseTestCase.php diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php new file mode 100644 index 0000000..15c9659 --- /dev/null +++ b/tests/BaseTestCase.php @@ -0,0 +1,39 @@ +loadMigrationsFrom(__DIR__ . '/migrations'); + $this->artisan('migrate', ['--database' => ':memory:'])->run(); + } + + protected function getEnvironmentSetUp($app): void + { + // sqlite test database + $app['config']->set('database.connections.the_test', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + // set config + $app['config']->set('database.default', 'the_test'); + $app['config']->set('test', 'test'); + } + + public function getPackageProviders($app): array + { + return [ + \AchyutN\LaravelHelpers\LaravelHelperProvider::class, + ]; + } +} \ No newline at end of file diff --git a/tests/NepaliHelpersTest.php b/tests/NepaliHelpersTest.php index b09719c..b121d12 100644 --- a/tests/NepaliHelpersTest.php +++ b/tests/NepaliHelpersTest.php @@ -1,10 +1,8 @@ Date: Tue, 6 Feb 2024 00:27:26 +0545 Subject: [PATCH 4/8] fix(test): Memory removed with sqlite migration --- tests/BaseTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 15c9659..c84e48b 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -14,7 +14,7 @@ public function setUp(): void // test migrations $this->loadMigrationsFrom(__DIR__ . '/migrations'); - $this->artisan('migrate', ['--database' => ':memory:'])->run(); + $this->artisan('migrate')->run(); } protected function getEnvironmentSetUp($app): void From cd22e1fcb2531890a0597c475c1706cab8ee82de Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 6 Feb 2024 00:39:30 +0545 Subject: [PATCH 5/8] test: Inactive tests added --- tests/Factories/ArticleFactory.php | 29 ++++++++++++++++++++++++ tests/InactiveTest.php | 32 +++++++++++++++++++++++++++ tests/Models/Article.php | 22 +++++++++++++++++++ tests/migrations/articles_table.php | 34 +++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 tests/Factories/ArticleFactory.php create mode 100644 tests/InactiveTest.php create mode 100644 tests/Models/Article.php create mode 100644 tests/migrations/articles_table.php diff --git a/tests/Factories/ArticleFactory.php b/tests/Factories/ArticleFactory.php new file mode 100644 index 0000000..12deeb7 --- /dev/null +++ b/tests/Factories/ArticleFactory.php @@ -0,0 +1,29 @@ + + */ + protected $model = Article::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => $this->faker->sentence, + 'content' => $this->faker->paragraph + ]; + } +} \ No newline at end of file diff --git a/tests/InactiveTest.php b/tests/InactiveTest.php new file mode 100644 index 0000000..dc5c73d --- /dev/null +++ b/tests/InactiveTest.php @@ -0,0 +1,32 @@ +create(); + $article->setInactive(); + $this->assertFalse($article->inactive_at == null); + } + + public function test_change_to_active() + { + $article = Article::factory()->create(['inactive_at' => now()]); + $article->setActive(); + $this->assertTrue($article->inactive_at == null); + } + + public function test_count_active() + { + Article::factory()->count(5)->create(); + Article::factory()->count(3)->create(['inactive_at' => now()]); + $this->assertEquals(3, Article::onlyInactive()->count()); + $this->assertEquals(5, Article::count()); + $this->assertEquals(8, Article::withInactive()->count()); + $this->assertEquals(5, Article::withoutInactive()->count()); + } +} \ No newline at end of file diff --git a/tests/Models/Article.php b/tests/Models/Article.php new file mode 100644 index 0000000..0b07828 --- /dev/null +++ b/tests/Models/Article.php @@ -0,0 +1,22 @@ + 1) { + return ArticleFactory::times($count); + } else { + return ArticleFactory::new(); + } + } +} \ No newline at end of file diff --git a/tests/migrations/articles_table.php b/tests/migrations/articles_table.php new file mode 100644 index 0000000..bf2d2e3 --- /dev/null +++ b/tests/migrations/articles_table.php @@ -0,0 +1,34 @@ +id(); + $table->string("title"); + $table->text("content"); + $table->dateTime('inactive_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists("articles"); + } +}; From 29eb6b668d9f6870a2e0440288f6ea58441c94f8 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 6 Feb 2024 01:11:07 +0545 Subject: [PATCH 6/8] fix(test): Sluggable config added in basecase --- src/LaravelHelperProvider.php | 1 + tests/BaseTestCase.php | 14 ++++++++++++++ tests/Models/Article.php | 3 ++- tests/SlugTest.php | 11 +++++++++++ tests/migrations/articles_table.php | 1 + 5 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/SlugTest.php diff --git a/src/LaravelHelperProvider.php b/src/LaravelHelperProvider.php index 5c3bdd5..7ac7096 100644 --- a/src/LaravelHelperProvider.php +++ b/src/LaravelHelperProvider.php @@ -4,6 +4,7 @@ use AchyutN\LaravelHelpers\Pagination\CustomPaginator; use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; class LaravelHelperProvider extends ServiceProvider diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index c84e48b..d64c87f 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -28,6 +28,20 @@ protected function getEnvironmentSetUp($app): void // set config $app['config']->set('database.default', 'the_test'); $app['config']->set('test', 'test'); + + // sluggable config + $app['config']->set('sluggable', [ + 'source' => null, + 'onUpdate' => false, + 'separator' => '-', + 'method' => null, + 'maxLength' => null, + 'maxLengthKeepWords' => true, + 'unique' => true, + 'slugEngineOptions' => [], + 'reserved' => null, + 'includeTrashed' => false, + ]); } public function getPackageProviders($app): array diff --git a/tests/Models/Article.php b/tests/Models/Article.php index 0b07828..e9d9fef 100644 --- a/tests/Models/Article.php +++ b/tests/Models/Article.php @@ -4,12 +4,13 @@ use AchyutN\LaravelHelpers\Tests\Factories\ArticleFactory; use AchyutN\LaravelHelpers\Traits\CanBeInactive; +use AchyutN\LaravelHelpers\Traits\HasTheSlug; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Model; class Article extends Model { - use CanBeInactive; + use CanBeInactive, HasTheSlug; protected $guarded = []; protected static function factory(int $count = 1): Factory { diff --git a/tests/SlugTest.php b/tests/SlugTest.php new file mode 100644 index 0000000..39a40a8 --- /dev/null +++ b/tests/SlugTest.php @@ -0,0 +1,11 @@ +id(); $table->string("title"); + $table->string('slug')->nullable(); $table->text("content"); $table->dateTime('inactive_at')->nullable(); $table->timestamps(); From 97d16f2536e33593051a07650cdb30a514b004b7 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 6 Feb 2024 01:14:49 +0545 Subject: [PATCH 7/8] test: Slug Tests added --- tests/BaseTestCase.php | 2 ++ tests/SlugTest.php | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index d64c87f..be08041 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -41,6 +41,8 @@ protected function getEnvironmentSetUp($app): void 'slugEngineOptions' => [], 'reserved' => null, 'includeTrashed' => false, + 'uniqueSuffix' => null, + 'firstUniqueSuffix' => 2, ]); } diff --git a/tests/SlugTest.php b/tests/SlugTest.php index 39a40a8..02ebb91 100644 --- a/tests/SlugTest.php +++ b/tests/SlugTest.php @@ -7,5 +7,37 @@ class SlugTest extends BaseTestCase { - // + + public function test_title_can_be_slugified() + { + $article = Article::factory()->create(['title' => 'This is a test title']); + $this->assertEquals('this-is-a-test-title', $article->slug); + } + + public function test_title_stays_same_after_update() + { + $article = Article::factory()->create(['title' => 'This is a test title']); + $this->assertEquals('this-is-a-test-title', $article->slug); + $article->update(['title' => 'This is a new title']); + $this->assertEquals('this-is-a-test-title', $article->slug); + } + + public function test_slugs_with_same_title() + { + $article1 = Article::factory()->create(['title' => 'This is a test title']); + $article2 = Article::factory()->create(['title' => 'This is a test title']); + $this->assertEquals('this-is-a-test-title', $article1->slug); + $this->assertEquals('this-is-a-test-title-2', $article2->slug); + } + + public function test_slugs_with_same_title_and_soft_deleted() + { + $article1 = Article::factory()->create(['title' => 'This is a test title']); + $this->assertEquals('this-is-a-test-title', $article1->slug); + $article1->delete(); + $article2 = Article::factory()->create(['title' => 'This is a test title']); + $this->assertEquals('this-is-a-test-title', $article2->slug); + $article3 = Article::factory()->create(['title' => 'This is a test title']); + $this->assertEquals('this-is-a-test-title-2', $article3->slug); + } } \ No newline at end of file From a3d5fa8090052f610b18a6677d33878d67d413bd Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 6 Feb 2024 01:40:36 +0545 Subject: [PATCH 8/8] test: Latitude and Longitude tests added --- tests/BaseTestCase.php | 3 ++ tests/Routes/LatLongRoutes.php | 22 ++++++++++++ tests/ValidationTest.php | 65 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 tests/Routes/LatLongRoutes.php create mode 100644 tests/ValidationTest.php diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index be08041..c5eb774 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -2,6 +2,7 @@ namespace AchyutN\LaravelHelpers\Tests; +use AchyutN\LaravelHelpers\Tests\Routes\LatLongRoutes; use Illuminate\Foundation\Testing\RefreshDatabase; use Orchestra\Testbench\TestCase as Orchestra; @@ -15,6 +16,8 @@ public function setUp(): void // test migrations $this->loadMigrationsFrom(__DIR__ . '/migrations'); $this->artisan('migrate')->run(); + + LatLongRoutes::setupLatLongRoutes($this->app->get('router')); } protected function getEnvironmentSetUp($app): void diff --git a/tests/Routes/LatLongRoutes.php b/tests/Routes/LatLongRoutes.php new file mode 100644 index 0000000..2c339e3 --- /dev/null +++ b/tests/Routes/LatLongRoutes.php @@ -0,0 +1,22 @@ +post('lat-long', function (Request $request) { + $validated = $request->validate([ + 'latitude' => new LatitudeRule, + 'longitude' => new LongitudeRule + ]); + return response()->json($validated); + }); + } +} \ No newline at end of file diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php new file mode 100644 index 0000000..d16db50 --- /dev/null +++ b/tests/ValidationTest.php @@ -0,0 +1,65 @@ +post('/lat-long', [ + 'latitude' => 27.7172, + 'longitude' => 85.3240 + ]); + $response->assertStatus(200); + } + + public function test_call_lat_long_route_with_string() + { + $response = $this->post('/lat-long', [ + 'latitude' => 'test', + 'longitude' => 'test' + ]); + $response->assertStatus(302); + $response->assertSessionHasErrors(['latitude']); + $response->assertSessionHasErrors(['longitude']); + + $this->assertEquals('The latitude must be a number.', session('errors')->get('latitude')[0]); + $this->assertEquals('The longitude must be a number.', session('errors')->get('longitude')[0]); + } + + public function test_call_lat_long_route_with_invalid_latitude() + { + $response = $this->post('/lat-long', [ + 'latitude' => 91, + 'longitude' => 85.3240 + ]); + $response->assertStatus(302); + $response->assertSessionHasErrors(['latitude']); + $this->assertEquals('The latitude must be between -90 and 90.', session('errors')->get('latitude')[0]); + } + + public function test_call_lat_long_route_with_invalid_longitude() + { + $response = $this->post('/lat-long', [ + 'latitude' => 27.7172, + 'longitude' => 181 + ]); + $response->assertStatus(302); + $response->assertSessionHasErrors(['longitude']); + $this->assertEquals('The longitude must be between -180 and 180.', session('errors')->get('longitude')[0]); + } + + public function test_call_lat_long_route_with_invalid_latitude_and_longitude() + { + $response = $this->post('/lat-long', [ + 'latitude' => 91, + 'longitude' => 181 + ]); + $response->assertStatus(302); + $response->assertSessionHasErrors(['latitude', 'longitude']); + $this->assertEquals('The latitude must be between -90 and 90.', session('errors')->get('latitude')[0]); + $this->assertEquals('The longitude must be between -180 and 180.', session('errors')->get('longitude')[0]); + } +} \ No newline at end of file