Skip to content

Commit

Permalink
Reorganize unit tests and add feature tests for blade view
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Jan 2, 2018
1 parent 1a5cfcb commit a2313ba
Show file tree
Hide file tree
Showing 19 changed files with 568 additions and 60 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"laravel/laravel": "5.5.*",
"mockery/mockery": "0.9.*",
"phpmd/phpmd": "^2.6",
"phpunit/phpunit": "5.7.*",
"phpunit/phpunit": "6.*",
"php-coveralls/php-coveralls" : "*",
"sebastian/phpcpd": "*"
"sebastian/phpcpd": "*",
"laravel/browser-kit-testing": "^2.0"
},
"autoload": {
"classmap": [],
Expand Down
4 changes: 2 additions & 2 deletions resources/views/impersonatees.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class="list-group-item"
{{ csrf_field () }}
{{ method_field ('PUT') }}

<button type="submit" class="btn btn-primary">
<i class="fa fa-lg fa-btn fa-user-secret"></i>
<button type="submit" class="btn btn-primary " name="{{ $user->name }}">
<span class="fa fa-lg fa-btn fa-user-secret"></span>
</button>
{{ $user->name }}
</form>
Expand Down
1 change: 1 addition & 0 deletions src/Http/Controllers/ImpersonateeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct()
public function index() : View
{
$this->authorize('impersonation', auth()->user());

$users = (new $this->userClass)->orderBy('name')
->get()
->filter(function ($user) {
Expand Down
22 changes: 17 additions & 5 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
use GeneaLabs\LaravelImpersonator\Providers\Service as LaravelImpersonatorService;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Eloquent\Factory;
use Illuminate\Support\Facades\Artisan;

trait CreatesApplication
{
public function createApplication()
{
$this->preLoadRoutes();
$this->copyFixtures([
__DIR__ . '/Fixtures/app/Http/Controllers/HomeController.php' => __DIR__ . '/../vendor/laravel/laravel/app/Http/Controllers/HomeController.php',
__DIR__ . '/Fixtures/resources/views/home.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/home.blade.php',
__DIR__ . '/Fixtures/resources/views/layouts/app.blade.php' => __DIR__ . '/../vendor/laravel/laravel/resources/views/layouts/app.blade.php',
__DIR__ . '/Fixtures/routes/web.php' => __DIR__ . '/../vendor/laravel/laravel/routes/web.php',
]);
$app = require __DIR__ . '/../vendor/laravel/laravel/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$app->make(Factory::class)->load(__DIR__ . '/Fixtures/database/factories');
Expand All @@ -17,10 +23,16 @@ public function createApplication()
return $app;
}

protected function preLoadRoutes()
protected function copyFixtures(array $fixtures)
{
$routes = file_get_contents(__DIR__ . '/../vendor/laravel/laravel/routes/web.php');
$routes .= str_contains($routes, 'Auth::routes();') ? '' : "\nAuth::routes();\n";
file_put_contents(__DIR__ . '/../vendor/laravel/laravel/routes/web.php', $routes);
$fixtures = collect($fixtures)
->each(function ($destination, $source) {
if (! file_exists(dirname($destination))) {
mkdir(dirname($destination), 0777, true);
}

$contents = file_get_contents($source);
file_put_contents($destination, $contents);
});
}
}
41 changes: 0 additions & 41 deletions tests/Feature/Console/Commands/PublishTest.php

This file was deleted.

56 changes: 56 additions & 0 deletions tests/Feature/ImpersonationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php namespace GeneaLabs\LaravelImpersonator\Tests\Feature;

use GeneaLabs\LaravelImpersonator\Tests\Fixtures\User;
use GeneaLabs\LaravelImpersonator\Tests\FeatureTestCase;

class ImpersonationTest extends FeatureTestCase
{
public function testImpersonatingPageLoads()
{
$user = factory(User::class)->create([
'canImpersonate' => true,
'canBeImpersonated' => false,
]);

$response = $this
->actingAs($user)
->get(route('impersonatees.index'));

$response->assertResponseOk();
}

public function testImpersonatableUsersAreListed()
{
config(['genealabs-laravel-impersonator.user-model' => User::class]);
$user = factory(User::class)->create([
'canImpersonate' => true,
'canBeImpersonated' => false,
]);
$users = factory(User::class, 10)->create();

$response = $this
->actingAs($user)
->get(route('impersonatees.index'));

$response->assertResponseOk();
foreach ($users as $user) {
$response->see(htmlspecialchars($user->name));
}
}

public function testUserCanBeImpersonated()
{
config(['genealabs-laravel-impersonator.user-model' => User::class]);
$user = factory(User::class)->create([
'canImpersonate' => true,
'canBeImpersonated' => false,
]);
$users = factory(User::class, 10)->create();

$response = $this->actingAs($user)
->visit(route('impersonatees.index'))
->press($users->first()->name);

$response->assertSessionHas('impersonator', $user);
}
}
13 changes: 13 additions & 0 deletions tests/FeatureTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace GeneaLabs\LaravelImpersonator\Tests;

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\BrowserKitTesting\TestCase;

abstract class FeatureTestCase extends TestCase
{
use CreatesApplication;
use DatabaseMigrations;
use DatabaseTransactions;
}
2 changes: 1 addition & 1 deletion tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class User extends OG
use Impersonatable;

protected $canImpersonateFlag = false;
protected $canBeImpersonatedFlag = false;
protected $canBeImpersonatedFlag = true;

public function getCanImpersonateAttribute() : bool
{
Expand Down
28 changes: 28 additions & 0 deletions tests/Fixtures/app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}

/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
69 changes: 69 additions & 0 deletions tests/Fixtures/resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>

<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>

<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>

@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>

<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>

<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
47 changes: 47 additions & 0 deletions tests/Fixtures/resources/views/auth/passwords/email.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Password</div>

<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif

<form class="form-horizontal" method="POST" action="{{ route('password.email') }}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Send Password Reset Link
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

0 comments on commit a2313ba

Please sign in to comment.