pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

154
PHPSpec & Behat: Two Testing Tools That Write Code For You Presented by Joshua Warren

Transcript of pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Page 1: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec & Behat: Two Testing Tools That Write Code For You

Presented by Joshua Warren

Page 2: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

OR:

Page 3: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You
Page 4: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

I heard you like to code, so let’s write code that writes

code while you code.

Page 5: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

About Me

Page 6: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHP DeveloperWorking with PHP since 1999

Page 7: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Founder & CEOFounded Creatuity in 2008

PHP Development Firm

Focused on the Magento platform

Page 8: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

JoshuaWarren.com

@JoshuaSWarren

Page 9: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

IMPORTANT!

• joind.in/14919

• Download slides

• Post comments

• Leave a rating!

Page 10: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

What You Need To Know

ASSUMPTIONS

Page 11: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Today we assume you’re a PHP developer.

Page 12: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

That you are familiar with test driven development.

Page 13: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

And that you’ve at least tried PHPUnit, Selenium or another testing tool.

Page 14: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

BDD - no, the B does not stand for beer, despite what a Brit might tell you

Behavior Driven Development

Page 15: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Think of BDD as stepping up a level from TDD.

Page 17: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

TDD generally deals with functional units.

Page 18: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

BDD steps up a level to consider complete features.

Page 19: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

In BDD, you write feature files in the form of user stories that you test against.

Page 20: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

BDD uses a ubiquitous language - basically, a language that business stakeholders, project

managers, developers and our automated tools can all understand.

Page 21: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Sample Behat Feature FileFeature: Up and Running In order to confirm Behat is Working As a developer I need to see a homepage Scenario: Homepage Exists When I go to "/bdd/" Then I should see "Welcome to the world of BDD"

Page 22: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

BDD gets all stakeholders to agree on what “done” looks like before you write a single line of code

Page 23: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat

Page 24: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

We implement BDD in PHP with a tool called Behat

Page 25: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat is a free, open source tool designed for BDD and PHP

Page 26: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

behat.org

Page 27: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

SpecBDD - aka, Testing Tongue Twisters

Specification Behavior Driven Development

Page 28: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Before you write a line of code, you write a specification for how that code should work

Page 29: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Focuses you on architectural decisions up-front

Page 30: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec

Page 31: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Open Source tool for specification driven development in PHP

Page 32: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

www.phpspec.net

Page 33: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Why Use Behat and PHPSpec?

Page 34: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

These tools allow you to focus exclusively on logic

Page 35: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Helps build functional testing coverage quickly

Page 36: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Guides planning and ensuring that all stakeholders are in agreement

Page 37: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Why Not PHPUnit?

Page 38: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec is opinionated - in every sense of the word

Page 39: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec forces you to think differently and creates a mindset that encourages usage

Page 40: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec tests are much more readable

Page 41: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Read any of Marcello Duarte’s slides on testing

Page 42: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

What About Performance?

Page 43: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Tests that take days to run won’t be used

Page 44: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec is fast

Page 45: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat supports parallel execution

Page 46: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat and PHPSpec will be at least as fast as the existing testing tools, and can be much faster

Page 47: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Enough Theory:Let’s Build Something!

Page 48: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

We’ll be building a basic time-off request app.

Page 49: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Visitors can specify their name and a reason for their time off request.

Page 50: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Time off requests can be viewed, approved and denied.

Page 51: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Intentionally keeping things simple, but you can follow this pattern to add authentication,

roles, etc.

Page 52: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Want to follow along or view the sample code?

Page 53: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Vagrant box:

https://github.com/joshuaswarren/bdd-box

Project code:

https://github.com/joshuaswarren/bdd

Page 54: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Setting up Our Project

Page 55: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Setup a folder for your project

Page 56: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Use composer to install Behat, phpspec & friends

Page 57: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

composer require behat/behat —dev

Page 58: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

composer require behat/mink-goutte-driver —dev

Page 59: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

composer require phpspec/phpspec —dev

Page 60: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

We now have Behat and Phpspec installed

Page 61: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

We also have Mink - an open source browser emulator/controller

Page 62: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Mink Drivers

Goutte - headless, fast, no JS

Selenium2 - requires Selenium server, slower, supports JS

Zombie - headless, fast, does support JS

Page 63: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

We are using Goutte today because we don’t need Javascript support

Page 64: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

We’ll perform some basic configuration to let Behat know to use Goutte

Page 65: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

And we need to let phpspec know where our code should go

Page 66: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Run:

vendor/bin/behat —init

Page 67: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Create /behat.yml

default: extensions: Behat\MinkExtension: base_url: http://192.168.33.10/ default_session: goutte goutte: ~

Page 68: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/bootstrap/FeatureContext.php

use Behat\Behat\Context\Context;use Behat\Behat\Context\SnippetAcceptingContext;use Behat\Gherkin\Node\PyStringNode;use Behat\Gherkin\Node\TableNode;use Behat\MinkExtension\Context\MinkContext;/** * Defines application features from the specific context. */class FeatureContext extends Behat\MinkExtension\Context\MinkContext {}

Page 69: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Create /phpspec.yml

suites: app_suites: namespace: App psr4_prefix: App src_path: app

Page 70: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Features

Page 71: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/UpAndRunning.featureFeature: Up and Running In order to confirm Behat is Working As a developer I need to see a homepage Scenario: Homepage Exists When I go to "/bdd/" Then I should see "Welcome to the world of BDD"

Page 72: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Run:

bin/behat

Page 73: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/SubmitTimeOffRequest.featureFeature: Submit Time Off Request In order to request time off As a developer I need to be able to fill out a time off request form Scenario: Time Off Request Form Exists When I go to "/bdd/timeoff/new" Then I should see "New Time Off Request" Scenario: Time Off Request Form Works When I go to "/bdd/timeoff/new" And I fill in "name" with "Josh" And I fill in "reason" with "Attending a great conference" And I press "submit" Then I should see "Time Off Request Submitted"

Page 74: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/SubmitTimeOffRequest.featureFeature: Submit Time Off Request In order to request time off As a developer I need to be able to fill out a time off request form Scenario: Time Off Request Form Exists When I go to "/bdd/timeoff/new" Then I should see "New Time Off Request" Scenario: Time Off Request Form Works When I go to "/bdd/timeoff/new" And I fill in "name" with "Josh" And I fill in "reason" with "Attending a great conference" And I press "submit" Then I should see "Time Off Request Submitted"

Page 75: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/SubmitTimeOffRequest.featureFeature: Submit Time Off Request In order to request time off As a developer I need to be able to fill out a time off request form Scenario: Time Off Request Form Exists When I go to "/bdd/timeoff/new" Then I should see "New Time Off Request" Scenario: Time Off Request Form Works When I go to "/bdd/timeoff/new" And I fill in "name" with "Josh" And I fill in "reason" with "Attending a great conference" And I press "submit" Then I should see "Time Off Request Submitted"

Page 76: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/SubmitTimeOffRequest.featureFeature: Submit Time Off Request In order to request time off As a developer I need to be able to fill out a time off request form Scenario: Time Off Request Form Exists When I go to "/bdd/timeoff/new" Then I should see "New Time Off Request" Scenario: Time Off Request Form Works When I go to "/bdd/timeoff/new" And I fill in "name" with "Josh" And I fill in "reason" with "Attending a great conference" And I press "submit" Then I should see "Time Off Request Submitted"

Page 77: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/ProcessTimeOffRequest.featureFeature: Process Time Off Request In order to manage my team As a manager I need to be able to approve and deny time off requests Scenario: Time Off Request Management View Exists When I go to "/bdd/timeoff/manage" Then I should see "Manage Time Off Requests" Scenario: Time Off Request List When I go to "/bdd/timeoff/manage" And I press "View" Then I should see "Pending Time Off Request Details" Scenario: Approve Time Off Request When I go to "/bdd/timeoff/manage" And I press "View" And I press "Approve" Then I should see "Time Off Request Approved" Scenario: Deny Time Off Request When I go to "/bdd/timeoff/manage" And I press "View" And I press "Deny" Then I should see "Time Off Request Denied"

Page 78: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/ProcessTimeOffRequest.feature

Feature: Process Time Off Request In order to manage my team As a manager I need to be able to approve and deny time off requests

Page 79: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/ProcessTimeOffRequest.feature

Scenario: Time Off Request Management View Exists When I go to "/bdd/timeoff/manage" Then I should see "Manage Time Off Requests" Scenario: Time Off Request List When I go to "/bdd/timeoff/manage" And I press "View" Then I should see "Pending Time Off Request Details"

Page 80: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

features/ProcessTimeOffRequest.feature

Scenario: Approve Time Off Request When I go to "/bdd/timeoff/manage" And I press "View" And I press "Approve" Then I should see "Time Off Request Approved" Scenario: Deny Time Off Request When I go to "/bdd/timeoff/manage" And I press "View" And I press "Deny" Then I should see "Time Off Request Denied"

Page 81: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

run behat: bin/behat

Page 82: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat Output--- Failed scenarios:

features/ProcessTimeOffRequest.feature:6

features/ProcessTimeOffRequest.feature:10

features/ProcessTimeOffRequest.feature:15

features/ProcessTimeOffRequest.feature:21

features/SubmitTimeOffRequest.feature:6

features/SubmitTimeOffRequest.feature:10

7 scenarios (1 passed, 6 failed)

22 steps (8 passed, 6 failed, 8 skipped)

0m0.61s (14.81Mb)

Page 83: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat Output

Scenario: Time Off Request Management View Exists

When I go to “/bdd/timeoff/manage"

Then I should see "Manage Time Off Requests"

The text "Manage Time Off Requests" was not found anywhere in the text of the current page.

Page 84: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You
Page 85: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

These failures show us that Behat is testing our app properly, and now we just need to

write the application logic.

Page 86: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Specifications

Page 87: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Now we write specifications for how our application should work.

Page 88: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

These specifications should provide the logic to deliver the results that Behat is testing for.

Page 89: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

bin/phpspec describe App\\Timeoff

Page 90: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec generates a basic spec file for us

Page 91: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.phpnamespace spec\App;use PhpSpec\ObjectBehavior;use Prophecy\Argument;class TimeoffSpec extends ObjectBehavior{ function it_is_initializable() { $this->shouldHaveType('App\Timeoff'); }}

Page 92: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

This default spec tells PHPSpec to expect a class named Timeoff.

Page 93: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Now we add a bit more to the file so PHPSpec will understand what this class should do.

Page 94: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.phpfunction it_creates_timeoff_requests() { $this->create("Name", "reason")->shouldBeString();}function it_loads_all_timeoff_requests() { $this->loadAll()->shouldBeArray();}function it_loads_a_timeoff_request() { $this->load("uuid")->shouldBeArray();}function it_loads_pending_timeoff_requests() { $this->loadPending()->shouldBeArray();}function it_approves_timeoff_requests() { $this->approve("id")->shouldReturn(true);}function it_denies_timeoff_requests() { $this->deny("id")->shouldReturn(true);}

Page 95: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

function it_creates_timeoff_requests() { $this->create("Name", "reason")->shouldBeString();}function it_loads_all_timeoff_requests() { $this->loadAll()->shouldBeArray();}

Page 96: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

function it_loads_a_timeoff_request() { $this->load("uuid")->shouldBeArray();}function it_loads_pending_timeoff_requests() { $this->loadPending()->shouldBeArray();}

Page 97: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

function it_approves_timeoff_requests() { $this->approve("id")->shouldReturn(true);}function it_denies_timeoff_requests() { $this->deny("id")->shouldReturn(true);}

Page 98: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Now we run PHPSpec once more…

Page 99: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Phpspec output10 ✔ is initializable

15 ! creates timeoff requests

method App\Timeoff::create not found.

19 ! loads all timeoff requests

method App\Timeoff::loadAll not found.

23 ! loads pending timeoff requests

method App\Timeoff::loadPending not found.

27 ! approves timeoff requests

method App\Timeoff::approve not found.

31 ! denies timeoff requests

method App\Timeoff::deny not found.

Page 100: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Lots of failures…

Page 101: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

But wait a second - PHPSpec prompts us!

Page 102: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec output

Do you want me to create `App\Timeoff::create()` for you?

[Y/n]

Page 103: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec will create the class and the methods for us!

Page 104: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

This is very powerful with frameworks like Laravel and Magento, which have PHPSpec plugins that help

PHPSpec know where class files should be located.

Page 105: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

And now, the easy part…

Page 106: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Implementation

Page 107: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Implement logic in the new Timeoff class in the locations directed by PHPSpec

Page 108: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Implement each function one at a time, running phpspec after each one.

Page 109: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.phppublic function create($name, $reason){ $uuid1 = Uuid::uuid1(); $uuid = $uuid1->toString(); DB::table('requests')->insert([ 'name' => $name, 'reason' => $reason, 'uuid' => $uuid, ]); return $uuid;}

Page 110: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

public function load($uuid) { $results = DB::select('select * from requests WHERE uuid = ?', [$uuid]); return $results;}

Page 111: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

public function loadAll(){ $results = DB::select('select * from requests'); return $results;}

Page 112: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

public function loadPending(){ $results = DB::select('select * from requests WHERE reviewed = ?', [0]); return $results;}

Page 113: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

public function approve($uuid){ DB::update('update requests set reviewed = 1, approved = 1 where uuid = ?', [$uuid]); return true;}

Page 114: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

spec\TimeoffSpec.php

public function deny($uuid){ DB::update('update requests set reviewed = 1, approved = 0 where uuid = ?', [$uuid]); return true;}

Page 115: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

phpspec should be returning all green

Page 116: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Move on to implementing the front-end behavior

Page 117: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Using Lumen means our view/display logic is very simple

Page 118: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

app\Http\route.php

$app->get('/bdd/', function() use ($app) { return "Welcome to the world of BDD";});

Page 119: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

app\Http\route.php$app->get('/bdd/timeoff/new/', function() use ($app) { if(Request::has('name')) { $to = new \App\Timeoff(); $name = Request::input('name'); $reason = Request::input('reason'); $to->create($name, $reason); return "Time off request submitted"; } else { return view('request.new'); }});

Page 120: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

app\Http\route.php$app->get('/bdd/timeoff/manage/', function() use ($app) { $to = new \App\Timeoff(); if(Request::has('uuid')) { $uuid = Request::input('uuid'); if(Request::has('process')) { $process = Request::input('process'); if($process == 'approve') { $to->approve($uuid); return "Time Off Request Approved"; } else { if($process == 'deny') { $to->deny($uuid); return "Time Off Request Denied"; } } } else { $request = $to->load($uuid); return view('request.manageSpecific', ['request' => $request]); } } else { $requests = $to->loadAll(); return view('request.manage', ['requests' => $requests]); }

Page 121: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

app\Http\route.php$app->get('/bdd/timeoff/manage/', function() use ($app) { $to = new \App\Timeoff(); if(Request::has('uuid')) { $uuid = Request::input('uuid'); if(Request::has('process')) { $process = Request::input('process'); if($process == 'approve') { $to->approve($uuid); return "Time Off Request Approved"; } else { if($process == 'deny') { $to->deny($uuid); return "Time Off Request Denied"; } }

Page 122: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

app\Http\route.php

… } else { $request = $to->load($uuid); return view('request.manageSpecific', ['request' => $request]); }

Page 123: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

app\Http\route.php

… } else { $requests = $to->loadAll(); return view('request.manage', ['requests' => $requests]); }

Page 124: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Our views are located in resources\views\request\ and are simple HTML forms

Page 125: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Once we’re done with the implementation, we move on to…

Page 126: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Testing

Page 127: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Once we’re done, running phpspec run should return green

Page 128: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Once phpspec returns green, run behat, which should return green as well

Page 129: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

We now know that our new feature is working correctly without needing to open a web

browser

Page 130: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

This allows us to flow from function to function as we implement our app, without

breaking our train of thought.

Page 131: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec gives us confidence that the application logic was implemented correctly.

Page 132: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat gives us confidence that the feature is being displayed properly to users.

Page 133: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Running both as we refactor and add new features will give us confidence we haven’t

broken an existing feature

Page 134: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Success!

Page 135: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Our purpose today was to get you hooked on Behat & PHPSpec and show you how easy it is

to get started.

Page 136: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat and PHPSpec are both powerful tools

Page 137: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PHPSpec can be used at a very granular level to ensure your application logic works

correctly

Page 138: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Advanced Behat & PHPSpec

Page 139: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

I encourage you to learn more about Behat & phpspec. Here’s a few areas to consider…

Page 140: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Parallel Execution

Page 141: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

A few approaches to running Behat in parallel to improve it’s performance. Start with:

shvetsgroup/ParallelRunner

Page 142: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Behat - Reusable Actions

Page 143: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

“I should see”, “I go to” are just steps - you can write your own steps.

Page 144: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Mocking & Prophesying

Page 145: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Mock objects are simulated objects that mimic the behavior of real objects

Page 146: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Helpful to mock very complex objects, or objects that you don’t want to call while

testing - i.e., APIs

Page 147: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Prophecy is a highly opinionated PHP mocking framework by the Phpspec team

Page 148: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Take a look at the sample code on Github - I mocked a Human Resource Management

System API

Page 149: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Mocking with Prophecy$this->prophet = new \Prophecy\Prophet;

$prophecy = $this->prophet->prophesize('App\HrmsApi');

$prophecy->getUser(Argument::type('string'))->willReturn('name');

$prophecy->decrement('name', Argument::type('integer'))->willReturn(true);

$dummyApi = $prophecy->reveal();

Page 150: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

PhantomJS

Page 151: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Can use PhantomJS with Behat to render Javascript, including automated screenshots

and screenshot comparison

Page 152: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Two Tasks For You

Page 153: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Next week, setup Behat and PHPSpec on one of your projects and take it for a quick test by

implementing one short feature.

Page 154: pnwphp - PHPSpec & Behat: Two Testing Tools That Write Code For You

Keep In Touch!

• joind.in/14919

• @JoshuaSWarren

• JoshuaWarren.com