Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Test for all helpers and features added #17

Merged
merged 8 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"AchyutN\\LaravelHelpers\\Tests\\": "tests/"
}
},
"authors": [
Expand All @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/LaravelHelperProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions tests/BaseTestCase.php
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,
];
}
}
29 changes: 29 additions & 0 deletions tests/Factories/ArticleFactory.php
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
];
}
}
32 changes: 32 additions & 0 deletions tests/InactiveTest.php
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());
}
}
23 changes: 23 additions & 0 deletions tests/Models/Article.php
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();
}
}
}
6 changes: 2 additions & 4 deletions tests/NepaliHelpersTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php

namespace Tests;
namespace AchyutN\LaravelHelpers\Tests;

use PHPUnit\Framework\TestCase;

class NepaliHelpersTest extends TestCase
class NepaliHelpersTest extends BaseTestCase
{
public function test_english_nepali_number()
{
Expand Down
22 changes: 22 additions & 0 deletions tests/Routes/LatLongRoutes.php
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);
});
}
}
43 changes: 43 additions & 0 deletions tests/SlugTest.php
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);
}
}
65 changes: 65 additions & 0 deletions tests/ValidationTest.php
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]);
}
}
35 changes: 35 additions & 0 deletions tests/migrations/articles_table.php
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");
}
};
Loading