2. Testing untestable code
About me
Stephan Hochdörfer, bitExpert AG
Department Manager Research Labs
enjoying PHP since 1999
S.Hochdoerfer@bitExpert.de
@shochdoerfer
11. Testing untestable code
"...our test strategy requires us to
have more control [...] of the sut."
Gerard Meszaros, xUnit Test Patterns: Refactoring Test
Code
23. Testing untestable code
Object construction
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this->Engine = Engine::getByType($sEngine);
}
}
25. Testing untestable code
Object construction
<?php
include('Engine.php');
class Car {
private $Engine;
public function __construct($sEngine) {
$this->Engine = Engine::getByType($sEngine);
}
}
30. Testing untestable code
Object construction – Namespaces
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this->Engine = CarEngine::
getByType($sEngine);
}
}
42. Testing untestable code
External resources – Mock filesystem
<?php
// set up test environmemt
vfsStream::setup('exampleDir');
// create directory in test enviroment
mkdir(vfsStream::url('exampleDir').'/sample/');
// check if directory was created
echo vfsStreamWrapper::getRoot()->hasChild('sample');
57. Testing untestable code
Generative Programming
A frame is a data structure
for representing knowledge.
58. Testing untestable code
A Frame instance
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this->Engine = <!{Factory}!>::
getByType($sEngine);
}
}
59. Testing untestable code
The Frame controller
public class MyFrameController extends
SimpleFrameController {
public void execute(Frame frame, FeatureConfig
config) {
if(config.hasFeature("unittest")) {
frame.setSlot("Factory", "FactoryMock");
}
else {
frame.setSlot("Factory", "EngineFactory");
}
}
}
60. Testing untestable code
Generated result - Testcase
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this->Engine = FactoryMock::
getByType($sEngine);
}
}
61. Testing untestable code
Generated result - Application
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this->Engine = EngineFactory::
getByType($sEngine);
}
}