by Enrico Zimuel
Principal Software Engineer @ Elastic
PUG Torino, Feb. 19, 2020
|
Refactoring is the process of changing the structure of code without changing its behavior
Legacy code is source code that relates to a no-longer supported or manufactured operating system or other computer technology
Code that developers are afraid to change
Never refactor a production code that does not have unit tests
Tool: PHPUnit
For example, using the following class:
class Foo
{
public function __construct(BarInterface $bar)
{
$this->bar = $bar;
}
public function baz()
{
return $this->bar->doSomething(/* ... */);
}
}
use PHPUnit\Framework\TestCase;
class StubTest extends TestCase
{
public function testStub()
{
$stub = $this->getMockBuilder(BarInterface::class)
->getMock();
$stub->method('doSomething')
->willReturn('foo');
$foo = new Foo($stub);
$this->assertSame('foo', $foo->baz());
}
}
class Book {}
interface BookRepository {
function find($id): Book;
function findAll(): array;
function add(Book $book): void;
}
$double = Mockery::mock(BookRepository::class);
$double->allows()->find(123)->andReturns(new Book());
$book = $double->find(123);
class Temperature
{
private $service;
public function __construct($service)
{
$this->service = $service;
}
public function average()
{
$total = 0;
for ($i=0; $i<3; $i++) {
$total += $this->service->readTemp();
}
return $total/3;
}
}
class TemperatureTest extends \PHPUnit\Framework\TestCase
{
public function tearDown()
{
Mockery::close();
}
public function testGetsAvgTemperatureFrom3ServiceReadings()
{
$service = Mockery::mock('service');
$service->shouldReceive('readTemp')
->times(3)
->andReturn(10, 12, 14);
$temperature = new Temperature($service);
$this->assertEquals(12, $temperature->average());
}
}
$spy = \Mockery::spy('MyDependency');
$sut = new MyClass($spy);
// act
$sut->callFoo();
// assert
$spy->shouldHaveReceived()
->foo()
->with('bar');
Feature of PHPUnit to measure how much code is covered by the unit test
Many options for legacy code (not PSR-4)
* deprecated, but still useful
{
"autoload": {
"classmap": ["src/", "lib/", "Something.php"]
}
}
Follow me: @ezimuel
This work is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
I used reveal.js to make this presentation.