Skip to content

Commit 33d7d70

Browse files
committed
First commit
0 parents commit 33d7d70

File tree

11 files changed

+319
-0
lines changed

11 files changed

+319
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# .gitattributes
2+
tests/ export-ignore
3+
4+
# Auto detect text files and perform LF normalization
5+
* text=auto

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/composer.phar
3+
/composer.lock

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
- 5.6
8+
- hhvm
9+
10+
before_script:
11+
- composer require satooshi/php-coveralls:dev-master --dev --no-progress
12+
13+
script:
14+
- mkdir -p build/logs
15+
- phpunit --coverage-clover build/logs/clover.xml
16+
17+
after_script:
18+
- php vendor/bin/coveralls -v

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributing
2+
3+
First of all, **thank you** for contributing!
4+
5+
Here are a few rules to follow in order to ease code reviews and merging:
6+
7+
- follow [PSR-1](http://www.php-fig.org/psr/1/) and [PSR-2](http://www.php-fig.org/psr/2/)
8+
- run the test suite
9+
- write (or update) unit tests when applicable
10+
- write documentation for new features
11+
- use [commit messages that make sense](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
12+
13+
One may ask you to [squash your commits](http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html) too. This is used to "clean" your pull request before merging it (we don't want commits such as `fix tests`, `fix 2`, `fix 3`, etc.).
14+
15+
When creating your pull request on GitHub, please write a description which gives the context and/or explains why you are creating it.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Matthieu Napoli
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# PHPUnit EasyMock
2+
3+
Helpers to build PHPUnit mock objects easily.
4+
5+
[![Build Status](https://travis-ci.org/mnapoli/PHP-DI.png?branch=master)](https://travis-ci.org/mnapoli/PHP-DI)
6+
[![Coverage Status](https://coveralls.io/repos/mnapoli/PHP-DI/badge.png?branch=master)](https://coveralls.io/r/mnapoli/PHP-DI?branch=master)
7+
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/mnapoli/PHP-DI/badges/quality-score.png)](https://scrutinizer-ci.com/g/mnapoli/PHP-DI/)
8+
[![Latest Stable Version](https://poser.pugx.org/mnapoli/php-di/v/stable.png)](https://packagist.org/packages/mnapoli/php-di)
9+
[![Total Downloads](https://poser.pugx.org/mnapoli/php-di/downloads.png)](https://packagist.org/packages/mnapoli/php-di)
10+
11+
## Why?
12+
13+
This library is **not** a mocking library. It's just a few helpers to write the most common mocks more easily.
14+
15+
It doesn't reinvent anything and is not intended to cover every use case: only the most common ones.
16+
17+
## Installation
18+
19+
```bash
20+
$ composer require --dev mnapoli/phpunit-easymock
21+
```
22+
23+
## Usage
24+
25+
Here is what a very common PHPUnit mock looks like:
26+
27+
```php
28+
// All these parameters to avoid calling the constructor
29+
$mock = $this->getMock('My\Class', array(), array(), '', false);
30+
31+
$mock->expect($this->any())
32+
->method('sayHello')
33+
->willReturn('Hello');
34+
```
35+
36+
Yuck!
37+
38+
Here is how to write it with EasyMock:
39+
40+
```php
41+
$mock = EasyMock::mock('My\Class', array(
42+
'sayHello' => 'Hello',
43+
));
44+
```
45+
46+
### Features
47+
48+
You can mock methods so that they return values:
49+
50+
```php
51+
$mock = EasyMock::mock('My\Class', array(
52+
'sayHello' => 'Hello',
53+
));
54+
```
55+
56+
Or so that they use a callback:
57+
58+
```php
59+
$mock = EasyMock::mock('My\Class', array(
60+
'sayHello' => function ($name) {
61+
return 'Hello ' . $name;
62+
},
63+
));
64+
```
65+
66+
### What if?
67+
68+
If you want to use assertions or other PHPUnit features, just do it:
69+
70+
```php
71+
$mock = EasyMock::mock('My\Class', array(
72+
'sayHello' => 'hello',
73+
));
74+
75+
$mock->expects($this->once())
76+
->method('sayGoodbye')
77+
->willReturn('Goodbye');
78+
```
79+
80+
Mocks are plain PHPUnit mocks, nothing special here.
81+
82+
## Contributing
83+
84+
See the [CONTRIBUTING](CONTRIBUTING.md) file.
85+
86+
## License
87+
88+
Released under the [MIT license](LICENSE).

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "mnapoli/phpunit-easymock",
3+
"description": "Helpers to build PHPUnit mocks",
4+
"keywords": ["phpunit", "mock"],
5+
"homepage": "https://github.com/mnapoli/phpunit-easymock",
6+
"license": "MIT",
7+
"type": "library",
8+
"autoload": {
9+
"psr-4": {
10+
"EasyMock\\": "src/"
11+
}
12+
},
13+
"autoload-dev": {
14+
"psr-4": {
15+
"EasyMock\\Test\\": "tests/"
16+
}
17+
},
18+
"require": {
19+
"php": ">=5.3.0",
20+
"phpunit/phpunit-mock-objects": "~2.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "~4.0"
24+
}
25+
}

phpunit.xml.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit colors="true"
3+
convertErrorsToExceptions="true"
4+
convertNoticesToExceptions="true"
5+
convertWarningsToExceptions="true"
6+
bootstrap="./vendor/autoload.php">
7+
8+
<testsuites>
9+
<testsuite name="Test suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
14+
</phpunit>

src/EasyMock.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace EasyMock;
4+
5+
use PHPUnit_Framework_MockObject_Generator;
6+
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
7+
use PHPUnit_Framework_MockObject_MockObject;
8+
9+
/**
10+
* Generates mock objects.
11+
*
12+
* @author Matthieu Napoli <matthieu@mnapoli.fr>
13+
*/
14+
class EasyMock
15+
{
16+
/**
17+
* Mock the given class.
18+
*
19+
* Methods not specified in $methods will be mocked to return null (default PHPUnit behavior).
20+
* The class constructor will *not* be called.
21+
*
22+
* @param string $classname The class to mock.
23+
* @param array $methods Array of values to return, indexed by the method name.
24+
*
25+
* @return PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
public static function mock($classname, array $methods = array())
28+
{
29+
$mockGenerator = new PHPUnit_Framework_MockObject_Generator();
30+
31+
/** @var PHPUnit_Framework_MockObject_MockObject $mock */
32+
$mock = $mockGenerator->getMock(
33+
$classname,
34+
array(),
35+
array(),
36+
'',
37+
false
38+
);
39+
40+
foreach ($methods as $method => $return) {
41+
self::mockMethod($mock, $method, $return);
42+
}
43+
44+
return $mock;
45+
}
46+
47+
private static function mockMethod(PHPUnit_Framework_MockObject_MockObject $mock, $method, $return)
48+
{
49+
$methodAssertion = $mock->expects(new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount)
50+
->method($method);
51+
52+
if (is_callable($return)) {
53+
$methodAssertion->willReturnCallback($return);
54+
} else {
55+
$methodAssertion->willReturn($return);
56+
}
57+
}
58+
}

tests/Fixture/ClassFixture.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace EasyMock\Test\Fixture;
4+
5+
class ClassFixture
6+
{
7+
public function foo()
8+
{
9+
return true;
10+
}
11+
}

0 commit comments

Comments
 (0)