Test{able|ing} Zend Framework 2 Applications

fishhookFladgeInternet και Εφαρμογές Web

13 Δεκ 2013 (πριν από 3 χρόνια και 7 μήνες)

57 εμφανίσεις

Test{able|ing} Zend Framework 2 Applications
Sebastian Bergmann | November 20
th
2013
sharing experience
Sebastian Bergmann
»
Has instrumentally contributed
to transforming PHP into a
reliable platform for large-scale,
critical projects.
»
Enterprises and PHP developers
around the world benefit from
the tools that he has developed
and the experience he shares.

sharing experience
sharing experience
What is a Zend Framework 2 application anyway?
What is a Zend Framework 2 application anyway?
web
Presentation
Application Logic
Domain Logic
Persistence
Controller
Routing
HTTP Abstraction
Template
View
Presentation Model
What is a framework anyway?
Presentation
Application Logic
Domain Logic
Persistence
Controller
Routing
HTTP Abstraction
Template
View
Presentation Model
Presentation
Application Logic
Persistence
Controller
Routing
HTTP Abstraction
A framework can only help with
these aspects of a web application.
Template
View
Presentation Model
Presentation
Application Logic
Persistence
Controller
Routing
HTTP Abstraction
Template
View
Presentation Model
Zend Framework
Domain Logic

Responsible for representing concepts of the business,
information about the business situation, and business rules.
State that reflects the business situation is controlled and used
here, even though the technical details of storing it are delegated
to the infrastructure.
This layer is the heart of business software.”

Eric Evans
Unit

sharing experience
<?php
namespace
BankAccount
\
Domain
;
use
SebastianBergmann
\
Money
\
EUR
;
class
BankAccount
{

private
$balance
;

public function
__construct
()

{

$this
->
balance
= new
EUR
(
0
);

}

public function
getBalance
()

{

return
$this
->
balance
;

}

// ...
}

sharing experience
<?php
namespace
BankAccount
\
Domain
;
use
SebastianBergmann
\
Money
\
EUR
;
class
BankAccountTest
extends \
PHPUnit_Framework_TestCase
{

public function
testBalanceIsInitiallyZero
()

{

$ba
= new
BankAccount
;

$this
->
assertEquals
(new
EUR
(
0
),
$ba
->
getBalance
());

}

// ...
}
Unit
Unit
Unit
Stub / Mock
Unit
Value Object
Client
Server

sharing experience
<?php
namespace
BankAccount
\
Controller
;
use
Zend
\
Mvc
\
Controller
\
AbstractActionController
;
use
Zend
\
View
\
Model
\
ViewModel
;
class
BankAccountController
extends
AbstractActionController
{

public function
indexAction
()

{

}
}

sharing experience
<?php
namespace BankAccount\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class BankAccountController extends AbstractActionController
{

public function indexAction()

{

$id
= (
int
)
$this
->
params
()->
fromRoute
(
'id'
,
0
);

}
}

sharing experience
<?php
namespace BankAccount\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class BankAccountController extends AbstractActionController
{

public function indexAction()

{

$id = (int) $this->params()->fromRoute('id', 0);

$mapper
=
$this
->
getServiceLocator
()->
get
(

'BankAccount\Persistence\BankAccountMapper'

);

}
}

sharing experience
<?php
namespace BankAccount\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class BankAccountController extends AbstractActionController
{

public function indexAction()

{

$id = (int) $this->params()->fromRoute('id', 0);

$mapper = $this->getServiceLocator()->get(

'BankAccount\Persistence\BankAccountMapper'

);

return new
ViewModel
(

array(

'id'
=>
$id
,

'balance'
=>
$mapper
->
findById
(
$id
)->
getBalance
()

)

);

}
}

sharing experience
<?php
namespace
BankAccount
\
Controller
;
use
Zend
\
Test
\
PHPUnit
\
Controller
\
AbstractHttpControllerTestCase
;
class
BankAccountControllerTest
extends
AbstractHttpControllerTestCase
{

public function
testBankAccountCanBeDisplayed
()

{

}
}

sharing experience
<?php
namespace BankAccount\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class BankAccountControllerTest
extends AbstractHttpControllerTestCase
{

public function testBankAccountCanBeDisplayed()

{

$this
->
dispatch
(
'/bankaccount/1'
);

}
}

sharing experience
<?php
namespace BankAccount\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class BankAccountControllerTest
extends AbstractHttpControllerTestCase
{

public function testBankAccountCanBeDisplayed()

{

$this->dispatch('/bankaccount/1');

$this
->
assertContains
(

'The balance of bank account #1 is 1,00 €.'
,

$this
->
getResponse
()

);

}
}
Client
Server

sharing experience
<?php
class
ApplicationTest
extends
MinkTestCase
{

public function
testBankAccountCanBeDisplayed
()

{

$page
=
$this
->
visit
(
'http://localhost/bankaccount/1'
);

$this
->
assertContains
(

'The balance of bank account #1 is 1,00 €.'
,

$page
->
getContent
()

);

}
}

sharing experience
<?php
use
Behat
\
Mink
\
Session
;
use
Behat
\
Mink
\
Driver
\
GoutteDriver
;
abstract class
MinkTestCase
extends
PHPUnit_Framework_TestCase
{

private
$session
;

protected function
setUp
()

{

$this
->
session
= new
Session
(new
GoutteDriver
);

}

protected function
visit
(
$url
)

{

$this
->
session
->
visit
(
$url
);

return
$this
->
session
->
getPage
();

}
}

sharing experience
Feature:
BankAccount

Scenario:
Accessing a bank account that exists

Given
I am on "
/bankaccount/1
"

Then
I should see "
The balance of bank account #1 is 1,00 €.
"

The secret in testing
is in writing testable code”
Miško Hevery

When applying a framework, the team needs to focus
on its goal: building an implementation that expresses a
domain model and uses it to solve important problems.
The team must seek ways of employing the framework to
those ends, even if it means not using all of the
framework's features.
Judiciously applying only the most valuable of
framework features reduces the coupling of the
implementation and the framework, allowing more
flexibility in later design decisions.”

Eric Evans

talks.thePHP.cc

sebastian@thePHP.cc

@s_bergmann
Become a
Certified PHP Craftsman
in 2014!
http://thephpcurriculum.com/