Skip to content

Commit

Permalink
Merge pull request #684 from davedevelopment/global-helpers
Browse files Browse the repository at this point in the history
Adds some global helper functions
  • Loading branch information
davedevelopment committed Jan 27, 2017
2 parents 85ccdd4 + 2ab9136 commit 0e36552
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Bumped minimum PHP version to 5.6
* `andThrow` will now throw anything `\Throwable`
* Adds `allows` and `expects` syntax
* Adds optional global helpers for `mock`, `namedMock` and `spy`

## 0.9.4 (XXXX-XX-XX)

Expand Down
10 changes: 10 additions & 0 deletions library/Mockery.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class Mockery
*/
private static $_filesToCleanUp = [];

/**
* Defines the global helper functions
*
* @return void
*/
public static function globalHelpers()
{
require_once __DIR__.'/helpers.php';
}

/**
* Static shortcut to \Mockery\Container::mock().
*
Expand Down
37 changes: 37 additions & 0 deletions library/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2016 Dave Marshall
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

if (!function_exists("mock")) {
function mock() {
return call_user_func_array([Mockery::class, "mock"], func_get_args());
}
}

if (!function_exists("spy")) {
function spy() {
return call_user_func_array([Mockery::class, "spy"], func_get_args());
}
}

if (!function_exists("namedMock")) {
function namedMock() {
return call_user_func_array([Mockery::class, "namedMock"], func_get_args());
}
}
56 changes: 56 additions & 0 deletions tests/Mockery/GlobalHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2016 Dave Marshall
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

class GlobalHelpersTest extends PHPUnit_Framework_TestCase
{
public function setup()
{
\Mockery::globalHelpers();
}

/** @test */
public function mock_creates_a_mock()
{
$double = mock();

$this->assertInstanceOf(\Mockery\MockInterface::class, $double);
$this->setExpectedException(\Exception::class);
$double->foo();
}

/** @test */
public function spy_creates_a_spy()
{
$double = spy();

$this->assertInstanceOf(\Mockery\MockInterface::class, $double);
$double->foo();
}

/** @test */
public function named_mock_creates_a_named_mock()
{
$className = "Class".uniqid();
$double = namedMock($className);

$this->assertInstanceOf(\Mockery\MockInterface::class, $double);
$this->assertInstanceOf($className, $double);
}
}

0 comments on commit 0e36552

Please sign in to comment.