Search code examples
phpunit-testingsocketstddadapter

How to (really) unit test an Adapter


My question is really simple, shall an adapter (design pattern) class be unit tested, and how?

Example:

I want to create a class ClientSocket with PHP which is an adapter of the fsockopen, fread, fwrite.

Class ClientSocket extends SenderAdapterAbstract
{
    private $s = null;

    public function __construct($host, $port)
    {
        $this->s = fsockopen($host, $port);
    }

    // Abstract method concrete implementation
    public function send($message)
    {
        fwrite($this->s, $message);

        $result = fgets($this->s);
        fclose($this->s);
    }
}

Should I test this class? A unit test is not really one if it talks to the network.


Solution

  • Should I test this class? a unit test is not really one if it talks to the network...

    Exactly. This is why you can't unit test an Adapter, but you can create integration tests for it if you want. These integration tests should remain basic checks that the 3d party library behaves correctly. If they are too slow, you might need to move them from your main test suite to a separate one that runs less frequently.

    See http://blog.8thlight.com/eric-smith/2011/10/27/thats-not-yours.html http://davesquared.net/2011/04/dont-mock-types-you-dont-own.html