Skip to content

Commit c254ed1

Browse files
committed
Integrate PHP-Mock into Prophecy
0 parents  commit c254ed1

File tree

9 files changed

+363
-0
lines changed

9 files changed

+363
-0
lines changed

.travis.yml

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

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Mock PHP built-in functions with Prophecy
2+
3+
This package integrates the function mock library
4+
[PHP-Mock](https://github.com/php-mock/php-mock) with Prophecy.
5+
6+
# Installation
7+
8+
Use [Composer](https://getcomposer.org/):
9+
10+
```sh
11+
composer require php-mock/php-mock-prophecy
12+
```
13+
14+
# Usage
15+
16+
TODO
17+
18+
# License and authors
19+
20+
This project is free and under the WTFPL.
21+
Responsable for this project is Markus Malkusch [email protected].
22+
23+
## Donations
24+
25+
If you like this project and feel generous donate a few Bitcoins here:
26+
[1335STSwu9hST4vcMRppEPgENMHD2r1REK](bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK)
27+
28+
[![Build Status](https://travis-ci.org/php-mock/php-mock-prophecy.svg?branch=master)](https://travis-ci.org/php-mock/php-mock-prophecy)
29+

classes/FunctionProphecy.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace phpmock\prophecy;
4+
5+
use phpmock\integration\MockDelegateFunctionBuilder;
6+
use Prophecy\Prophet;
7+
use Prophecy\Prophecy\ProphecyInterface;
8+
use Prophecy\Prophecy\MethodProphecy;
9+
10+
/**
11+
* Function prophecy.
12+
*
13+
* @author Markus Malkusch <[email protected]>
14+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
15+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
16+
*/
17+
class FunctionProphecy implements ProphecyInterface
18+
{
19+
20+
/**
21+
* @var Prophet The prophet
22+
*/
23+
private $prophet;
24+
25+
/**
26+
* @var string The namespace.
27+
*/
28+
private $namespace;
29+
30+
/**
31+
* @var Revelation[] The delegated prophecies.
32+
*/
33+
private $revelations = [];
34+
35+
/**
36+
* Sets the prophet.
37+
*
38+
* @param string $namespace The namespace.
39+
* @param Prophet $prophet The prophet.
40+
* @internal
41+
*/
42+
public function __construct($namespace, Prophet $prophet)
43+
{
44+
$this->prophet = $prophet;
45+
$this->namespace = $namespace;
46+
}
47+
48+
/**
49+
* Creates a new function prophecy using specified function name and arguments.
50+
*
51+
* @param string $functionName The function name.
52+
* @param array $arguments The arguments.
53+
*
54+
* @return MethodProphecy The function prophecy.
55+
*/
56+
public function __call($functionName, array $arguments)
57+
{
58+
$delegateBuilder = new MockDelegateFunctionBuilder();
59+
$delegateBuilder->build($functionName);
60+
$prophecy = $this->prophet->prophesize($delegateBuilder->getFullyQualifiedClassName());
61+
$this->revelations[] = new Revelation($this->namespace, $functionName, $prophecy);
62+
return $prophecy->__call(MockDelegateFunctionBuilder::METHOD, $arguments);
63+
}
64+
65+
/**
66+
* Reveals the function prophecies.
67+
*
68+
* I.e. the prophesized functions will become effective.
69+
*/
70+
public function reveal()
71+
{
72+
foreach ($this->revelations as $revelation) {
73+
$revelation->reveal();
74+
}
75+
}
76+
}

classes/PHPProphet.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace phpmock\prophecy;
4+
5+
use phpmock\MockRegistry;
6+
use Prophecy\Prophet;
7+
use Prophecy\Exception\Prediction\AggregateException;
8+
9+
/**
10+
* PHPProphet creates prophecies for functions.
11+
*
12+
* @author Markus Malkusch <[email protected]>
13+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
14+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
15+
*/
16+
class PHPProphet
17+
{
18+
19+
/**
20+
* @var Prophet The prophet.
21+
*/
22+
private $prophet;
23+
24+
/**
25+
* Sets an optional prophet.
26+
*
27+
* @param Prophet $prophet The prophet.
28+
*/
29+
public function __construct(Prophet $prophet = null)
30+
{
31+
$this->prophet = is_null($prophet) ? new Prophet() : $prophet;
32+
}
33+
34+
/**
35+
* Creates a new function prophecy.
36+
*
37+
* @param string $namespace The function namespace.
38+
*
39+
* @return FunctionProphecy The function prophecy.
40+
*/
41+
public function prophesize($namespace)
42+
{
43+
return new FunctionProphecy($namespace, $this->prophet);
44+
}
45+
46+
/**
47+
* Checks all predictions defined by prophecies of this Prophet.
48+
*
49+
* It will also disable all previously revealed function prophecies.
50+
*
51+
* @throws AggregateException If any prediction fails
52+
*/
53+
public function checkPredictions()
54+
{
55+
MockRegistry::getInstance()->unregisterAll();
56+
$this->prophet->checkPredictions();
57+
}
58+
}

classes/Revelation.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace phpmock\prophecy;
4+
5+
use phpmock\MockBuilder;
6+
use phpmock\integration\MockDelegateFunctionBuilder;
7+
use Prophecy\Prophecy\ProphecyInterface;
8+
9+
/**
10+
* The single function revelation.
11+
*
12+
* @author Markus Malkusch <[email protected]>
13+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
14+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
15+
* @internal
16+
*/
17+
class Revelation implements ProphecyInterface
18+
{
19+
20+
/**
21+
* @var string The function namespace.
22+
*/
23+
private $namespace;
24+
25+
/**
26+
* @var string The function name.
27+
*/
28+
private $functionName;
29+
30+
/**
31+
* @var ProphecyInterface The prophecy
32+
*/
33+
private $prophecy;
34+
35+
/**
36+
* Setup the revelation.
37+
*
38+
* @param String $namespace The namespace.
39+
* @param String $functionName The function name.
40+
* @param ProphecyInterface $prophecy The prophecy.
41+
*/
42+
public function __construct($namespace, $functionName, ProphecyInterface $prophecy)
43+
{
44+
$this->namespace = $namespace;
45+
$this->functionName = $functionName;
46+
$this->prophecy = $prophecy;
47+
}
48+
49+
/**
50+
* Reveals the function prophecy.
51+
*
52+
* I.e. the prophesized function will become effective.
53+
*
54+
* @return Mock The enabled function mock.
55+
*/
56+
public function reveal()
57+
{
58+
$delegate = $this->prophecy->reveal();
59+
$builder = new MockBuilder();
60+
$builder->setNamespace($this->namespace)
61+
->setName($this->functionName)
62+
->setFunction([$delegate, MockDelegateFunctionBuilder::METHOD]);
63+
$mock = $builder->build();
64+
$mock->enable();
65+
return $mock;
66+
}
67+
}

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "php-mock/php-mock-prophecy",
3+
"type": "library",
4+
"description": "Mock built-in PHP functions (e.g. time()) with Prophecy. This package relies on PHP's namespace fallback policy. No further extension is needed.",
5+
"keywords": ["prophecy", "mock", "stub", "test double", "function", "test", "TDD", "BDD"],
6+
"homepage": "https://github.com/php-mock/php-mock-prophecy",
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\\prophecy\\": "classes/"}
18+
},
19+
"require": {
20+
"php": ">=5.5",
21+
"php-mock/php-mock-integration": "^1",
22+
"phpspec/prophecy": "^1"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^5"
26+
},
27+
"archive": {
28+
"exclude": ["/tests"]
29+
}
30+
}

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/PHPProphetTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace phpmock\prophecy;
4+
5+
use phpmock\AbstractMockTest;
6+
use Prophecy\Argument;
7+
8+
/**
9+
* Tests PHPProphet.
10+
*
11+
* @author Markus Malkusch <[email protected]>
12+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
13+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
14+
* @see PHPProphet
15+
*/
16+
class PHPProphetTest extends AbstractMockTest
17+
{
18+
19+
/**
20+
* @var PHPProphet The SUT.
21+
*/
22+
private $prophet;
23+
24+
protected function setup()
25+
{
26+
parent::setUp();
27+
28+
$this->prophet = new PHPProphet();
29+
}
30+
31+
protected function disableMocks()
32+
{
33+
$this->prophet->checkPredictions();
34+
}
35+
36+
protected function mockFunction($namespace, $functionName, callable $function)
37+
{
38+
$prophecy = $this->prophet->prophesize($namespace);
39+
$prophecy->$functionName(Argument::cetera())->will(function(array $parameters) use ($function) {
40+
return call_user_func_array($function, $parameters);
41+
});
42+
$prophecy->reveal();
43+
}
44+
}

0 commit comments

Comments
 (0)