-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from achyutkneupane/tests
test: Test for all helpers and features added
- Loading branch information
Showing
11 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests; | ||
|
||
use AchyutN\LaravelHelpers\Tests\Routes\LatLongRoutes; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
abstract class BaseTestCase extends Orchestra | ||
{ | ||
use RefreshDatabase; | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
// test migrations | ||
$this->loadMigrationsFrom(__DIR__ . '/migrations'); | ||
$this->artisan('migrate')->run(); | ||
|
||
LatLongRoutes::setupLatLongRoutes($this->app->get('router')); | ||
} | ||
|
||
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'); | ||
|
||
// sluggable config | ||
$app['config']->set('sluggable', [ | ||
'source' => null, | ||
'onUpdate' => false, | ||
'separator' => '-', | ||
'method' => null, | ||
'maxLength' => null, | ||
'maxLengthKeepWords' => true, | ||
'unique' => true, | ||
'slugEngineOptions' => [], | ||
'reserved' => null, | ||
'includeTrashed' => false, | ||
'uniqueSuffix' => null, | ||
'firstUniqueSuffix' => 2, | ||
]); | ||
} | ||
|
||
public function getPackageProviders($app): array | ||
{ | ||
return [ | ||
\AchyutN\LaravelHelpers\LaravelHelperProvider::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests\Factories; | ||
|
||
use AchyutN\LaravelHelpers\Tests\Models\Article; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class ArticleFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var class-string<\Illuminate\Database\Eloquent\Model> | ||
*/ | ||
protected $model = Article::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'title' => $this->faker->sentence, | ||
'content' => $this->faker->paragraph | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests; | ||
|
||
use AchyutN\LaravelHelpers\Tests\Models\Article; | ||
|
||
class InactiveTest extends BaseTestCase | ||
{ | ||
public function test_change_to_inactive() | ||
{ | ||
$article = Article::factory()->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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests\Models; | ||
|
||
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, HasTheSlug; | ||
protected $guarded = []; | ||
protected static function factory(int $count = 1): Factory | ||
{ | ||
if($count && $count > 1) { | ||
return ArticleFactory::times($count); | ||
} else { | ||
return ArticleFactory::new(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests\Routes; | ||
|
||
use AchyutN\LaravelHelpers\Rules\LatitudeRule; | ||
use AchyutN\LaravelHelpers\Rules\LongitudeRule; | ||
use AchyutN\LaravelHelpers\Tests\BaseTestCase; | ||
use Illuminate\Http\Request; | ||
|
||
class LatLongRoutes extends BaseTestCase | ||
{ | ||
public static function setupLatLongRoutes($router): void | ||
{ | ||
$router->post('lat-long', function (Request $request) { | ||
$validated = $request->validate([ | ||
'latitude' => new LatitudeRule, | ||
'longitude' => new LongitudeRule | ||
]); | ||
return response()->json($validated); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests; | ||
|
||
use AchyutN\LaravelHelpers\Tests\BaseTestCase; | ||
use AchyutN\LaravelHelpers\Tests\Models\Article; | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests; | ||
|
||
use AchyutN\LaravelHelpers\Tests\BaseTestCase; | ||
|
||
class ValidationTest extends BaseTestCase | ||
{ | ||
public function test_call_lat_long_route_with_valid_data() | ||
{ | ||
$response = $this->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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create("articles", function (Blueprint $table) { | ||
$table->id(); | ||
$table->string("title"); | ||
$table->string('slug')->nullable(); | ||
$table->text("content"); | ||
$table->dateTime('inactive_at')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists("articles"); | ||
} | ||
}; |