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
- Host: GitHub
- URL: https://github.com/phpspec/phpspec-mocks
- Owner: phpspec
- Created: 2011-05-14T22:43:36.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2011-06-11T20:15:43.000Z (about 15 years ago)
- Last Synced: 2025-03-26T09:51:16.495Z (over 1 year ago)
- Language: PHP
- Homepage: http://www.phpspec.net
- Size: 121 KB
- Stars: 7
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.markdown
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');