mercredi 23 septembre 2015

PHPUnit: how to test that a method hasn't been called "yet", but will be called later in test case?

I need to test that a method is not called at some point in the test case, but is expected to be called later. Here's an example test:

<?php

class B {
    public function flush($buffer) {}
}

class A {
    private $b;
    private $buffer = array();

    public function __construct($b) {
        $this->b = $b;
    }

    public function x($val) {
        $this->buffer[] = $val;
        if (count($this->buffer) >= 3) $this->b->flush($this->buffer);
    }
}

class XTest extends PHPUnit_Framework_TestCase {
    public function testB() {
        $b = $this->getMock('B');
        $a = new A($b);

        $a->x(1);
        $a->x(2);

        // flush is not called YET
        $b->expects($this->never())->method('flush');

//      $b->expects($this->at(0))->method('flush'); // ??????

        $a->x(3);
    }
}

Aucun commentaire:

Enregistrer un commentaire