Marty Wallace
Marty Wallace

Reputation: 35776

Test driven development and interfaces

How would i unit test an interface such as this following simple example:

interface My_App_My_Interface
{
    /**
     * @return int
     */
    public function getInteger();

    /**
     * @return string
     */
    public function getString();
}

also how would this be organised in my applications test directory:

tests > My > App > My > InterfaceTest ??

Upvotes: 2

Views: 278

Answers (2)

artragis
artragis

Reputation: 3713

As piotrek said, you will never test interfaces as they are just a contract, there is no code in there.

For example, with atoum testing framework you could write for a class that implements your interface.

namespace mageekguy\atoum\tests;

class TestMyInterfaceImplementation extends atoum\test{
    public function test__construct(){
        $object = new MyObject();
        $this->object($object)->instanceof('MyInterface');
    }
    public function test_getInteger(){
       $object = new MyObject();
        $this->integer($object->getInteger);
    }
 }

As an interface just provides abstract methods, they just can't be instanciated so no test can be written.

Upvotes: 0

piotrek
piotrek

Reputation: 14550

you don't test interfaces. you test implementation. interfaces should be checked (by human) if they provide all required functionality

Upvotes: 4

Related Questions