PHPackages                             potherca/phpunit-testcase-traits - 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. potherca/phpunit-testcase-traits

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

potherca/phpunit-testcase-traits
================================

Traits that offer helper functions to be used in PHPUnit TestCases.

v0.7.0(8y ago)06.4k1[1 issues](https://github.com/Potherca/phpunit-testcase-traits/issues)[2 PRs](https://github.com/Potherca/phpunit-testcase-traits/pulls)GPL-3.0-or-laterPHPPHP ~5.3 | ~7.0

Since Jun 23Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Potherca/phpunit-testcase-traits)[ Packagist](https://packagist.org/packages/potherca/phpunit-testcase-traits)[ Docs](https://github.com/Potherca/phpunit-testcase-traits)[ RSS](/packages/potherca-phpunit-testcase-traits/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (1)Versions (23)Used By (0)

Traits for PHPUnit Testcases
============================

[](#traits-for-phpunit-testcases)

[![project-stage-badge](https://camo.githubusercontent.com/75d108879c5a75bbf773fca6137514a69cd1a6de3fa3133e90da3959aae20980/687474703a2f2f696d672e736869656c64732e696f2f62616467652f50726f6a65637425323053746167652d446576656c6f706d656e742d79656c6c6f77677265656e2e737667)](http://bl.ocks.org/potherca/raw/a2ae67caa3863a299ba0/)[![license-badge](https://camo.githubusercontent.com/c8e817d0fab13b6b935489e0692f5301982fdcc96451d589d7444f2055cf9a7c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d47504c2d2d332e302d626c75652e737667)](./LICENSE.md)

> Traits that offer helper functions to be used in PHPUnit TestCases.

Introduction
------------

[](#introduction)

This projects contains various traits that offer convenience methods for tasks that occur when creating test code.

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

[](#installation)

```
composer require 'potherca/phpunit-testcase-traits'

```

Usage
-----

[](#usage)

Add a `use` statement for a desired trait to a class definition. The public API of that trait can then be used in the defined class.

For full details on how to use traits, please refer to [the section on traits in the PHP manual](http://php.net/manual/en/language.oop5.traits.php).

### PHP 5.3 compatibility

[](#php-53-compatibility)

Traits were not introduced until PHP5.4 so for older versions (i.e. PHP5.3) another way to load the trait's functionality is needed. A `traitShim` function is provided that can be used from a [magic `__call` method](http://php.net/manual/en/language.oop5.overloading.php#object.call).

This is done by adding the following code to each (abstract base) test-case were a Trait is to be used(1):

```
class ExampleTest extends \PHPUnit_Framework_TestCase
{
    // ....

    final public function __call($name, array $parameters)
    {
        return \Potherca\PhpUnit\Shim\Util::traitShim($this, $name, $parameters);
    }

    // ....
}
```

The public API of *all* traits can then be used.

In order to aid text-editors and IDEs in offering auto-completion, the following doc-block can be added to the test-case class:

```
/**
 * @method array[] createDataProvider(array $subject)
 * @method \PHPUnit_Framework_MockObject_MockObject | \PHPUnit\Framework\MockObject\MockObject createObjectFromAbstractClass($className)
 * @method string getCompatibleExceptionName($exceptionName)
 * @method void setDataProviderMaximumKeyLength($length)
 * @method void setDataProviderSortByKey($sort)
 * @method void setNonPublicProperty($subject, $name, $value)
 */
```

(1) Alternatively, the `src/Shim/function.traitShim.php` could be [loaded using composer's autoloader](https://getcomposer.org/doc/04-schema.md#files).

Available traits
----------------

[](#available-traits)

- **CreateDataProviderTrait** -- *Create data-provider arrays.*
    Creates a `key/value` pair from a given one-dimensional array of values, which is meant to be returned from a data-provider method in a TestCase.
- **CreateObjectFromAbstractClassTrait** -- *Create an object instance from an abstract class.*
    Creates a concrete object whose methods can be called and, thus, be tested.
- **GetCompatibleExceptionNameTrait** -- *Provide names of PHP5 compatible `PHPUnit_Framework_Exception` for (new) PHP7 Exceptions.*
- **SetNonPublicPropertyTrait** -- *Change the value of a non-public class properties.*

Functioning usage examples are available in the [`example`](./example) directory. All examples can be run with `phpunit`. Simply use the `--config` flag to point to the desired config file (either `example-php-phpunit.xml` for the traits or `example-php53-phpunit.xml` for the PHP5.3 compatible Trait shims).

Colophon
--------

[](#colophon)

- **Author**: Created by [Potherca](http://pother.ca/).
- **Homepage**: [packagist](https://packagist.org/packages/potherca/phpunit-testcase-traits) / [git-repo](https://github.com/Potherca/phpunit-testcase-traits)
- **License**: Licensed under the [GPL-3.0 license](./LICENSE.md) (GNU General Public License v3.0)

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.9% 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 ~12 days

Recently: every ~0 days

Total

19

Last Release

3031d ago

PHP version history (2 changes)v0.0.0PHP ~5.6 | ~7.0

v0.5.0PHP ~5.3 | ~7.0

### Community

Maintainers

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

---

Top Contributors

[![Potherca](https://avatars.githubusercontent.com/u/195757?v=4)](https://github.com/Potherca "Potherca (62 commits)")[![CodeLingoBot](https://avatars.githubusercontent.com/u/45469328?v=4)](https://github.com/CodeLingoBot "CodeLingoBot (1 commits)")[![renovate-bot](https://avatars.githubusercontent.com/u/25180681?v=4)](https://github.com/renovate-bot "renovate-bot (1 commits)")

---

Tags

phpphp53-compatiblephpunitpothercatraitstestingphpunithelpertraittestspothercatraitstest case

### Embed Badge

![Health badge](/badges/potherca-phpunit-testcase-traits/health.svg)

```
[![Health](https://phpackages.com/badges/potherca-phpunit-testcase-traits/health.svg)](https://phpackages.com/packages/potherca-phpunit-testcase-traits)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[typo3/testing-framework

The TYPO3 testing framework provides base classes for unit, functional and acceptance testing.

675.0M775](/packages/typo3-testing-framework)[robiningelbrecht/phpunit-pretty-print

Prettify PHPUnit output

76460.0k15](/packages/robiningelbrecht-phpunit-pretty-print)[juampi92/test-seo

Easy way to test your SEO

26341.0k](/packages/juampi92-test-seo)[janmarek/mockista

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

29221.0k28](/packages/janmarek-mockista)[code-distortion/adapt

A Laravel package that builds databases for your tests, improving their speed.

2835.5k](/packages/code-distortion-adapt)

PHPackages © 2026

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