Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e748e76

Browse files
committed
added shouldReceive method to facade.
1 parent 828b58f commit e748e76

3 files changed

Lines changed: 69 additions & 11 deletions

File tree

src/Illuminate/Support/Facades/Facade.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ abstract class Facade {
1616
*/
1717
protected static $resolvedInstance;
1818

19+
/**
20+
* Hotswap the underlying instance behind the facade.
21+
*
22+
* @param mixed $instance
23+
* @return void
24+
*/
25+
public static function swap($instance)
26+
{
27+
static::$resolvedInstance = $instance;
28+
29+
static::$app->instance(static::getFacadeAccessor(), $instance);
30+
}
31+
32+
/**
33+
* Initiate a mock expectation on the facade.
34+
*
35+
* @param dynamic
36+
* @return Mockery\Expectation
37+
*/
38+
public static function shouldReceive()
39+
{
40+
static::$resolvedInstance = $mock = \Mockery::mock(get_class(static::getFacadeRoot()));
41+
42+
static::$app->instance(static::getFacadeAccessor(), $mock);
43+
44+
return call_user_func_array(array($mock, 'shouldReceive'), func_get_args());
45+
}
46+
1947
/**
2048
* Get the root object behind the facade.
2149
*

src/Illuminate/Support/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"php": ">=5.3.0",
1212
"patchwork/utf8": "1.0.*"
1313
},
14+
"require-dev": {
15+
"mockery/mockery": "0.7.2"
16+
},
1417
"autoload": {
1518
"psr-0": {
1619
"Illuminate\\Support": ""
Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,61 @@
11
<?php
22

3+
use Mockery as m;
4+
35
class SupportFacadeTest extends PHPUnit_Framework_TestCase {
46

7+
public function setUp()
8+
{
9+
Illuminate\Support\Facades\Facade::clearResolvedInstances();
10+
}
11+
12+
13+
public function tearDown()
14+
{
15+
m::close();
16+
}
17+
18+
519
public function testFacadeCallsUnderlyingApplication()
620
{
7-
FacadeStub::setFacadeApplication(array('foo' => new ApplicationStub));
21+
$app = new ApplicationStub;
22+
$app->setAttributes(array('foo' => $mock = m::mock('StdClass')));
23+
$mock->shouldReceive('bar')->once()->andReturn('baz');
24+
FacadeStub::setFacadeApplication($app);
825
$this->assertEquals('baz', FacadeStub::bar());
926
}
1027

28+
29+
public function testShouldReceiveReturnsAMockeryMock()
30+
{
31+
$app = new ApplicationStub;
32+
$app->setAttributes(array('foo' => new StdClass));
33+
FacadeStub::setFacadeApplication($app);
34+
35+
$this->assertInstanceOf('Mockery\MockInterface', $mock = FacadeStub::shouldReceive('foo')->once()->with('bar')->andReturn('baz')->getMock());
36+
$this->assertEquals('baz', $app['foo']->foo('bar'));
37+
}
38+
1139
}
1240

1341
class FacadeStub extends Illuminate\Support\Facades\Facade {
1442

15-
/**
16-
* Get the registered name of the component.
17-
*
18-
* @return string
19-
*/
2043
protected static function getFacadeAccessor()
2144
{
2245
return 'foo';
2346
}
2447

2548
}
2649

27-
class ApplicationStub {
50+
class ApplicationStub implements ArrayAccess {
2851

29-
public function bar()
30-
{
31-
return 'baz';
32-
}
52+
protected $attributes = array();
53+
54+
public function setAttributes($attributes) { $this->attributes = $attributes; }
55+
public function instance($key, $instance) { $this->attributes[$key] = $instance; }
56+
public function offsetExists($offset) { isset($this->attributes[$offset]); }
57+
public function offsetGet($key) { return $this->attributes[$key]; }
58+
public function offsetSet($key, $value) { $this->attributes[$key] = $value; }
59+
public function offsetUnset($key) { unset($this->attributes[$key]); }
3360

3461
}

0 commit comments

Comments
 (0)