Testing a trait with PHPUnit

Posted: 2017-06-08 13:57:43

In an effort to start recording test coverage using vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover I had some issues with traits being seen.

I tried numerous approaches my default being just to Use the trait in the test class. I ended up with this as I found some online info about trait testing here that references a now missing post by Sebastian Bergman the maker of PHPUnit

<?php

use Symfony\Component\Yaml\Yaml;

/**
 * Test class for {@see PfawsYaml}.
 *
 * @covers \PfAws\Traits\PfawsYaml
 */
class PfawsYamlTest extends TestCase
{

    /**
     * The object under test.
     *
     * @var object
     */
    protected $traitObject;

    public function setUp()
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->traitObject = $this->createObjectForTrait();
    }

    /**
     * *Creation Method* to create an object for the trait under test.
     *
     * @return object The newly created object.
     */
    private function createObjectForTrait()
    {
        $traitName = '\PfAws\Traits\PfawsYaml';

        return $this->getObjectForTrait($traitName);
    }

    /**
     * coversDefaultClass loadPfawsYaml
     */
    public function testLoadPfawsYaml()
    {
        $path = __DIR__ . '/fixtures/pfaws.yaml';

        $results = $this->traitObject->loadPfawsYaml('staging', $path);

        $this->assertEquals("bar2", $results->getPfawsYamlEnv('aws_key'));

        $this->assertEquals("foo2", $results->getPfawsYamlEnv('aws_secret'));

        $this->assertNotNull($this->traitObject->getPfawsyaml());
    }

    /**
     * @coversDefaultClass getPfawsYamlEnv
     */
    public function testGetPfawsYamlEnv()
    {
        $path = __DIR__ . '/fixtures/pfaws.yaml';

        $results = $this->traitObject->loadPfawsYaml('staging', $path);

        $this->assertEquals("bar2", $results->getPfawsYamlEnv('aws_key'));

        $this->assertEquals("foo2", $results->getPfawsYamlEnv('aws_secret'));
    }

    /**
     * @coversDefaultClass getPfawsyaml
     */
    public function testGetPfawsyaml()
    {
        $path = __DIR__ . '/fixtures/pfaws.yaml';

        $results = $this->traitObject->loadPfawsYaml('staging', $path);

        $this->assertNotNull($results->getPfawsyaml());

        $this->assertNotNull($results);

        $this->assertEquals("bar2", $results->getPfawsYamlEnv('aws_key'));

        $this->assertEquals("foo2", $results->getPfawsYamlEnv('aws_secret'));
    }

    /**
     * @coversDefaultClass setS3
     */
    public function testSetS3() {
        $path = __DIR__ . '/fixtures/pfaws.yaml';
        $this->traitObject->loadPfawsYaml('staging', $path);
        $this->traitObject->setS3();

        $filesystem = \League\Flysystem\AwsS3v3\AwsS3Adapter::class;
        $this->assertInstanceOf($filesystem, $this->traitObject->getS3());
    }

    /**
     * @coversDefaultClass getPfawsYamlKeyValue
     */
    public function testGetPfawsYamlKeyValue() {
        $path = __DIR__ . '/fixtures/pfaws.yaml';
        $this->traitObject->loadPfawsYaml('staging', $path)->setS3();

        $app_name = $this->traitObject->getPfawsYamlKeyValue('app_name');
        $this->assertEquals('foo', $app_name);
    }
}

The annotations are for the coverage to know what I am testing.

This area puts the Trait in to an object using a method provided by PHPUnit:

/**
 * *Creation Method* to create an object for the trait under test.
 *
 * @return object The newly created object.
 */
private function createObjectForTrait()
{
    $traitName = '\PfAws\Traits\PfawsYaml';

    return $this->getObjectForTrait($traitName);
}

And then I run vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover and the coverage was boosted / recognized.

On a side note adding to my phpunit.xml

    <logging>
        <log
                type="coverage-html"
                target="./docs/coverage"
                charset="UTF-8"
                yui="true"
                lowUpperBound="35"
                highLowerBound="70"
                showUncoveredFiles="true"
        />

        <log
                type="coverage-text"
                target="php://stdout"
                lowUpperBound="35"
                highLowerBound="70"
        />

    </logging>

Gave me some really nice code coverage information