PHPackages                             cyruscollier/phpspec-php-mock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. cyruscollier/phpspec-php-mock

ActiveLibrary[Testing &amp; Quality](/categories/testing)

cyruscollier/phpspec-php-mock
=============================

Adds the PHP Mock function mocking library as a phpspec Collaborator

2.1.2(6y ago)1404MITPHP

Since Aug 26Pushed 6y ago1 watchersCompare

[ Source](https://github.com/cyruscollier/phpspec-php-mock)[ Packagist](https://packagist.org/packages/cyruscollier/phpspec-php-mock)[ Docs](http://github.com/cyruscollier/phpspec-php-mock)[ RSS](/packages/cyruscollier-phpspec-php-mock/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (7)Used By (0)

PhpSpec - PHP-Mock Extension
============================

[](#phpspec---php-mock-extension)

[![Build Status](https://camo.githubusercontent.com/bda4de9bcc87cb57b9855654d93bc2822455485647238d62ada1b0ac2c3226da/68747470733a2f2f7472617669732d63692e6f72672f6379727573636f6c6c6965722f706870737065632d7068702d6d6f636b2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/cyruscollier/phpspec-php-mock)

Adds the PHP Mock function mocking library as a phpspec Collaborator

This [phpspec](http://www.phpspec.net/) extension allows you to mock non-deterministic PHP core functions (`time()`, `rand()`, etc.), or mock functions from other libraries or frameworks that have side effects from dependencies like a database, filesystem or HTTP request.

By using the specially named parameter `$functions` in any example method, **phpspec-php-mock** will turn that parameter into a special `FunctionCollaborator` that wraps the [php-mock-prophecy](https://github.com/php-mock/php-mock-prophecy) library's `PHPProphet`. This allows you to mock return values for *any* function as you normally would for an `ObjectProphecy`.

Changelog
---------

[](#changelog)

v2.1.2 - Fixed tests related to previous version, updated phpspec to 6.x

v2.1.1 - Interface return type fix

v2.1 - Added support for defining multiple namespaces for function prophecies

v2.0 - Updated for phpspec 4.x, added spec and doc for usage with Throw Matcher

v1.0 - Initial build for phpspec 2.x

Installation
------------

[](#installation)

Add this to your composer.json:

```

    {
        "require-dev": {
            "cyruscollier/phpspec-php-mock": "dev-master"
        }
    }

```

Then add this to your phpspec.yml:

```

    extensions:
      PhpSpec\PhpMock\Extension\PhpMockExtension: ~

```

Example
-------

[](#example)

A PHP class that uses a non-deterministic function:

```
    class Time
    {
        function getCurrentTime()
        {
            return time();
        }
    }
```

The spec for that class that mocks the `time()` function:

```
    use PhpSpec\ObjectBehavior;

    class TimeSpec extends ObjectBehavior
    {
        function it_is_initializable()
        {
            $this->shouldHaveType('Time');
        }

        function it_gets_the_current_time($functions)
        {
            $functions->time()->willReturn(123);
            $this->getCurrentTime()->shouldReturn(123);
        }
    }
```

Examples that test Exceptions require an extra line to reveal the function prophecy manually, since the Throw Matcher executes the Subject method differently than the other matchers :

```
    use PhpSpec\ObjectBehavior;

    class TimeSpec extends ObjectBehavior
    {
        function it_is_initializable()
        {
            $this->shouldHaveType('Time');
        }

        function it_gets_the_current_time($functions)
        {
            $functions->time()->willReturn(123);
            $functions->reveal();
            $this->shouldThrow('\Exception')->during('getCurrentTime', [123]);
        }
    }
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~303 days

Total

5

Last Release

2380d ago

Major Versions

1.0.0 → 2.02018-03-15

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6005043?v=4)[Cyrus Collier](/maintainers/cyruscollier)[@cyruscollier](https://github.com/cyruscollier)

---

Top Contributors

[![cyruscollier](https://avatars.githubusercontent.com/u/6005043?v=4)](https://github.com/cyruscollier "cyruscollier (42 commits)")

---

Tags

testprophecyphpspecphpmock

### Embed Badge

![Health badge](/badges/cyruscollier-phpspec-php-mock/health.svg)

```
[![Health](https://phpackages.com/badges/cyruscollier-phpspec-php-mock/health.svg)](https://phpackages.com/packages/cyruscollier-phpspec-php-mock)
```

###  Alternatives

[benconstable/phpspec-laravel

Test your Laravel applications with PhpSpec

144350.6k15](/packages/benconstable-phpspec-laravel)[friends-of-phpspec/phpspec-code-coverage

Generate Code Coverage reports for PhpSpec tests

202.6M129](/packages/friends-of-phpspec-phpspec-code-coverage)[phpspec/nyan-formatters

PHPSpec Nyan Formatter Extension

77240.3k23](/packages/phpspec-nyan-formatters)[php-mock/php-mock-prophecy

Mock built-in PHP functions (e.g. time()) with Prophecy. This package relies on PHP's namespace fallback policy. No further extension is needed.

16513.2k15](/packages/php-mock-php-mock-prophecy)[ramunasd/symfony-container-mocks

Provides extended Symfony dependency injection container enabling service mocking.

2297.0k2](/packages/ramunasd-symfony-container-mocks)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
