Laravel Montréal #6 - February 12th, 2015
Thanks for comming !
Sponsors
Jobs
Get involved
Software development cycle that relies on the repetition of a very short development cycle.
" You can not create quality software without test driven development" - Kristofer Joseph
"It has become infeasible for a software developer to consider himself professional if he does not practice test-driven development.
"Don’t force yourself to test-first every controller, model, and view (my ratio is typically 20% test-first, 80% test-after)."
A very simple test of an isolated piece of functionality.
class Greeter {
public function greet($tags)
{
echo "Hello, World.";
}
}
class PracticeTest extends PHPUnit_Framework_TestCase {
public function testGreeterGreetsHelloWorld()
{
$greeter = new Greeter;
$this->assertTrue($greeter->greet() === 'Hello, World.');
}
}
An objects that allows you to simulate behaviour of another object.
Class Generator {
public function __construct(File $file)
{
$this->file = $file;
}
public function fire()
{
$this->file->put('foo.txt', 'Something here...');
}
}
public function testGeneratesFile() {
$mockedFile = Mockery::mock('File');
$mockedFile->shouldReceive('exists')
->once()
->andReturn(true);
$generator = new Generator($mockedFile);
$generator->fire();
}
An objects with a very specific set of data
public function testRegistrationEmailsUser() {
$fakeUser = new User();
$fakeUser->fill(['name' => 'Benjamin Gonzalez', 'email' => 'benjamin.rosell@gmail.com']);
$mockedMailer = Mockery::mock('File');
$mockedMailer->shouldReceive('exists')
->once()
->andReturn(true);
$generator = new Mailer($user, );
$generator->fire();
}
If a unit test verifies that code works correctly in isolation, then an integration test will fall on the other end of the spectrum. These tests will flex multiple parts of your application, and typically won’t rely on mocks or stubs.
Does this code meet the requirements of the client? Your software can pass all unit, functional, and integration tests, but still fail the acceptance tests, if the client or customer realizes that the feature doesn’t work as they expected.
./app/tests/
class FooTest extends TestCase {
public function testSomethingIsTrue()
{
$this->assertTrue(true);
}
}
Create a simple String calculator with a method int Add(string numbers). The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0). For example "" or "1" or "1,2"
Start with the simplest test case of an empty string and move to 1 and two numbers
Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
Remember to refactor after each passing test
Allow the Add method to handle an unknown amount of numbers
Allow the Add method to handle new lines between numbers (instead of commas).
the following input is ok: "1\n2,3" (will equal 6)
the following input is NOT ok: "1,\n" (not need to prove it - just clarifying)
Support different delimiters. To change a delimiter, the beginning of the string will contain a separate line that looks like this: [delimiter]\n[numbers...],
for example ;\n1;2 should return three where the default delimiter is ; .
he first line is optional. all existing scenarios should still be supported
Calling Add with a negative number will throw an exception "negatives not allowed" - and the negative that was passed. if there are multiple negatives, show all of them in the exception message/p>