Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkidn committed Sep 19, 2023
1 parent 8665601 commit fbbe71d
Show file tree
Hide file tree
Showing 21 changed files with 495 additions and 36 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"license": "MIT",
"require": {
"php": "^8.2",
"filament/filament": "^3.0",
"illuminate/contracts": "^10.0",
"maatwebsite/excel": "^3.1",
"spatie/eloquent-sortable": "^4.0",
"spatie/laravel-package-tools": "^1.12"
},
"require-dev": {
Expand All @@ -20,6 +23,7 @@
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"pestphp/pest-plugin-livewire": "^2.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0"
Expand Down
2 changes: 1 addition & 1 deletion database/factories/RedirectFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Codedor\FilamentUrlMapper\Database\Factories;
namespace Codedor\FilamentRedirects\Database\Factories;

use Codedor\FilamentRedirects\Models\Redirect;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\:\\:ordered\\(\\)\\.$#"
count: 1
path: src/Filament/RedirectResource.php
File renamed without changes.
12 changes: 0 additions & 12 deletions src/Filament/RedirectsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,4 @@ public function hasRedirectResource(): bool
// is retrieved from the plugin property.
return $this->hasRedirectResource;
}

public function menuBuilderPage(bool $condition = true): static
{
$this->hasMenuBuilderPage = $condition;

return $this;
}

public function hasMenuBuilderPage(): bool
{
return $this->hasMenuBuilderPage;
}
}
10 changes: 2 additions & 8 deletions src/Imports/RedirectsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ public function __construct()

public function collection(Collection $rows)
{
$created = null;
$updated = null;
$offlineCount = null;

$rows->each(function ($row) use (&$created, &$updated, &$offlineCount) {
$rows->each(function ($row) {
if ($row->has('from') && ($row['from'] !== null)) {
$from = $this->removeTrailingSlashes($row['from']);
$to = $this->removeTrailingSlashes($row['to']);

if ($from && $from !== $to) {
$urlMap = Redirect::updateOrCreate(
Redirect::updateOrCreate(
[
'from' => $from,
],
Expand All @@ -40,8 +36,6 @@ public function collection(Collection $rows)
'online' => 1,
]
);
$urlMap->wasRecentlyCreated ? $created++ : $created;
$urlMap->wasChanged() ? $updated++ : $updated;
}
}
});
Expand Down
11 changes: 10 additions & 1 deletion src/Models/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

namespace Codedor\FilamentRedirects\Models;

use Codedor\FilamentRedirects\Database\Factories\RedirectFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

/**
* @property string $from
* @property string $to
* @property int $sort_order
* @property int $status
* @property bool $online
* @property bool $pass_query_string
*/
class Redirect extends Model implements Sortable
{
use HasFactory;
Expand Down Expand Up @@ -53,6 +62,6 @@ public function getCleanFromAttribute()
*/
protected static function newFactory()
{
return new UrlMapFactory();
return new RedirectFactory();
}
}
6 changes: 3 additions & 3 deletions src/Observers/RedirectObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

class RedirectObserver
{
public function created(Redirect $domain)
public function created(Redirect $redirect)
{
Cache::forget('redirects');
}

public function updated(Redirect $domain)
public function updated(Redirect $redirect)
{
Cache::forget('redirects');
}

public function deleted(Redirect $domain)
public function deleted(Redirect $redirect)
{
Cache::forget('redirects');
}
Expand Down
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

103 changes: 103 additions & 0 deletions tests/Feature/Filament/RedirectResource/Pages/ManageRedirectsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

use Codedor\FilamentRedirects\Filament\RedirectResource\Pages\ManageRedirects;
use Codedor\FilamentRedirects\Models\Redirect;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Mockery\MockInterface;

use function Pest\Livewire\livewire;

beforeEach(function () {
$this->redirects = Redirect::factory()->createMany([
[
'from' => '/one',
'to' => '/two',
],
[
'from' => '/foo',
'to' => '/bar',
],
]);

$this->actingAs(\Codedor\FilamentRedirects\Tests\Fixtures\Models\User::factory()->create());
});

it('can list redirects', function () {
livewire(ManageRedirects::class)
->assertSuccessful()
->assertCanSeeTableRecords($this->redirects);
});

it('has an edit action', function () {
livewire(ManageRedirects::class)
->assertTableActionExists('edit');
});

it('has a delete action', function () {
livewire(ManageRedirects::class)
->assertTableActionExists('delete')
->assertTableBulkActionExists('delete');
});

it('has an import action that can throw an error', function () {
livewire(ManageRedirects::class)
->assertActionExists('import')
->callAction('import');

Notification::assertNotified('Something went wrong during the import');
});

it('has an import action that can truncate the table', function () {
Storage::disk('local')->put(
'import_redirects.xlsx',
file_get_contents(__DIR__ . '/../../../../Fixtures/import_redirects.xlsx', 'import_redirects.xlsx')
);

livewire(ManageRedirects::class)
->assertActionExists('import')
->callAction('import', ['file' => ['file' => 'import_redirects.xlsx'],
]);

Notification::assertNotified(
Notification::make()
->success()
->title('Import was successful')
);

$this->assertDatabaseCount(Redirect::class, 3);
$this->assertDatabaseHas(Redirect::class, [
'from' => '/from',
'to' => '/to',
'status' => 301,
]);
});

it('can create a redirect with validation errors', function () {
livewire(ManageRedirects::class)
->assertActionExists('create')
->callAction('create', [
'from' => '/from',
])
->assertHasActionErrors(['to' => 'required']);
});

it('can create a redirect', function () {
livewire(ManageRedirects::class)
->assertActionExists('create')
->callAction('create', [
'from' => '/from',
'to' => '/to',
'status' => 410
])
->assertHasNoActionErrors();

$this->assertDatabaseCount(Redirect::class, 3);
$this->assertDatabaseHas(Redirect::class, [
'from' => '/from',
'to' => '/to',
'status' => 410,
]);
});
21 changes: 21 additions & 0 deletions tests/Feature/Filament/RedirectResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Codedor\FilamentRedirects\Filament\RedirectResource;
use Codedor\FilamentRedirects\Models\Redirect;
use Codedor\FilamentRedirects\Tests\Fixtures\Models\User;

beforeEach(function () {
Redirect::factory()->create();

$this->actingAs(User::factory()->create());
});

it('has an index page', function () {
$this->get(RedirectResource::getUrl('index'))->assertSuccessful();
});

it('has only an index and edit action', function () {
expect(RedirectResource::getPages())
->toHaveCount(1)
->toHaveKeys(['index']);
});
133 changes: 133 additions & 0 deletions tests/Feature/Http/Middleware/RedirectsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php


use Codedor\FilamentRedirects\Models\Redirect;
use Symfony\Component\HttpKernel\Exception\HttpException;

it('will skip if it is a post request', function () {
$response = createResponse('/from', 'POST');

$this->assertEquals($response, null);
});

it('will not skip if it is a get request', function () {
Redirect::factory()->create([
'sort_order' => 1,
'from' => '/from',
'to' => '/to',
'status' => 302,
'pass_query_string' => false,
'online' => true,
]);

$response = createResponse('/from');

$this->assertEquals($response->getStatusCode(), 302);
$this->assertEquals($response->getTargetUrl(), 'http://localhost/to');
});

it('will only redirect for an online url map', function () {
Redirect::factory()->create([
'sort_order' => 1,
'from' => '/from',
'to' => '/to',
'status' => 302,
'pass_query_string' => false,
'online' => false,
]);

$response = createResponse('/from');

$this->assertEquals($response, null);
});

it('will keep sort order in account', function () {
Redirect::factory()->createQuietly([
'sort_order' => 2,
'from' => '/from',
'to' => '/to',
'status' => 302,
'pass_query_string' => false,
'online' => true,
]);
Redirect::factory()->createQuietly([
'sort_order' => 1,
'from' => '/from',
'to' => '/to-2',
'status' => 301,
'pass_query_string' => false,
'online' => true,
]);

$response = createResponse('/from');

$this->assertEquals($response->getStatusCode(), 301);
$this->assertEquals($response->getTargetUrl(), 'http://localhost/to-2');
});

it('will not redirect if no url map matches', function () {
Redirect::factory()->create([
'sort_order' => 1,
'from' => '/from',
'to' => '/to',
'status' => 302,
'pass_query_string' => false,
'online' => true,
]);

$response = createResponse('/to');

$this->assertEquals($response, null);
});

it('will redirect with query string', function () {
Redirect::factory()->create([
'sort_order' => 1,
'from' => '/from?query=string',
'to' => '/to',
'status' => 302,
'pass_query_string' => true,
'online' => true,
]);

$response = createResponse('/from?query=string');

$this->assertEquals($response->getStatusCode(), 302);
$this->assertEquals($response->getTargetUrl(), 'http://localhost/to?query=string');
});

it('will abort if status is 410', function () {
Redirect::factory()->create([
'sort_order' => 1,
'from' => '/from',
'to' => null,
'status' => 410,
'pass_query_string' => true,
'online' => true,
]);

$this->expectException(HttpException::class);

createResponse('/from');
});

it('will redirect with a wildcard', function () {
Redirect::factory()->create([
'sort_order' => 1,
'from' => '/from/*',
'to' => '/to',
'status' => 302,
'pass_query_string' => false,
'online' => true,
]);


$response = createResponse('/from');

$this->assertEquals($response, null);

$response2 = createResponse('/from/wildcard');

$this->assertEquals($response2->getStatusCode(), 302);
$this->assertEquals($response2->getTargetUrl(), 'http://localhost/to');
});
Loading

0 comments on commit fbbe71d

Please sign in to comment.