Testavimas su Laravel 5.1
Justas Maziliauskas, maziliauskas.lt
Žalias
package.json
{
"private": true,
"devDependencies": {
"gulp": "^3.8.8"
},
"dependencies": {
"laravel-elixir": "^3.0.0"
}
}
npm install
var elixir = require('laravel-elixir');
elixir(function (mix) {
mix.phpUnit();
});
gulpfile.js
vagrant@homestead:~/projects/meetup-tests$ gulp
[07:40:40] Using gulpfile ~/projects/meetup-tests/gulpfile.js
[07:40:40] Starting 'default'...
[07:40:40] Starting 'phpUnit'...
Fetching PhpUnit Source Files...
- tests/**/*Test.php
[07:40:40] Finished 'default' after 23 ms
[07:40:40]
*** Debug Cmd: ./vendor/bin/phpunit --colors --debug -c phpunit.xml ***
[07:40:41] PHPUnit 4.8.18 by Sebastian Bergmann and contributors.
Starting test 'ExampleTest::testBasicExample'.
.
Time: 811 ms, Memory: 13.00Mb
OK (1 test, 2 assertions)
[07:40:41] gulp-notify: [Green!] Your phpUnit tests passed!
[07:40:41] Finished 'phpUnit' after 921 ms
class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5');
}
}
tests/ExampleTest.php
<body>
<div class="container">
<div class="content">
<div class="title">Laravel 6</div>
</div>
</div>
</body>
welcome.blade.php
vagrant@homestead:~/projects/meetup-tests$ gulp
[07:57:19] Using gulpfile ~/projects/meetup-tests/gulpfile.js
[07:57:19] Starting 'default'...
[07:57:19] Starting 'phpUnit'...
Starting test 'ExampleTest::testBasicExample'.
F
There was 1 failure:
1) ExampleTest::testBasicExample
Failed asserting that '<!DOCTYPE html>
<...>
<body>
<div class="container">
<div class="content">
<div class="title">Laravel 6</div>
</div>
</div>
</body>
</html>
' matches PCRE pattern "/Laravel 5/i".
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
[07:57:21] gulp-notify: [Red!] Your phpUnit tests failed!
[07:57:21] Finished 'phpUnit' after 2.21 s
<div class="content">
<div class="title">Laravel 5</div>
<div class="title">Laravel 6</div>
</div>
[08:07:51] gulp-notify: [Green!] Your phpUnit tests passed!
class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5');
}
}
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5')
->dontSee('Laravel 6');
}
[08:11:17] gulp-notify: [Red!] Your phpUnit tests failed!
<div class="title">Laravel 5</div>
<div class="title">Laravel 6</div>
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5')
->dontSee('Laravel 6');
}
<div class="title">Laravel 5</div>
{{--<div class="title">Laravel 6</div>--}}
[08:07:51] gulp-notify: [Green!] Your phpUnit tests passed!
Create new test
pa make:test FormSubmitTest
*pa - php artisan
routes.php
get('auth/register', 'Auth\AuthController@getRegister');
post('auth/register', 'Auth\AuthController@postRegister');
get('dashboard', function () {
return 'OK';
});
register.blade.php
<form method="POST" action="/auth/register">
{!! csrf_field() !!}
<div>
Name
<input type="text" name="name" value="{{ old('name') }}">
</div>
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password">
</div>
<div>
Confirm Password
<input type="password" name="password_confirmation">
</div>
<div>
<button type="submit">Register</button>
</div>
</form>
FormSubmitTest.php
public function testNewUserRegistration()
{
$this->visit('auth/register')
->type('Justas', 'name')
->type('justas@maziliauskas.lt', 'email')
->type('hello1', 'password')
->type('hello1', 'password_confirmation')
->press('Register')
->seePageIs('/dashboard');
}
vagrant@homestead:~/projects/meetup-tests$ gulp
[09:35:46] Using gulpfile ~/projects/meetup-tests/gulpfile.js
[09:35:46] Starting 'default'...
[09:35:46] Starting 'phpUnit'...
Starting test 'ExampleTest::testBasicExample'.
.
Starting test 'FormSubmitTest::testNewUserRegistration'.
F
There was 1 failure:
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard'
+'http://localhost/home'
1) FormSubmitTest::testNewUserRegistration
Did not land on expected page [http://localhost/dashboard].
FAILURES!
Tests: 2, Assertions: 8, Failures: 1.
[09:35:47] gulp-notify: [Red!] Your phpUnit tests failed!
[09:35:47] Finished 'phpUnit' after 985 ms
protected $redirectPath = '/dashboard';
AuthController.php
vagrant@homestead:~/projects/meetup-tests$ gulp
[09:43:50] gulp-notify: [Green!] Your phpUnit tests passed!
[09:43:50] Finished 'phpUnit' after 1.2 s
vagrant@homestead:~/projects/meetup-tests$ gulp
There was 1 failure:
1) FormSubmitTest::testNewUserRegistration
Did not land on expected page [http://localhost/dashboard].
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard'
+'http://localhost/auth/register'
[09:44:35] gulp-notify: [Red!] Your phpUnit tests failed!
[09:44:35] Finished 'phpUnit' after 1.72 s
ONE MORE TIME
?
vagrant@homestead:~/projects/meetup-tests$ pa tinker
Psy Shell v0.6.1 (PHP 5.6.15-1+deb.sury.org~trusty+1 — cli) by Justin Hileman
>>> \App\User::all();
=> Illuminate\Database\Eloquent\Collection {#683
all: [
App\User {#684
id: 1,
name: "Justas",
email: "justas@maziliauskas.lt",
created_at: "2015-11-27 10:27:25",
updated_at: "2015-11-27 10:27:25",
},
],
}
>>> q
Exit: Goodbye.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
class FormSubmitTest extends TestCase
{
use DatabaseTransactions;
public function testNewUserRegistration()
{
$this->visit('auth/register')
->type('Justas', 'name')
->type('justas@maziliauskas.lt', 'email')
->type('hello1', 'password')
->type('hello1', 'password_confirmation')
->press('Register')
->seePageIs('/dashboard');
}
}
vagrant@homestead:~/projects/meetup-tests$ gulp
[09:43:50] gulp-notify: [Green!] Your phpUnit tests passed!
[09:43:50] Finished 'phpUnit' after 1.2 s
vagrant@homestead:~/projects/meetup-tests$ gulp
[09:44:50] gulp-notify: [Green!] Your phpUnit tests passed!
[09:44:50] Finished 'phpUnit' after 1.3 s
SELECT AUTO_INCREMENT
FROM information_schema.tables
WHERE table_name = 'users'
AND table_schema = DATABASE( ) ;
// 1
SELECT AUTO_INCREMENT
FROM information_schema.tables
WHERE table_name = 'users'
AND table_schema = DATABASE( ) ;
// 2
class FormSubmitTest extends TestCase
{
use DatabaseMigrations;
public function testNewUserRegistration()
{
$this->visit('auth/register')
->type('Justas', 'name')
->type('justas@maziliauskas.lt', 'email')
->type('hello1', 'password')
->type('hello1', 'password_confirmation')
->press('Register')
->seePageIs('/dashboard');
}
}
vagrant@homestead:~/projects/meetup-tests$ gulp
[09:43:50] gulp-notify: [Green!] Your phpUnit tests passed!
[09:43:50] Finished 'phpUnit' after 3 s
vagrant@homestead:~/projects/meetup-tests$ gulp
[09:44:50] gulp-notify: [Green!] Your phpUnit tests passed!
[09:44:50] Finished 'phpUnit' after 2.8 s
SHOW TABLES;
// migrations
SHOW TABLES;
// migrations
API testavimas
API ??? :)
vagrant@homestead:~/projects/meetup-tests$ pa make:test APITest
Test created successfully.
vagrant@homestead:~/projects/meetup-tests$ pa make:model Post -m
Model created successfully.
Created Migration: 2015_11_27_112705_create_posts_table
vagrant@homestead:~/projects/meetup-tests$ pa make:controller PostsController
Controller created successfully.
// routes.php
resource('posts', 'PostsController', ['except' => ['create', 'edit']]);
// 2015_11_27_112705_create_posts_table.php
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->text('body');
$table->timestamps();
});
}
class APITest extends TestCase
{
use DatabaseMigrations; // new database every test
use WithoutMiddleware; // CSRF token
}
seeJson($arr)
seeJsonEquals($arr)
assertResponseStatus($statusCode)
public function index()
{
return Post::all();
}
//test
public function testPostsList()
{
$this->visit('/posts')->seeJsonEquals([]);
}
[Green!] Your phpUnit tests passed!
public function store(Request $request)
{
if (Post::create($request->all())) {
return response(['created' => true], 201);
}
return response('ERROR', 500);
}
//test
public function testSavePost()
{
$this->post('/posts', ['body' => 'Post Body'])
->seeJson(['created' => true,]);
}
[Green!] Your phpUnit tests passed!
public function store(Request $request)
{
if (Post::create($request->all())) {
return response(['created' => true], 201);
}
return response('ERROR', 500);
}
//test
public function testEmptySavePost()
{
$this->post('/posts', ['body' => null])
->assertResponseStatus(500);
}
[Green!] Your phpUnit tests passed!
public function show($id)
{
return Post::findOrFail($id);
}
//test
public function testShowPost()
{
$post = \App\Post::create(['body' => 'Test Body']);
$this->get('/posts/' . $post->id)
->seeJsonEquals($post->toArray());
}
[Green!] Your phpUnit tests passed!
public function show($id)
{
return Post::findOrFail($id);
}
//test
public function testShowPost()
{
$post = \App\Post::create(['body' => 'Test Body']);
$this->get('/posts/' . $post->id)
->seeJsonEquals($post->toArray());
}
[Green!] Your phpUnit tests passed!
public function update(Request $request, $id)
{
$post = Post::find($id);
if ($post->update($request->all())) {
return response()->json(['updated' => true]);
};
return response('ERROR', 500);
}
//test
public function testUpdatePost()
{
$post = \App\Post::create(['body' => 'Test Body']);
$this->patch('/posts/' . $post->id, ['body' => 'New Body'])
->seeJson(['updated' => true]);
}
[Green!] Your phpUnit tests passed!
public function destroy($id)
{
Post::destroy($id);
return response()->json(['deleted' => true]);
}
//test
public function testDestroyPost()
{
$post = \App\Post::create(['body' => 'Test Body']);
$this->delete('/posts/' . $post->id)
->seeJson(['deleted' => true]);
$this->visit('/posts')
->seeJsonEquals([]);
}
[Green!] Your phpUnit tests passed!
vagrant@homestead:~/projects/meetup-tests$ gulp
[13:19:38] Using gulpfile ~/projects/meetup-tests/gulpfile.js
[13:19:38] Starting 'default'...
[13:19:38] Starting 'phpUnit'...
Fetching PhpUnit Source Files...
- tests/**/*Test.php
[13:19:38] Finished 'default' after 18 ms
[13:19:38]
*** Debug Cmd: ./vendor/bin/phpunit --colors --debug -c phpunit.xml ***
[13:19:40] PHPUnit 4.8.18 by Sebastian Bergmann and contributors.
Starting test 'APITest::testPostsList'.
.
Starting test 'APITest::testSavePost'.
.
Starting test 'APITest::testEmptySavePost'.
.
Starting test 'APITest::testShowPost'.
.
Starting test 'APITest::testUpdatePost'.
.
Starting test 'APITest::testDestroyPost'.
.
Starting test 'ExampleTest::testBasicExample'.
.
Starting test 'FormSubmitTest::testNewUserRegistration'.
.
Time: 1.83 seconds, Memory: 23.25Mb
OK (8 tests, 17 assertions)
[13:19:40] gulp-notify: [Green!] Your phpUnit tests passed!
[13:19:40] Finished 'phpUnit' after 1.98 s
https://github.com/johnkary/phpunit-speedtrap
Copy of Kaip aš testuoju?
By boxcore
Copy of Kaip aš testuoju?
- 385