Open menu icon N-ARY CIRCLED PLUS OPERATOR
atoum

Release 2.5.0

@atoum/RMs - 08 Jan 2016

Stats

  • 45 commits,
  • 69 files changed,
  • 6 contributors,
  • 8 new features,
  • 3 bug fixes.

What’s new

Asserting on child arrays

Asserting on arrays has just become a bit more awesome. Remember when you wanted to assert on child arrays, you had to do things like:

<?php 

$array = [
    'foo',
    42,
    'baz' => [
        'boo'
    ]
];

$this
    ->array($array)
        ->string[0]->isEqualTo('foo')
        ->integer[1]->isEqualTo(42)
        ->hasKey('baz')
    ->array($array['baz'])
        ->string[0]->isEqualTo('boo')
;

The chain was broken because you were not able to easily assert on child arrays. With this new release, atoum brings a new helper method to do this:

<?php

$this
    ->array($array)
        ->string[0]->isEqualTo('foo')
        ->integer[1]->isEqualTo(42)
        ->child['baz'](function($child) {
                $child->string[0]->isEqualTo('boo');
            }
        )
    ->array($array['baz'])
        ->string[0]->isEqualTo('boo')
;

You might also know that atoum provides an alternative syntax to assert on child arrays:

<?php

$this
    ->array($array)
        ->string[0]->isEqualTo('foo')
        ->integer[1]->isEqualTo(42)
        ->string['baz'][0]->isEqualTo('boo')
;

Just choose the one you prefer and fits the best to your needs.

castToArray and iterator asserters

This is now possible to assert on iterators thanks to a dedicated iterator asserter group:

<?php

$iterator = new \someIterator();

$this
    ->iterator($iterator)
        ->hasSize(5)
;

This asserter will only check that the subject value implements the Iterator interface and will allow you to use a single hasSize assertion as regular iterators can’t be randomly accessed.

castToArray to the rescue: This new asserter will let you cast any value to an array to assert on it. This is very usefull with iterators:

<?php

$this
    ->castToArray($iterator)
        ->hasSize(5)
        ->object[0]->isInstanceOf('stdClass')
;

There is also a toArray shortcut method to cast any iterator or string asserter group to an array:

<?php

$this
    ->iterator($iterator)
        ->hasSize(5)
        ->toArray
            ->object[0]->isInstanceOf('stdClass')
            
    ->string('Hello World!')
        ->toArray
            ->string[0]->isEqualTo('H')
;

resource asserter

The resource asserter group provides useful asserters to check resources’ types. For instance:

<?php

$this
    ->resource($var)
        ->isOfType('stream')
;

is true if the resource $var is a stream. In many cases, we can use the is* magic methods as a replacement of isOfType. Thus:

<?php

$this
    ->resource($var)
        ->isStream()
;

is strictly equivalent.

Finally, if you would like more thinner asserters on the type of the resource, you can use ->resource($var)->type->… where type returns a string asserter group.

Discover all the available resources in PHP at http://php.net/resource.

Improvements on mock

Mock generator has been updated to support variadic arguments passed by reference. Method declared with such arguments will be correctly mocked.

Another interesting feature around mocks is the automatic mock generation and injection. When you wanted to use mocks in a test, you had to do things like:

<?php

class cachingIterator extends atoum
{
    public function test__construct()
    {
        $this
            ->given($iterator = new \mock\iterator())
            ->then
                ->object(new \cachingIterator($iterator))               
        ;
    }
}

Now instead of manually generating the mock, you can ask atoum to do it for you:

<?php

class cachingIterator extends atoum
{
    public function test__construct(\iterator $iterator)
    {
        $this
            ->object(new \cachingIterator($iterator))               
        ;
    }
}

When you add type-hints to a test method (which is not associated with a data provider) atoum will automatically generate mocks for its arguments ad inject them automatically.

atoum has always provided a simple and nice way of generating mocks: you simply have to instanciate a class with top-namespace mock\. Some users don’t like this so we added a new helper method on test classes:

$mock = $this->newMockInstance('cachingIterator');

Of course, this method provides the exatc same features as the previous one: you can mock concrete classes, abstract classes and interfaces.

Asides

Stubs

atoum stubs has been updated according to the features introduced in this release. Don’t forget to add them to your composer.json if you want to have a better IDE integration (like autocompletion).

Extensions

Some extensions have been updated:

Thanks Adrien Gallou for the amazing work he did on those extensions!

Documentation

We also updated missing parts of the documentation. Again, Adrien Gallou and our two doc masters did an amazing job!