PHPackages                             quizlet/hammock - 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. quizlet/hammock

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

quizlet/hammock
===============

Hammock is a stand-alone mocking library for Hacklang.

v1.1.0(5y ago)27445.5k↓33.3%8[1 PRs](https://github.com/quizlet/hammock/pulls)MITHack

Since Nov 4Pushed 4y ago7 watchersCompare

[ Source](https://github.com/quizlet/hammock)[ Packagist](https://packagist.org/packages/quizlet/hammock)[ Docs](https://github.com/quizlet/hammock)[ RSS](/packages/quizlet-hammock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (14)Used By (0)

Overview
========

[](#overview)

Hammock is a stand-alone mocking library for the Hack language. At its core, it uses `fb_intercept`, which can intercept any function and change its behavior. Hammock aims to provide APIs for mocking public methods and global functions with ease. While it is also possible to mock protected and private methods, it is generally frowned upon to do so. Here are some of Hammock's key features:

- Block-scoped mocking, which automatically restores the original behavior of the mocked function at the end of the block.
- Tracking the intercepted arguments and number of calls into mocked functions.
- Spying on functions without altering their behavior.

Installation
============

[](#installation)

```
composer require --dev quizlet/hammock
```

Usage
=====

[](#usage)

Mock an object's method:

```
$dog = new Dog();

$dog->fetch('ball') === 'ball'; // true

using Hammock\mock_object_method($dog, 'fetch', $args ==> 'frisbee');

$dog->fetch('ball') === 'frisbee'; // true
```

Same thing, but using a block scope:

```
$dog = new Dog();

using (Hammock\mock_object_method($dog, 'fetch', $args ==> 'frisbee')) {
	$dog->fetch('ball') === 'frisbee'; // true
} // Original behavior is restored at the end of the `using` block.

$dog->fetch('ball') === 'ball'; // true
```

Use the intercepted arguments and get the number of calls:

```
$dog = new Dog();

using $fetchMock = Hammock\mock_object_method($dog, 'fetch', $args ==> {
	// Each intercepted argument must be type-asserted.
	// We recommend https://github.com/hhvm/type-assert for this.
	$arg = strval($args[0]);

	if ($arg === 'ball') {
		return 'frisbee';
	}

	return $arg;
});

$dog->fetch('ball') === 'frisbee'; // true
$dog->fetch('bone') === 'bone'; // true

// The arguments for each intercepted call can be retrieved later.
$fetchMock->getArgsForCall(0) === vec['ball']; // true
$fetchMock->getArgsForCall(1) === vec['bone']; // true

// The number of calls from the time the function was mocked.
$fetchMock->getNumCalls() === 2; // true
```

Spy on an object's method without altering its behavior:

```
$dog = new Dog();

using $fetchSpy = Hammock\spy_object_method($dog, 'fetch');

$dog->fetch('ball') === 'ball'; // true

$fetchSpy->getNumCalls() === 1; // true
```

Mock a class' method when no instance is available:

```
using Hammock\mock_class_method(Ball::class, 'bounce', $args ==> false);

Ball::bounce() === false; // true
```

The full API documentation can be found in [MAIN\_API.md](https://github.com/quizlet/hammock/blob/master/MAIN_API.md).

Advanced use cases can be found in [ADVANCED.md](https://github.com/quizlet/hammock/blob/master/ADVANCED.md).

Contributing
============

[](#contributing)

If you ever wanted to contribute to an open-source project, now is the chance! Please read [CONTRIBUTING.md](https://github.com/quizlet/hammock/blob/master/CONTRIBUTING.md) to understand the process for submitting a pull request.

Acknowledgements
================

[](#acknowledgements)

Thanks to the following people who have contributed to Hammock:

- [Tyron Jung](https://github.com/tyronjung-quizlet)
- [Riya Dashoriya](https://github.com/riyadashoriya-qz)
- [Lexidor](https://github.com/lexidor)
- [Karoun Kasraie](https://github.com/karoun)
- [Andrew Sutherland](https://github.com/asuth)
- [Josh Rai](https://github.com/joshrai)
- [Shaobo Sun](https://github.com/shaobos)
- [Turadg Aleahmad](https://github.com/turadg)
- [Sean Young](https://github.com/syoung-quizlet)
- [Chris Opperwall](https://github.com/copperwall)

Contact
=======

[](#contact)

Please reach out to  if you have any questions.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity41

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~26 days

Recently: every ~38 days

Total

12

Last Release

2100d ago

Major Versions

v0.2.0 → v1.0.02020-03-25

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/50502922?v=4)[Riya Dashoriya](/maintainers/riyadashoriya-qz)[@riyadashoriya-qz](https://github.com/riyadashoriya-qz)

![](https://www.gravatar.com/avatar/dfa1efd09273e967d93bc7916b90b6daaf2cd3aebcb5efda4ced4fd05299b5c5?d=identicon)[tyronjung](/maintainers/tyronjung)

---

Top Contributors

[![lexidor](https://avatars.githubusercontent.com/u/31805625?v=4)](https://github.com/lexidor "lexidor (66 commits)")[![tyronjung-quizlet](https://avatars.githubusercontent.com/u/41349925?v=4)](https://github.com/tyronjung-quizlet "tyronjung-quizlet (53 commits)")[![riyadashoriya-qz](https://avatars.githubusercontent.com/u/50502922?v=4)](https://github.com/riyadashoriya-qz "riyadashoriya-qz (9 commits)")[![copperwall](https://avatars.githubusercontent.com/u/2539016?v=4)](https://github.com/copperwall "copperwall (6 commits)")[![kelseywong-qz](https://avatars.githubusercontent.com/u/62259148?v=4)](https://github.com/kelseywong-qz "kelseywong-qz (1 commits)")[![rlarner-quizlet](https://avatars.githubusercontent.com/u/44449417?v=4)](https://github.com/rlarner-quizlet "rlarner-quizlet (1 commits)")

---

Tags

phptestingmockingtestlibrarymockhackhacklanghammock

### Embed Badge

![Health badge](/badges/quizlet-hammock/health.svg)

```
[![Health](https://phpackages.com/badges/quizlet-hammock/health.svg)](https://phpackages.com/packages/quizlet-hammock)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[letsdrink/ouzo-goodies

Utility classes, test assertions and mocking framework extracted from Ouzo framework.

132617.9k7](/packages/letsdrink-ouzo-goodies)[janmarek/mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

29221.0k28](/packages/janmarek-mockista)[hackpack/hackunit

An xUnit testing framework for Hack

612.7k14](/packages/hackpack-hackunit)[doppiogancio/mocked-client

A simple way to mock a client

2174.9k3](/packages/doppiogancio-mocked-client)

PHPackages © 2026

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