An open API service indexing awesome lists of open source software.

https://github.com/phpspec/phpspec-mocks

PHPSpec Doubles (mocks and stubs) adapters
https://github.com/phpspec/phpspec-mocks

Last synced: about 1 year ago
JSON representation

PHPSpec Doubles (mocks and stubs) adapters

Awesome Lists containing this project

README

          

PHPSpec Mocks
=============

**PHPSpec Mocks** is a lightweight mocking framework designed to be used in the
PHP BDD Framework **PHPSpec**.

Examples
--------

### 1. Stubbing a method

Write your spec, replacing the dependency with a stub. Use the 'stub' method to
add a stubbed method and 'andReturn' to specify the value you want to be
returned:

stub('greet')->andReturn('Hello, World!');

$helloWorld = new HelloWorld($greeter);
$this->spec($helloWorld->hello())->should->equal('Hello, World!');
}
}

You can now write your class:

greeter = $message;
}

public function hello()
{
return $this->greeter->greet();
}
}

### 2. Partial stubbing

You can specify that you want a method to be stubbed only when certain arguments are
passed:

stub('greet')
->shouldReceive('Chuck')
->andReturn('Hello, Chuck!');

$helloWorld = new HelloWorld($greeter);
$this->spec($helloWorld->hello('Chuck'))
->should->equal('Hello, Chuck!');
}

### 3. Stubbing a property

It is possible to stub a property in the same manner:

stub('who')->andReturn('Chuck');

$helloWorld = new HelloWorld($greeter);
$this->spec($helloWorld->greetingWho())->should->equal('Chuck');
}
}

greeter->who;
}
}

### 4. Shortcut for creating a double

It is possible to create a double and stubbing methods/properties all at once.
So the method in the previous example could be replaced by

'Chuck');

$helloWorld = new HelloWorld($greeter);
$this->spec($helloWorld->greetingWho())->should->equal('Chuck');
}

### 5. Empty doubles

If your interface is type agnostic you can even create a mock without
giving it a name

stub('greet')->andReturn('Hello, World!');

$helloWorld = new HelloWorld($greeter);
$this->spec($helloWorld->hello())->should->equal('Hello, World!');
}

...
// HelloWorld constructor:
public function __construct($greeter) // <-- no type hinting

### 6. Counters

It is also possible to specify mocks that expect a method/property to be
accessed a specified number of times:

stub('who')->andReturn('Chuck')->exactly(1);

// property will never be accessed
$greeter->stub('who')->never();

// property will be accessed any number of times
$greeter->stub('who')->andReturn('Chuck');