Skip to content

Commit 8b2f8c4

Browse files
committed
Mockery integration
0 parents  commit 8b2f8c4

File tree

8 files changed

+248
-0
lines changed

8 files changed

+248
-0
lines changed

.travis.yml

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

LICENSE

Lines changed: 14 additions & 0 deletions
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 <[email protected]>
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Mock PHP built-in functions with Mockery
2+
3+
This package integrates the function mock library
4+
[PHP-Mock](https://github.com/php-mock/php-mock) with Mockery.
5+
6+
# Requirements
7+
8+
# Installation
9+
10+
# Usage
11+
12+
# License and authors
13+
14+
This project is free and under the WTFPL.
15+
Responsable for this project is Markus Malkusch [email protected].
16+
17+
## Donations
18+
19+
If you like this project and feel generous donate a few Bitcoins here:
20+
[1335STSwu9hST4vcMRppEPgENMHD2r1REK](bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK)
21+
22+
[![Build Status](https://travis-ci.org/php-mock/mockery.svg?branch=master)](https://travis-ci.org/php-mock/mockery)

classes/MockDisabler.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace phpmock\mockery;
4+
5+
use malkusch\phpmock\Deactivatable;
6+
use Mockery\Mock;
7+
8+
/**
9+
* Deactivatable Mockery integration.
10+
*
11+
* This class disables mock functions with Mockery::close().
12+
*
13+
* @author Markus Malkusch <[email protected]>
14+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
15+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
16+
* @internal
17+
*/
18+
class MockDisabler extends Mock
19+
{
20+
21+
/**
22+
* @var Deactivatable The function mocks.
23+
*/
24+
private $deactivatable;
25+
26+
/**
27+
* Sets the function mocks.
28+
*
29+
* @param Deactivatable $deactivatable The function mocks.
30+
*/
31+
public function __construct(Deactivatable $deactivatable)
32+
{
33+
$this->deactivatable = $deactivatable;
34+
}
35+
36+
/**
37+
* @SuppressWarnings(PHPMD)
38+
*/
39+
// @codingStandardsIgnoreStart
40+
public function mockery_teardown()
41+
{
42+
// @codingStandardsIgnoreEnd
43+
$this->deactivatable->disable();
44+
}
45+
}

classes/PHPMockery.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace phpmock\mockery;
4+
5+
use Mockery;
6+
use Mockery\Expectation;
7+
use malkusch\phpmock\MockBuilder;
8+
use phpmock\integration\MockDelegateFunctionBuilder;
9+
10+
/**
11+
* Mock built-in PHP functions with Mockery.
12+
*
13+
* <code>
14+
* <?php
15+
*
16+
* namespace foo;
17+
*
18+
* use phpmock\mockery\PHPMockery;
19+
*
20+
* $mock = PHPMockery::mock(__NAMESPACE__, "time")->andReturn(3);
21+
* assert (3 == time());
22+
*
23+
* \Mockery::close();
24+
* </code>
25+
*
26+
* @author Markus Malkusch <[email protected]>
27+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
28+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
29+
*/
30+
class PHPMockery
31+
{
32+
33+
/**
34+
* Builds a function mock.
35+
*
36+
* Disable this mock after the test with Mockery::close().
37+
*
38+
* @param string $namespace The function namespace.
39+
* @param string $name The function name.
40+
*
41+
* @return Expectation The mockery expectation.
42+
* @SuppressWarnings(PHPMD)
43+
*/
44+
public static function mock($namespace, $name)
45+
{
46+
$delegateBuilder = new MockDelegateFunctionBuilder();
47+
$delegateBuilder->build($name);
48+
49+
$mockeryMock = Mockery::mock($delegateBuilder->getFullyQualifiedClassName());
50+
$expectation = $mockeryMock->makePartial()->shouldReceive(MockDelegateFunctionBuilder::METHOD);
51+
52+
$builder = new MockBuilder();
53+
$builder->setNamespace($namespace)
54+
->setName($name)
55+
->setFunctionProvider($mockeryMock);
56+
$mock = $builder->build();
57+
$mock->enable();
58+
59+
$disabler = new MockDisabler($mock);
60+
Mockery::getContainer()->rememberMock($disabler);
61+
62+
return $expectation;
63+
}
64+
}

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "php-mock/mockery",
3+
"type": "library",
4+
"description": "Mock built-in PHP functions (e.g. time()) with Mockery. This package relies on PHP's namespace fallback policy. No further extension is needed.",
5+
"keywords": ["mockery", "mock", "stub", "test double", "function", "test", "TDD", "BDD"],
6+
"homepage": "https://github.com/php-mock/mockery",
7+
"license": "WTFPL",
8+
"authors": [
9+
{
10+
"name": "Markus Malkusch",
11+
"email": "[email protected]",
12+
"homepage": "http://markus.malkusch.de",
13+
"role": "Developer"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {"phpmock\\mockery\\": "classes/"}
18+
},
19+
"require": {
20+
"php": ">=5.4",
21+
"mockery/mockery": "^0.8",
22+
"php-mock/integration": "dev-master"
23+
},
24+
"minimum-stability": "dev",
25+
"require-dev": {
26+
"phpunit/phpunit": "^4"
27+
},
28+
"archive": {
29+
"exclude": ["/tests"]
30+
}
31+
}

phpunit.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php">
5+
<testsuite name="Unit tests">
6+
<directory>tests/</directory>
7+
</testsuite>
8+
</phpunit>

tests/PHPMockeryTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace phpmock\mockery;
4+
5+
use malkusch\phpmock\AbstractMockTest;
6+
use Mockery;
7+
8+
/**
9+
* Tests PHPMockery.
10+
*
11+
* @author Markus Malkusch <[email protected]>
12+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
13+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
14+
* @see PHPMockery
15+
*/
16+
class PHPMockeryTest extends AbstractMockTest
17+
{
18+
19+
protected function disableMocks()
20+
{
21+
Mockery::close();
22+
}
23+
24+
protected function mockFunction($namespace, $functionName, callable $function)
25+
{
26+
PHPMockery::mock($namespace, $functionName)->andReturnUsing($function);
27+
}
28+
}

0 commit comments

Comments
 (0)