Skip to content

Commit

Permalink
Merge pull request #3 from knash94/feature-unit-testing
Browse files Browse the repository at this point in the history
Feature unit testing
  • Loading branch information
knash94 authored Mar 18, 2018
2 parents 399c2d3 + 9c42dc1 commit 4de197f
Show file tree
Hide file tree
Showing 13 changed files with 4,037 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~7.0",
"orchestra/testbench": "~3.0"
"orchestra/testbench": "~3.0",
"orchestra/testbench-browser-kit": "~3.6"
},
"license": "MIT",
"autoload": {
Expand Down
3,757 changes: 3,757 additions & 0 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config/seo-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'routing' => [
'prefix' => 'admin/seo-tools',
'namespace' => 'Knash94\Seo\Http\Controllers',
'middleware' => ['web', 'auth']
'middleware' => ['web']
],

'views' => [
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<includePath>.</includePath>
<server name="REQUEST_URI" value="http://dev.testing.com"/>
<server name="HTTP_HOST" value="dev.testing.com"/>
<env name="APP_DEBUG" value="true"/>
<env name="APP_KEY" value="base64:vJ+4f7tZjo0WlCewMCyYjx5PEpSF7+x7ZeNhbND5wdA="/>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
Expand Down
5 changes: 4 additions & 1 deletion src/SeoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ class SeoServiceProvider extends ServiceProvider {
*/
public function boot()
{
$this->registerRoutes();
$this->mergeConfigFrom(__DIR__.'/../config/seo-tools.php', 'seo-tools');

$this->registerRoutes();

$this->loadViewsFrom(__DIR__.'/../views', 'seo-tools');


$this->publishes([
__DIR__.'/../config/seo-tools.php' => config_path('seo-tools.php')
], 'config');
Expand Down
1 change: 0 additions & 1 deletion src/routes.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

Route::group([
'prefix' => config('seo-tools.routing.prefix'),
'namespace' => config('seo-tools.routing.namespace'),
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/Exception/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Knash94\Seo\Tests\Unit\Exception;

use Exception;
use Illuminate\Http\RedirectResponse;
use Knash94\Seo\Traits\LogsMissingPages;

class Handler extends \Orchestra\Testbench\Exceptions\Handler
{
/**
* This class is made to emulate an applications handler.php for testing purposed only.
*/

use LogsMissingPages;

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
*
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
$redirect = $this->reportNotFound($e);

if ($redirect && $redirect instanceof RedirectResponse) {
return $redirect;
}

return parent::render($request, $e);
}
}
37 changes: 37 additions & 0 deletions tests/Unit/Exception/HandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Knash94\Seo\Tests\Unit\Exception;


use Knash94\Seo\Store\Eloquent\Models\HttpError;
use Knash94\Seo\Store\Eloquent\Models\HttpRedirect;
use Knash94\Seo\Tests\Unit\TestCase;

class HandlerTest extends TestCase
{
/** @test */
public function it_logs_404_pages()
{
$request = $this->get('/test');


$request->assertResponseStatus(404);

$this->assertTrue(HttpError::where('path', 'test')->exists());
}

/** @test */
public function it_redirects_404()
{
factory(HttpRedirect::class)->create([
'path' => 'test',
'redirect_url' => 'https://www.google.com',
'status_code' => 302
]);

$request = $this->get('/test');

$request->assertResponseStatus(302);
$request->assertRedirectedTo('https://www.google.com');
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Http/Controllers/ErrorsControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Knash94\Seo\Tests\Unit\Http\Controllers;

use Knash94\Seo\Store\Eloquent\Models\HttpError;
use Knash94\Seo\Tests\Unit\TestCase;

class ErrorsControllerTest extends TestCase
{
/** @test */
public function it_returns_404_if_id_does_not_exist()
{
$request = $this->get(route('seo-tools.error.view', 1));

$request->assertResponseStatus(404);
}

/** @test */
public function it_returns_error_view_when_pass_id()
{
$error = factory(HttpError::class)->create();

$request = $this->get(route('seo-tools.error.view', $error->id));

$request->assertResponseStatus(200);
$request->assertViewHas('template');
$request->seeText('User Agent');
}

/** @test */
public function it_shows_edit_page()
{
$error = factory(HttpError::class)->create();

$request = $this->get(route('seo-tools.error.edit', $error->id));

$request->assertResponseStatus(200);
$request->assertViewHas('template');
$request->seeText('Managing');
}

/** @test */
public function it_updates_errors()
{
$error = factory(HttpError::class)->create();

$request = $this->post(
route('seo-tools.error.update', $error->id),
[
'redirect_url' => 'https://www.google.com',
'status_code' => 302
]
);

$request->assertResponseStatus(302);
$this->assertEquals($error->redirect->redirect_url, 'https://www.google.com');
}
}
91 changes: 91 additions & 0 deletions tests/Unit/Http/Controllers/RedirectControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
namespace Knash94\Seo\Tests\Unit\Http\Controllers;

use Knash94\Seo\Store\Eloquent\Models\HttpRedirect;
use Knash94\Seo\Tests\Unit\TestCase;

class RedirectControllerTest extends TestCase
{
/** @test */
public function it_returns_404_if_id_does_not_exist()
{
$request = $this->get(route('seo-tools.redirect.edit', 1));

$request->assertResponseStatus(404);
}

/** @test */
public function it_shows_edit_page()
{
$redirect = factory(HttpRedirect::class)->create();

$request = $this->get(route('seo-tools.redirect.edit', $redirect->id));

$request->assertResponseStatus(200);
$request->assertViewHas('template');
$request->seeText('Manage redirect');
}

/** @test */
public function it_updates_redirect()
{
$redirect = factory(HttpRedirect::class)->create();

$request = $this->post(
route('seo-tools.redirect.update', $redirect->id),
[
'redirect_url' => 'https://www.google.com',
'path' => '/test',
'status_code' => 302
]
);
$redirect = $redirect->fresh();

$request->assertResponseStatus(302);
$request->assertSessionHas('seo-tools.message');
$this->assertEquals($redirect->redirect_url, 'https://www.google.com');
}

/** @test */
public function it_creates_a_redirect()
{
$initialCount = HttpRedirect::count();

$request = $this->post(
route('seo-tools.redirect.store'),
[
'redirect_url' => 'https://www.google.com',
'path' => '/test',
'status_code' => 302
]
);

$request->assertResponseStatus(302);
$request->assertSessionHas('seo-tools.message');
$this->assertNotEquals($initialCount, HttpRedirect::count());
}

/** @test */
public function it_shows_delete_screen()
{
$redirect = factory(HttpRedirect::class)->create();

$request = $this->get(route('seo-tools.redirect.remove', $redirect->id));

$request->assertResponseStatus(200);
$request->assertViewHas('template');
$request->seeText('Delete redirect');
}

/** @test */
public function it_destroys_a_redirect()
{
$redirect = factory(HttpRedirect::class)->create();

$request = $this->get(route('seo-tools.redirect.destroy', $redirect->id));

$request->assertResponseStatus(302);
$request->assertSessionHas('seo-tools.message');
$this->assertEmpty(HttpRedirect::count());
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Http/Controllers/SeoRedirectControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Knash94\Seo\Tests\Unit\Http\Controllers;

use Knash94\Seo\Store\Eloquent\Models\HttpError;
use Knash94\Seo\Tests\Unit\TestCase;

class SeoRedirectControllerTest extends TestCase
{
/** @test */
public function it_returns_404_if_id_does_not_exist()
{
$request = $this->get(route('seo-tools.index'));

$request->assertResponseStatus(200);
$request->seeText('Redirect Manager');
}
}
4 changes: 3 additions & 1 deletion tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Knash94\Seo\Tests\Unit;

use Knash94\Seo\Tests\Unit\Exception\Handler;

class TestCase extends \Orchestra\Testbench\TestCase
class TestCase extends \Orchestra\Testbench\BrowserKit\TestCase
{
protected function setUp()
{
parent::setUp();
$this->loadMigrationsFrom(__DIR__ . '\..\..\database/migrations');
$this->loadFactories();
$this->app->singleton(\Illuminate\Contracts\Debug\ExceptionHandler::class, Handler::class);
}
/**
* Loads the service provider
Expand Down
31 changes: 31 additions & 0 deletions tests/config/seo-tools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

return [
'routing' => [
'prefix' => 'admin/seo-tools',
'namespace' => 'Knash94\Seo\Http\Controllers',
'middleware' => ['web', 'auth']
],

'views' => [
'template' => 'seo-tools::templates.default',
'section' => 'content',
'index' => 'seo-tools::bootstrap3.index',

'errors' => [
'edit' => 'seo-tools::bootstrap3.errors.edit',
'view' => 'seo-tools::bootstrap3.errors.view'
],

'redirects' => [
'edit' => 'seo-tools::bootstrap3.redirects.edit',
'delete' => 'seo-tools::bootstrap3.redirects.delete'
]
],

// Filters what URLs to not record using regex
'filters' => [
'/(\.png)|(\.jpg)|(\.gif)/',
'/wp\-login\.php/'
]
];

0 comments on commit 4de197f

Please sign in to comment.