PHPackages                             cocur/ea - 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. cocur/ea

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

cocur/ea
========

Code generation for testing. Because mocks sometimes just don't cut it.

27PHP

Since Mar 15Pushed 11y ago1 watchersCompare

[ Source](https://github.com/cocur/ea)[ Packagist](https://packagist.org/packages/cocur/ea)[ RSS](/packages/cocur-ea/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Ea
==

[](#ea)

> Code generation for testing. Because mocks sometimes just don't cut it.

[![Build Status](https://camo.githubusercontent.com/2b26a6df33aade1e6fe30b31b5cca3b4c1ce4c429b10a589acc3aa1f1567e612/68747470733a2f2f7472617669732d63692e6f72672f636f6375722f65612e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/cocur/ea)

Developed by [Florian Eckerstorfer](https://florian.ec) in Vienna, Europe.

Features
--------

[](#features)

- Useful if the code under test contains calls to functions like `method_exists()`
- Generate classes with methods and properties
- Generate setters, getters, hassers, issers, and adders based on properties

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

[](#installation)

You can install Ea using [Composer](https://getcomposer.org):

```
$ composer require cocur/ea:dev-master
```

Usage
-----

[](#usage)

*Ea was developed to generate code for testing purposes. For example, if your code under test contains a call to a function like `method_exists()` typical mocks don't work.*

You can generate a class using `ClassFactory`. The first argument is the class name and the second argument is the namespace (which is optional):

```
$class = new ClassFactory('Foo', 'Foobar');
echo $class->generate();
```

will output

```
namespace Foobar;
class Foo {
}
```

You can create properties with `PropertyFactory` and use the `addProperty()` method to add them to a class. Properties have a name, a visibility (`public`, `protected` or `private`), can be static and can have a default value.

```
$property = new PropertyFactory('foo, 'public');
$property->isStatic(true);
$property->setDefault('bar');
$property->generate(); // "public static $foo = 'bar';"
```

If you call the `setDefault()` method a default value will be set, even if you use `null` or `false` as argument. The value given to `setDefault()` will be used as default. If you don't call `setDefault()` no default value will be set. You can check if a property has a default value using `hasDefault()` and remove it using `removeDefault()`.

Alternatively the `addProperty()` method of `ClassFactory` can create the `PropertyFactory` object for you, if you call it with a string as first parameter instead of an instance of `PropertyFactory`. The second argument is the visibility, the third static and the fourth the default value. If you call `addProperty()` with four arguments a default value will be set, if not no default value will be set.

```
$class->addProperty('foo', 'protected', false, null);
$class->addProperty('bar', 'public', true);
echo $class->generate();
```

will result in

```
// ...
    protected $foo = null;
    public static $bar;
// ...
```

Methods are created using `MethodFactory` and added to the class by calling `addMethod()`.

```
$method = new MethodFactory('foo', 'protected');
$method->isStatic(false);
$method->addArgument('foo', null, 21);
$method->setBody('return 21+$foo');
$method->generate(); // "protected function foo($foo = 21) { return 21+$foo; }"
```

The constructor takes the name and the visibility (`public`, `protected`, `private`) as arguments and `isStatic()` sets whether the method should be static or not. The code passed to `setBody()` will be returned as-is as body of the method. The `addArgument()` method takes the name of the argument as first parameter, the type (like `array` or `stdClass`; none by default) as second argument and the default value as third argument. If three arguments are provided the default value will always be set; if less arguments are provided no default value will be set.

For some common property access methods, such as setters, getters, hassers, adders and issers, the `ClassFactory`provides helper methods to quickly create these based on a given `PropertyFactory`.

```
$class->addGetter($property);
$class->addSetter($property);
$class->addIsser($property);
$class->addHasser($property);
```

The `addAdder()` method is a little bit different, as it provides more options. The second argument is a `bool` to indicate whether the value should be added by a key. The third argument is the singular name of the property. Example:

```
$bars = PropertyFactory('bars');
$class->addAdder($bars, true, 'bar');
$class->generate(); // -> "public function addBar($key, $bar) { $this->bars[$key] = $bar; }"
```

Instead of generating the code you can also dynamically create the classes using `execute()` of the `Ea`. **Attention. Internally Ea uses `eval()` to create the classes.**

```
Ea::create()->addClass($class)->execute();
```

License
-------

[](#license)

The MIT license applies to Ea. For the full copyright and license information, please view the [LICENSE](https://github.com/cocur/ea/blob/master/LICENSE) file distributed with this source code.

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

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

### Embed Badge

![Health badge](/badges/cocur-ea/health.svg)

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

###  Alternatives

[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14429.2M361](/packages/dms-phpunit-arraysubset-asserts)[gong023/assert_chain

enable you to use phpunit assert with method chain

1478.9k3](/packages/gong023-assert-chain)

PHPackages © 2026

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