2017 © Pedro Peláez
 

library fnmock

Mock PHP functions

image

fnmock/fnmock

Mock PHP functions

  • Wednesday, April 8, 2015
  • by adamnicholson
  • Repository
  • 1 Watchers
  • 2 Stars
  • 28 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

FnMock

A PHP testing tool for mocking functions., (*1)

Most mocking frameworks only allow mocking of objects. FnMock is a tiny class that makes it easy to test functions outside the context of a class., (*2)

Important: Mocking functions is only supported when the function caller is within a namespace., (*3)

Example

// Code
namespace Http;

class Client {
    public function touch($url) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);

        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
}

```php // Test use FnMock\FnMock;, (*4)

$client = new Http\Client();, (*5)

FnMock::mock('Http\curl_exec', function($ch) { $url = curl_getinfo($ch)['url']; assert($url === 'http://google.com'); return 'Fake response from server'; });, (*6)

$response = $client->touch('http://google.com'); assert($response === 'Fake response from server');, (*7)

// Important: Reset FnMock as part of your tearDown process FnMock::reset();, (*8)


## Install

composer require fnmock/fnmock, (*9)



## Integrate it with PHPUnit ```php use FnMock\FnMock; class TestCase extends \PHPUnit_Framework_TestCase { protected function mockFunction($fn, callable $callback) { FnMock::mock($fn, $callback); } public function tearDown() { FnMock::reset(); parent::tearDown(); } }

Or just use the trait we provide, (*10)

use FnMock\PHPUnitTrait;

class TestCase extends \PHPUnit_Framework_TestCase
{
    use PHPUnitTrait;
}

License

FnMock is licensed under the MIT License - see the LICENSE.txt file for details, (*11)

Author

Adam Nicholson - adamnicholson10@gmail.com, (*12)

The Versions

08/04 2015

dev-master

9999999-dev

Mock PHP functions

  Sources   Download

The Development Requires

by Adam Nicholson

26/03 2015

0.1.1

0.1.1.0

Mock PHP functions

  Sources   Download

The Development Requires

by Adam Nicholson

26/03 2015

0.1

0.1.0.0

Mock PHP functions

  Sources   Download

by Adam Nicholson