Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc1fe06

Browse files
committed
added PHPMockery::define()
1 parent e40b470 commit bc1fe06

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ assert (3 == time());
3333
\Mockery::close();
3434
```
3535

36+
## Restrictions
37+
38+
This library comes with the same restrictions as the underlying
39+
[`php-mock`](https://github.com/php-mock/php-mock#requirements-and-restrictions):
40+
41+
* Only *unqualified* function calls in a namespace context can be mocked.
42+
E.g. a call for `time()` in the namespace `foo` is mockable,
43+
a call for `\time()` is not.
44+
45+
* The mock has to be defined before the first call to the unqualified function
46+
in the tested class. This is documented in [Bug #68541](https://bugs.php.net/bug.php?id=68541).
47+
In most cases you can ignore this restriction. But if you happen to run into
48+
this issue you can call [`PHPMockery::define()`](http://php-mock.github.io/php-mock-mockery/api/class-phpmock.mockery.PHPMockery.html#_define)
49+
before that first call. This would define a side effectless namespaced function.
50+
3651
# License and authors
3752

3853
This project is free and under the WTFPL.

‎classes/PHPMockery.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,33 @@ public static function mock($namespace, $name)
6161

6262
return $expectation;
6363
}
64+
65+
/**
66+
* Defines the mocked function in the given namespace.
67+
*
68+
* In most cases you don't have to call this method. {@link mock()}
69+
* is doing this for you. But if the mock is defined after the first call in the
70+
* tested class, the tested class doesn't resolve to the mock. This is
71+
* documented in Bug #68541. You therefore have to define the namespaced
72+
* function before the first call.
73+
*
74+
* Defining the function has no side effects. If the function was
75+
* already defined this method does nothing.
76+
*
77+
* @see mock()
78+
* @link https://bugs.php.net/bug.php?id=68541 Bug #68541
79+
*
80+
* @param string $namespace The function namespace.
81+
* @param string $name The function name.
82+
*/
83+
public static function define($namespace, $name)
84+
{
85+
$builder = new MockBuilder();
86+
$builder->setNamespace($namespace)
87+
->setName($name)
88+
->setFunction(function () {
89+
})
90+
->build()
91+
->define();
92+
}
6493
}

‎tests/PHPMockeryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ protected function disableMocks()
2121
{
2222
Mockery::close();
2323
}
24+
25+
protected function defineFunction($namespace, $functionName)
26+
{
27+
PHPMockery::define($namespace, $functionName);
28+
}
2429

2530
protected function mockFunction($namespace, $functionName, callable $function)
2631
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /