Skip to content

Commit dbd6e8b

Browse files
committed
PHPUnit integration
0 parents  commit dbd6e8b

12 files changed

+606
-0
lines changed

.travis.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
language: php
2+
3+
sudo: false
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache
8+
9+
env:
10+
- PHPUNIT_VERSION=dev-master
11+
- PHPUNIT_VERSION=~4.5.0
12+
- PHPUNIT_VERSION=~4.4.0
13+
- PHPUNIT_VERSION=~4.3.0
14+
- PHPUNIT_VERSION=~4.2.0
15+
- PHPUNIT_VERSION=~4.1.0
16+
- PHPUNIT_VERSION=~4.0.0
17+
18+
php:
19+
- 7.0
20+
- 5.6
21+
- 5.5
22+
- 5.4
23+
- hhvm
24+
25+
matrix:
26+
fast_finish: true
27+
allow_failures:
28+
- php: hhvm
29+
30+
install:
31+
- composer require phpunit/phpunit:${PHPUNIT_VERSION}
32+
- composer require squizlabs/php_codesniffer
33+
- composer require phpmd/phpmd
34+
35+
script:
36+
- vendor/bin/phpunit
37+
- vendor/bin/phpcs --standard=PSR2 classes/ tests/
38+
- vendor/bin/phpmd classes/ text cleancode,codesize,controversial,design,naming,unusedcode
39+

LICENSE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
Version 2, December 2004
3+
4+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5+
6+
Everyone is permitted to copy and distribute verbatim or modified
7+
copies of this license document, and changing it is allowed as long
8+
as the name is changed.
9+
10+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
13+
0. You just DO WHAT THE FUCK YOU WANT TO.
14+

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Mock PHP built-in functions with PHPUnit
2+
3+
This package integrates the function mock library
4+
[PHP-Mock](https://github.com/php-mock/php-mock) with PHPUnit.
5+
6+
# Requirements
7+
8+
# Installation
9+
10+
# Usage
11+
12+
PHP-Mock integrates with the trait
13+
[`PHPMock`](http://php-mock.github.io/phpunit/api/class-phpmock.phpunit.PHPMock.html)
14+
to integrate into your PHPUnit-4 test case. This trait extends the framework
15+
by the method
16+
[`getFunctionMock()`](http://php-mock.github.io/phpunit/api/class-phpmock.phpunit.PHPMock.html#_getFunctionMock).
17+
With this method you can build a mock in the way you are used to build a
18+
PHPUnit mock:
19+
20+
```php
21+
<?php
22+
23+
namespace foo;
24+
25+
use phpmock\phpunit\PHPMock;
26+
27+
class FooTest extends \PHPUnit_Framework_TestCase
28+
{
29+
30+
use PHPMock;
31+
32+
public function testBar()
33+
{
34+
$time = $this->getFunctionMock(__NAMESPACE__, "time");
35+
$time->expects($this->once())->willReturn(3);
36+
$this->assertEquals(3, time());
37+
}
38+
}
39+
```
40+
41+
# License and authors
42+
43+
This project is free and under the WTFPL.
44+
Responsable for this project is Markus Malkusch markus@malkusch.de.
45+
46+
## Donations
47+
48+
If you like this project and feel generous donate a few Bitcoins here:
49+
[1335STSwu9hST4vcMRppEPgENMHD2r1REK](bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK)
50+
51+
[![Build Status](https://travis-ci.org/php-mock/phpunit.svg?branch=master)](https://travis-ci.org/php-mock/phpunit)

classes/DefaultArgumentRemover.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace phpmock\phpunit;
4+
5+
use malkusch\phpmock\MockFunctionHelper;
6+
7+
/**
8+
* Removes default arguments from the invocation.
9+
*
10+
* @author Markus Malkusch <markus@malkusch.de>
11+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
12+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
13+
* @internal
14+
*/
15+
class DefaultArgumentRemover implements \PHPUnit_Framework_MockObject_Matcher_Invocation
16+
{
17+
18+
/**
19+
* @SuppressWarnings(PHPMD)
20+
*/
21+
public function invoked(\PHPUnit_Framework_MockObject_Invocation $invocation)
22+
{
23+
}
24+
25+
/**
26+
* @SuppressWarnings(PHPMD)
27+
*/
28+
public function matches(\PHPUnit_Framework_MockObject_Invocation $invocation)
29+
{
30+
MockFunctionHelper::removeDefaultArguments($invocation->parameters);
31+
return false;
32+
}
33+
34+
public function verify()
35+
{
36+
}
37+
38+
/**
39+
* This method is not defined in the interface, but used in
40+
* PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers().
41+
*
42+
* @return boolean
43+
* @see \PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers()
44+
*/
45+
public function hasMatchers()
46+
{
47+
return false;
48+
}
49+
50+
public function toString()
51+
{
52+
return __CLASS__;
53+
}
54+
}

classes/MockDisabler.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace phpmock\phpunit;
4+
5+
use malkusch\phpmock\Deactivatable;
6+
7+
/**
8+
* Test listener for PHPUnit integration.
9+
*
10+
* This class disables mock functions after a test was run.
11+
*
12+
* @author Markus Malkusch <markus@malkusch.de>
13+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
14+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
15+
* @internal
16+
*/
17+
class MockDisabler extends \PHPUnit_Framework_BaseTestListener
18+
{
19+
20+
/**
21+
* @var Deactivatable The function mocks.
22+
*/
23+
private $deactivatable;
24+
25+
/**
26+
* Sets the function mocks.
27+
*
28+
* @param Deactivatable $deactivatable The function mocks.
29+
*/
30+
public function __construct(Deactivatable $deactivatable)
31+
{
32+
$this->deactivatable = $deactivatable;
33+
}
34+
35+
/**
36+
* Disables the function mocks.
37+
*
38+
* @param \PHPUnit_Framework_Test $test The test.
39+
* @param int $time The test duration.
40+
*
41+
* @see Mock::disable()
42+
*/
43+
public function endTest(\PHPUnit_Framework_Test $test, $time)
44+
{
45+
parent::endTest($test, $time);
46+
47+
$this->deactivatable->disable();
48+
}
49+
}

classes/MockObjectProxy.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace phpmock\phpunit;
4+
5+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
6+
use phpmock\integration\MockDelegateFunctionBuilder;
7+
8+
/**
9+
* Proxy for PHPUnit's PHPUnit_Framework_MockObject_MockObject.
10+
*
11+
* @author Markus Malkusch <markus@malkusch.de>
12+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
13+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
14+
* @internal
15+
*/
16+
class MockObjectProxy implements MockObject
17+
{
18+
19+
/**
20+
* @var MockObject $mockObject The mock object.
21+
*/
22+
private $mockObject;
23+
24+
/**
25+
* Inject the subject.
26+
*
27+
* @param MockObject $mockObject The subject.
28+
*/
29+
public function __construct(MockObject $mockObject)
30+
{
31+
$this->mockObject = $mockObject;
32+
}
33+
34+
/**
35+
* @SuppressWarnings(PHPMD)
36+
*/
37+
// @codingStandardsIgnoreStart
38+
public function __phpunit_getInvocationMocker()
39+
{
40+
// @codingStandardsIgnoreEnd
41+
return $this->mockObject->__phpunit_getInvocationMocker();
42+
}
43+
44+
/**
45+
* @SuppressWarnings(PHPMD)
46+
*/
47+
// @codingStandardsIgnoreStart
48+
public function __phpunit_setOriginalObject($originalObject)
49+
{
50+
// @codingStandardsIgnoreEnd
51+
return $this->mockObject->__phpunit_setOriginalObject($originalObject);
52+
}
53+
54+
/**
55+
* @SuppressWarnings(PHPMD)
56+
*/
57+
// @codingStandardsIgnoreStart
58+
public function __phpunit_verify()
59+
{
60+
// @codingStandardsIgnoreEnd
61+
return $this->mockObject->__phpunit_verify();
62+
}
63+
64+
public function expects(\PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
65+
{
66+
return $this->mockObject->expects($matcher)->method(MockDelegateFunctionBuilder::METHOD);
67+
}
68+
69+
/**
70+
* This method is not part of the contract but was found in
71+
* PHPUnit's mocked_class.tpl.dist.
72+
*
73+
* @SuppressWarnings(PHPMD)
74+
*/
75+
// @codingStandardsIgnoreStart
76+
public function __phpunit_hasMatchers()
77+
{
78+
// @codingStandardsIgnoreEnd
79+
return $this->mockObject->__phpunit_hasMatchers();
80+
}
81+
}

0 commit comments

Comments
 (0)