XML Configuration File

  • phpunit.xml -> nicht in git; persönliche Variablen speichern

  • phpunit.xml.dist -> in git

    <env name="foo" value="bar" force="true"/>


Skipping, Incomplete




The practice of replacing an object with a test double that (optionally) returns configured return values is referred to as stubbing. - PHPUnit

class SomeClass
    public function doSomething()
        // Do something.
use PHPUnit\Framework\TestCase;

class StubTest extends TestCase
    public function testStub()
        // Create a stub for the SomeClass class.
        $stub = $this->createStub(SomeClass::class);

        // Configure the stub.

        // Calling $stub->doSomething() will now return
        // 'foo'.
        $this->assertSame('foo', $stub->doSomething());


The practice of replacing an object with a test double that verifies expectations, for instance asserting that a method has been called, is referred to as mocking. - Mock Pbjects

use PHPUnit\Framework\TestCase;

class SubjectTest extends TestCase
    public function testObserversAreUpdated()
        // Create a mock for the Observer class,
        // only mock the update() method.
        $observer = $this->createMock(Observer::class);

        // Set up the expectation for the update() method
        // to be called only once and with the string 'something'
        // as its parameter.

        // Create a Subject object and attach the mocked
        // Observer object to it.
        $subject = new Subject('My subject');

        // Call the doSomething() method on the $subject object
        // which we expect to call the mocked Observer object's
        // update() method with the string 'something'.

Hard Dependencys

Wenn Klasse in einer anderen Funktion erstellt wird (Mockery erforderlich)

namespace AppTest;
use Mockery as m;
class ServiceTest extends \PHPUnit_Framework_TestCase
    public function testCallingExternalService()
        $param = 'Testing';

        $externalMock = m::mock('overload:App\Service\External');

        $service = new \App\Service();

        $result = $service->callExternalService($param);

        $this->assertSame('Tested!', $result);

Wie mache ich das mit PHPUnit?

Last updated