laravel-testing-deploying-authentication
creating user and permission
CREATE DATABASE databasename;
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password'
GRANT ALL ON database.* TO 'user'@'localhost';
mysql -uusername -ppassword -D databasename
setting up old laravel project
composer install
php artisan cache:clear
php artisan config:cache
php artisan migrate
importing database
mysql -uroot -ppassword -D databasename < database.sql
TTD
- Unit test
- functional test
tdd tools
- phpunit - most popular unit test
- phpSpec behavior driven test. test like a story
- Codeception - unit, functional, acceptance test
- Selenium browser test
- Behat symphony based component which heavily used for Behavior driven development
tdd deployment workflow
Staging environment just like production without real customer data in the database
Unit test => Isolated component test
public function testTitlesModelCount()
{
$titles = new Title;
$this->assertTrue(count($titles->all()) === 6, 'It should have 6 titles');
}
public function testLastTitleMustBeProfessor()
{
$titles = new Title;
$titles_array = $titles->all();
$this->assertEqual('Professor', array_pop($titles_array), 'Titles last element must to be professor');
}
functional test - Grouped component test
public function testNewClientForm()
{
$response = $this->get('/clients/new');
$response->assertStatus(200);
}
public function testProfessorOption()
{
$response = $this->get('/clients/new');
$this->assertContains('Professor', $response->getContent(), 'HTML should have Professor');
}
handle volatile data
Using session
- Temporary data
- Based on cookies
$request->session()->put('key', 'value');
$request->session()->pull('key');
$request->session()->get('key');
$request->session()->has('key');
$request->session()->forget('key');
csrf token
we can disable from karnel or inside VerifyCsrfToken.php
protected $except = ['*']
csrf token inside form
{{csrf_field()}}
adding middle ware
# Process 1
Route::get('/', 'PageController@index')->name('home')->middleware('auth');
# Process 2
Route::middleware('auth')->group(function () {
Route::get('/', 'PageController@index')->name('home');
});
# Process 3
Route::group(['middleware' => 'auth'], function () {
Route::get('/', 'PageController@index')->name('home');
});
# Process 4
Route::get('/', [
'uses' => 'PageController@index',
'as' => 'home',
'middleware' => 'auth'
]);
exporting table
It will export html table
public function export()
{
$data = [];
$data['clients'] = $this->client->all();
header('Content-disposition: attachment;filename=export.xls');
return view('client/export', $data);
}
uploading file
cheking method type
$request->isMethod('post');
html part
<form action="/upload" method="post" enctype="multipart/form-data" >
<div class="form-group">
{{csrf_field()}}
<label for="file">Upload</label>
<input type="text" name="file" id="file" class="form-control">
<input type="submit" class="btn btn-info">
</div>
</form>
php part
use Illuminate\Support\Facade\Input;
class GenericController {
public function upload(Request $request)
{
$data = [];
if ($request->isMethod('post')) {
$this->validate($request, [
'file' => 'mimes:jpeg,bmp,png'
]);
Input::file('file')->move('images_folder which is inside public folder', 'imagename.jpg');
}
}
}
Seeder
make a seeder class using php artisan
php artisan make:seeder artisan
seeding data inside seeder class
public function run()
{
DB::table('users')->insert([
'name' => 'shibu',
'email' => 'polodev10@gmail.com'
]);
}
calling seeder class from databaseSeeder class
public function run()
{
$this->call(UserTableSeeder::class);
}
to seed database
php artisan db:seed
migration with seed
php artisan migrate --seed
php artisan migrate:refresh --seed
homested and vagrant
download homested vagrant box
vagrant box add laravel/homestead
git clone homestead from laravel official website
git clone https://homestead_link
checkout to laravel stable one
git checkout v5.5.0
run init.sh
file for linux or mac user. run init.bat
for windows user to generate Homestead.yaml file
bash init.sh
Now generate rsa key (in windows we have to use puttyGen)
mkdir ssh_vagrant
cd ssh_vagrant
ssh-keygen -t rsa
after typing ssh-keygen -t rsa
we will pass the ssh_vagrant folder
absolute path which can be found by pwd
command . with suffix /id_rsa
/Users/polodev/Desktop/ssh_vagrant/id_rsa
inside Homestead.yaml file
authorize: /Users/polodev/Desktop/ssh_vagrant/id_rsa.pub
keys:
- /Users/polodev/Desktop/ssh_vagrant/id_rsa
configure folders
and sites
also inside Homestead.yaml file
now give credential to .env
file.
now start vagrant and do followings
vagrant up
vagrant ssh
cd code
php artisan cache:clear
php artisan config
php artisan migrate
php artisan db:seed