Skip to content

Commit 659ae67

Browse files
committed
integration package
0 parents  commit 659ae67

9 files changed

+323
-0
lines changed

.travis.yml

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

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# PHP-Mock integration package
2+
3+
This is a support package for PHP-Mock integration into other frameworks.
4+
You'll find these integrations:
5+
6+
- [php-mock/phpunit](https://github.com/php-mock/phpunit) - PHPUnit integration
7+
8+
- [php-mock/mockery](https://github.com/php-mock/mockery) - Mockery integration
9+
10+
# License and authors
11+
12+
This project is free and under the WTFPL.
13+
Responsable for this project is Markus Malkusch [email protected].
14+
15+
## Donations
16+
17+
If you like PHP-Mock and feel generous donate a few Bitcoins here:
18+
[1335STSwu9hST4vcMRppEPgENMHD2r1REK](bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK)
19+
20+
[![Build Status](https://travis-ci.org/php-mock/integration.svg?branch=master)](https://travis-ci.org/php-mock/integration)

classes/MockDelegateFunction.tpl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace {namespace};
2+
3+
use malkusch\phpmock\functions\FunctionProvider;
4+
5+
/**
6+
* Function provider which delegates to a mockable MockDelegate.
7+
*
8+
* @author Markus Malkusch <markus@malkusch.de>
9+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
10+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
11+
* @internal
12+
*/
13+
abstract class MockDelegateFunction implements FunctionProvider
14+
{
15+
16+
/**
17+
* A mocked function will redirect its call to this method.
18+
*
19+
* @return mixed Returns the function output.
20+
*/
21+
abstract public function delegate({signatureParameters});
22+
23+
public function getCallable()
24+
{
25+
return [$this, "delegate"];
26+
}
27+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace phpmock\integration;
4+
5+
use malkusch\phpmock\ParameterBuilder;
6+
7+
/**
8+
* Defines a MockDelegateFunction.
9+
*
10+
* @author Markus Malkusch <[email protected]>
11+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
12+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
13+
* @internal
14+
*/
15+
class MockDelegateFunctionBuilder
16+
{
17+
18+
/**
19+
* The delegation method name.
20+
*/
21+
const METHOD = "delegate";
22+
23+
/**
24+
* @var int The instance counter.
25+
*/
26+
private static $counter = 0;
27+
28+
/**
29+
* @var string The namespace of the build class.
30+
*/
31+
private $namespace;
32+
33+
/**
34+
* @var \Text_Template The MockDelegateFunction template.
35+
*/
36+
private $template;
37+
38+
/**
39+
* Instantiation.
40+
*/
41+
public function __construct()
42+
{
43+
$this->template = new \Text_Template(__DIR__ . "/MockDelegateFunction.tpl");
44+
}
45+
46+
/**
47+
* Builds a MockDelegateFunction for a function.
48+
*
49+
* @param string|null $functionName The mocked function.
50+
*
51+
* @SuppressWarnings(PHPMD)
52+
*/
53+
public function build($functionName = null)
54+
{
55+
self::$counter++;
56+
57+
$this->namespace = __NAMESPACE__ . self::$counter;
58+
59+
$parameterBuilder = new ParameterBuilder();
60+
$parameterBuilder->build($functionName);
61+
62+
$data = [
63+
"namespace" => $this->namespace,
64+
"signatureParameters" => $parameterBuilder->getSignatureParameters(),
65+
];
66+
$this->template->setVar($data, false);
67+
$definition = $this->template->render();
68+
69+
eval($definition);
70+
}
71+
72+
/**
73+
* Returns the fully qualified class name
74+
*
75+
* @return string The class name.
76+
*/
77+
public function getFullyQualifiedClassName()
78+
{
79+
return "$this->namespace\\MockDelegateFunction";
80+
}
81+
}

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/integration",
3+
"type": "library",
4+
"description": "PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock relies on PHP's namespace fallback policy. No further extension is needed.",
5+
"keywords": ["mock", "stub", "test double", "function", "test", "TDD", "BDD"],
6+
"homepage": "https://github.com/php-mock/integration",
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\\integration\\": "classes/"}
18+
},
19+
"require": {
20+
"php": ">=5.4",
21+
"php-mock/php-mock": "^0",
22+
"phpunit/php-text-template": "^1"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^4"
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>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace phpmock\integration;
4+
5+
/**
6+
* Tests MockDelegateFunctionBuilder.
7+
*
8+
* @author Markus Malkusch <[email protected]>
9+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
10+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
11+
* @see MockDelegateFunctionBuilder
12+
*/
13+
class MockDelegateFunctionBuilderTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* Test build() defines a class.
18+
*
19+
* @test
20+
*/
21+
public function testBuild()
22+
{
23+
$builder = new MockDelegateFunctionBuilder();
24+
$builder->build();
25+
$this->assertTrue(class_exists($builder->getFullyQualifiedClassName()));
26+
}
27+
28+
/**
29+
* Test build() would never create the same class name.
30+
*
31+
* @test
32+
*/
33+
public function testSubsequentCallsProduceDifferentClasses()
34+
{
35+
$builder = new MockDelegateFunctionBuilder();
36+
37+
$builder->build();
38+
$class1 = $builder->getFullyQualifiedClassName();
39+
40+
$builder->build();
41+
$class2 = $builder->getFullyQualifiedClassName();
42+
43+
$builder2 = new MockDelegateFunctionBuilder();
44+
$builder2->build();
45+
$class3 = $builder2->getFullyQualifiedClassName();
46+
47+
$this->assertNotEquals($class1, $class2);
48+
$this->assertNotEquals($class1, $class3);
49+
$this->assertNotEquals($class2, $class3);
50+
}
51+
}

tests/MockDelegateFunctionTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace phpmock\integration;
4+
5+
/**
6+
* Tests MockDelegateFunction.
7+
*
8+
* @author Markus Malkusch <[email protected]>
9+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
10+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
11+
* @see MockDelegateFunction
12+
*/
13+
class MockDelegateFunctionTest extends \PHPUnit_Framework_TestCase
14+
{
15+
16+
/**
17+
* @var string The class name of a generated class.
18+
*/
19+
private $className;
20+
21+
protected function setUp()
22+
{
23+
parent::setUp();
24+
25+
$builder = new MockDelegateFunctionBuilder();
26+
$builder->build();
27+
$this->className = $builder->getFullyQualifiedClassName();
28+
}
29+
30+
/**
31+
* Tests delegate() returns the mock's result.
32+
*
33+
* @test
34+
*/
35+
public function testDelegateReturnsMockResult()
36+
{
37+
$expected = 3;
38+
$mock = $this->getMockForAbstractClass($this->className);
39+
40+
$mock->expects($this->once())
41+
->method(MockDelegateFunctionBuilder::METHOD)
42+
->willReturn($expected);
43+
44+
$result = call_user_func($mock->getCallable());
45+
$this->assertEquals($expected, $result);
46+
}
47+
48+
/**
49+
* Tests delegate() forwards the arguments.
50+
*
51+
* @test
52+
*/
53+
public function testDelegateForwardsArguments()
54+
{
55+
$mock = $this->getMockForAbstractClass($this->className);
56+
57+
$mock->expects($this->once())
58+
->method(MockDelegateFunctionBuilder::METHOD)
59+
->with(1, 2);
60+
61+
call_user_func($mock->getCallable(), 1, 2);
62+
}
63+
}

0 commit comments

Comments
 (0)