Software QA FYI - SQAFYI

Test Better, Deploy Faster with Laravel

By: Voja Janjic

Laravel is an increasingly popular PHP framework that can help you develop better, more sustainable code in a shorter amount of time. Although the first part of this article explained the basic features of Laravel, this part will cover more advanced techniques, such as application testing and deployment. Some of the features that will be shown in the following sections are: installing sample data, database migrations, writing unit tests, and application deployment.

Database Seeding in Laravel
Database seeding is the process of adding the sample test data into the database. That process is made simple by using Laravel's built-in seeding class. For example, let's seed the table with user data; it is done by creating a new seeder class that inherits the default DatabaseSeeder class in the database/seeds folder:
Unit Testing in Laravel
Unit testing is a software testing method where individual units of code are tested to see whether they are ready to be used in production. Laravel is built with unit testing support; PHPUnit is included out of the box. Also, additional libraries are included to allow you to manipulate views and simulate a web browser during testing.
To create a unit test, create a new class that extends TestCase class in the app/tests directory:
class MyFirstTest extends TestCase {
// Write testing functions here
}

Because the tests are database-agnostic (meaning that they can work with any database driver), we need to do the database schema migration before running the test:

class TestCase extends Illuminate\Foundation\Testing\TestCase {
// Prepare the test
public function setUp()
{
parent::setUp();

$this->prepareForTests();
}

// Create the application
public function createApplication()
{
$unitTesting = true;

$testEnvironment = 'testing';

return require __DIR__.'/../../bootstrap/start.php'; }

// Migrate the database
private function prepareForTests()
{
Artisan::call('migrate');
}

}

Now, we are ready to write a test. Let's see an example where we test whether the username is required and whether the correct error message is shown:
public function MyFirstTest()
{
// Create a new User
$user = new User;
$user->email = "login@email.com";
$user->password = "password";

// User should not save
$this->assertFalse($user->save());

// Save the errors
$errors = $user->errors()->all();

// There should be 1 error
$this->assertCount(1, $errors);

// The username error should be set
$this->assertEquals($errors[0], "The username field is required.");
}


Note that assertion functions are used to test for values. For example, if the assertFalse function finds a value that is "true", the test would fail. For more information on assertion functions and other testing functions, check this page.

Deploying Your Laravel Application
Laravel provides quick application deployment by using Laravel Forge. Although it is a paid service ($10 per month), it enables developers to quickly configure servers on DigitalOcean, Linode, Rackspace, and Amazon EC2. Forge also has many advanced features, such as deployment from the Git repository or sub-domain configuration.

Full article...


Other Resource

... to read more articles, visit http://sqa.fyicenter.com/art/

Test Better, Deploy Faster with Laravel