PHPackages                             c-malet/class-test - 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. c-malet/class-test

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

c-malet/class-test
==================

PHP Class testing

5.0.0(8mo ago)03.3k2[1 PRs](https://github.com/C-Malet/class-test/pulls)MITPHPPHP &gt;=8.3CI passing

Since May 4Pushed 8mo ago3 watchersCompare

[ Source](https://github.com/C-Malet/class-test)[ Packagist](https://packagist.org/packages/c-malet/class-test)[ Docs](https://github.com/c-malet/class-test)[ RSS](/packages/c-malet-class-test/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (17)Used By (0)

Class test
==========

[](#class-test)

[![Current version](https://camo.githubusercontent.com/634105748c45eb17daeff372b79ea590146f93ae292dc6be8106635a9e75bf79/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f632d6d616c65742f636c6173732d746573742e7376673f6c6f676f3d636f6d706f736572)](https://packagist.org/packages/velkuns/pipeline)[![Supported PHP version](https://camo.githubusercontent.com/5bd26c8888ccfdc62df2a75c4e7cf1310ff66c5fc451c3ca0dc2c5e15a2f137a/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6f676f3d706870266c6162656c3d504850266d6573736167653d253545382e3126636f6c6f723d373737626234)](https://packagist.org/packages/c-malet/class-test)[![CI](https://github.com/C-Malet/class-test/workflows/CI/badge.svg)](https://github.com/C-Malet/class-test/workflows/CI/badge.svg)

PHP testing library focused on testing classes mocking all of their constructor parameters, using Prophecy.

The main objective is to make testing a class quicker by automatizing the tested class instantiation with dummy parameters, and retrieve a whole new instance for each test. Particularly useful to test classes with many objects as constructor parameters that you don't want to be executed, such as helpers, loggers, services, repositories, ...

*This library is based on the PHP object mocking framework Prophecy, and requires to know how to test with it, [learn more about it here](https://github.com/phpspec/prophecy)*

Simple example
--------------

[](#simple-example)

```
class TestSomeClass extends ClassTestCase {
    public function getTestedClassName() {
        return SomeClass::class;
    }

    public function getTestedClassConstructorParameters() {
        return [
            SomeRepository::class,
            SomeService::class,
            Logger::class,
            DbInterface::class,
            'someString'
        ];
    }

    public function testSomething() {
        // Retrieve a new instance of SomeClass with dummy constructor parameters
        // that can handle any parameters and return null all the time
        $someClass = $this->getTestedClass();

        $result = $someClass->doSomething('someText', 3);
        $this->assert(...);
    }
}
```

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

[](#installation)

#### Prerequisites

[](#prerequisites)

Requires PHP 8.1 or greater and PHPUnit ^9.0 || ^10.0

#### Composer

[](#composer)

Run composer command `composer require c-malet/class-test`

Usage
-----

[](#usage)

#### Test set up

[](#test-set-up)

Set up the test class for the tested class as such :

Extend the ClassTestCase class (which extends the PHPUnit TestCase)

```
class TestSomeClass extends ClassTestCase {}
```

Implement the two mandatory methods defining the tested class :

```
public function getTestedClassName() {
    return SomeClass::class; // or the fully qualified class name
}

public function getTestedClassConstructorParameters() {
    return [
        SomeClass::class,
        SomeInterface::class,

        // An object can be provided directly, it will be kept unchanged
        $someObject,

        // If you want to provide several instances of the same class,
        // or if you want to force a key to retrieve mocks later on,
        // simply specify a key
        'Logger1' => LoggerInterface::class,
        'Logger2' => LoggerInterface::class,

        // string, integer, array, ... can still be used directly
        'someString',
        12345,

        // If a string would match a class name for instance, you can still
        // force it as a string parameter this way
        'SomeClass' => ClassTestCase::STRING_PARAMETER
    ]
}
```

#### Testing

[](#testing)

For each test, a whole new instance of the tested class is created and can be retrieved :

```
$someClass = $this->getTestedClass();
```

*You can override the `getTestedClass` method in each test case to inform your IDE the class type of `$someClass`*

It returns a "real" instance of the tested class, it is not a mock, and the whole code of this class will be executed.

Contrariwise, every constructor parameter provided that matches an instantiatable class is transformed into a revealed Prophecy, in such a way that they will automatically handle any parameter and always return null by default.

These Prophecy mocks can be retrieved anytime during tests, to do anything you could do with Prophecy alone, using the `getMock` method, allowing you to override the default dummy set up as needed for your tests.

```
$this->getProphecy(SomeRepository::class)->someMethod()->shouldBeCalled(1);
```

*Mocks can be retrieved by their class name or by key, as described earlier in the 'Test set up' part*

If your IDE fails to resolve the methods from the class name and gives warning, you can also get ProphecyMethod objects this way :

```
$this->getProphecyMethod(SomeRepository::class, 'someMethod')->shouldBeCalled(1);
```

If you wish to use the internal mocks container for any other class to mock, for instance mocks you'd want to use in your tested methods, you can also create mocks and dummies that can be retrieved as other mocks with `addNewMock` or `addNewDummy`

```
$this->addNewMock(SomeClassToMock::class);
$this->addNewDummy(SomeClassToDummy::class);
...
$this->getProphecyMock(SomeClassToMock::class)->myMethod()->willReturn(true);
```

Full example
------------

[](#full-example)

```
class TestPizzaCooker extends ClassTestCase {
    public function getTestedClassName() {
        return PizzaCooker::class;
    }

    public function getTestedClassConstructorParameters() {
        OvenInterface::class,
        IngredientPicker::class,
        PizzaFolder::class,
        TimerHelper::class
    }

    public function testPizzaShouldAlwaysBeBaked() {
        $pizzaCooker = $this->getTestedClass();
        $pizzaCooker->cookPizza('margerhita', 'XL')

        $this->getProphecy(OvenInterface::class)->bake()->shouldHaveBeenCalled(1);
    }

    public function testPizzaCalzoneShouldBeFolded() {
        $pizza = $this->getProphecy(OvenInterface::class)->bake()->willReturn(
            $this->addNewDummy(Pizza::class)->reveal()
        );

        // Folder should fold the previously baked pizza
        $this->getProphecyMethod(FolderInterface::class, 'fold')->with([$pizza->reveal()]);

        $pizzaCooker = $this->getTestedClass();
        $pizzaCooker->cookPizza('calzone')

        // A calzone should always be folded
        $this->getProphecy(FolderInterface::class)->bake()->shouldHaveBeenCalled(1);
    }
}
```

Contributing
------------

[](#contributing)

See the [CONTRIBUTING](CONTRIBUTING.md) file.

### Install / update project

[](#install--update-project)

You can install project with the following command:

```
make install
```

And update with the following command:

```
make update
```

Check dependencies:

```
make deps
```

NB: For the components, the `composer.lock` file is not committed.

### Testing &amp; CI (Continuous Integration)

[](#testing--ci-continuous-integration)

#### Tests

[](#tests)

You can run unit tests (with coverage) on your side with following command:

```
make tests
```

For prettier output (but without coverage), you can use the following command:

```
make testdox # run tests without coverage reports but with prettified output
```

#### Code Style

[](#code-style)

You also can run code style check with following commands:

```
make phpcs-check
```

You also can run code style fixes with following commands:

```
make phpcs-fix
```

#### Static Analysis

[](#static-analysis)

To perform a static analyze of your code (with phpstan, lvl max at default), you can use the following command:

```
make analyze
```

To ensure you code still compatible with current supported version at Deezer and futures versions of php, you need to run the following commands (both are required for full support):

Minimal supported version:

```
make phpmin-compatibility
```

Maximal supported version:

```
make phpmax-compatibility
```

#### CI Simulation

[](#ci-simulation)

And the last "helper" commands, you can run before commit and push, is:

```
make ci
```

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance58

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity86

Battle-tested with a long release history

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

Recently: every ~404 days

Total

12

Last Release

269d ago

Major Versions

v1.0.9 → 2.0.02021-01-12

2.0.0 → 3.0.02021-02-11

1.0.10 → 4.0.02023-12-21

4.0.0 → 5.0.02025-08-13

PHP version history (6 changes)v1.0.0PHP ^5.3|^7.0

v1.0.9PHP ^5.6|^7.0

2.0.0PHP ^7.1

3.0.0PHP ^7.3 || ^8

4.0.0PHP ^8.1

5.0.0PHP &gt;=8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/583364c6c76eab5f4c7b5f7c235b1ac2835f066e238241292af168aa9bc922d6?d=identicon)[C-Malet](/maintainers/C-Malet)

---

Top Contributors

[![C-Malet](https://avatars.githubusercontent.com/u/4960389?v=4)](https://github.com/C-Malet "C-Malet (10 commits)")[![Skyree](https://avatars.githubusercontent.com/u/1636904?v=4)](https://github.com/Skyree "Skyree (6 commits)")[![velkuns](https://avatars.githubusercontent.com/u/503648?v=4)](https://github.com/velkuns "velkuns (6 commits)")

---

Tags

phpunittestmockprophecy

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/c-malet-class-test/health.svg)

```
[![Health](https://phpackages.com/badges/c-malet-class-test/health.svg)](https://phpackages.com/packages/c-malet-class-test)
```

###  Alternatives

[phpspec/prophecy-phpunit

Integrating the Prophecy mocking library in PHPUnit test cases

19254.9M1.4k](/packages/phpspec-prophecy-phpunit)[php-mock/php-mock-phpunit

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

1718.2M397](/packages/php-mock-php-mock-phpunit)[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)[colinodell/psr-testlogger

PSR-3 compliant test logger based on psr/log v1's, but compatible with v2 and v3 too!

1712.1M47](/packages/colinodell-psr-testlogger)[elliotchance/concise

Concise is test framework for using plain English and minimal code, built on PHPUnit.

45223.8k4](/packages/elliotchance-concise)[icecave/isolator

Dependency injection for global functions.

371.3M29](/packages/icecave-isolator)

PHPackages © 2026

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