idrinth/phpunit-test-generator

Generates tests for use with PHPunit based on an existing composer.json

dev-move-to-php71 2018-11-25 11:23 UTC

This package is auto-updated.

Last update: 2020-09-18 11:46:01 UTC


README

Codacy Badge Codacy Badge Build Status CodeFactor BCH compliance

Command & Options

php bin/generate-tests --dir=/path/to/alternative/dir --mode=replace

dir

Dir is optional and will default to the current working directory. Pick the directory that the composer.json is in.

replace (deprecated, see mode)

If set old testfiles will be overwritten, if not set they will be renamed instead. Usually you shouldn't need this.

mode

One of replace, skip or move. Defines how to handle file conflicts and defaults to moving the old file.

output

If set will be prepended to the relative paths of dev-autoload paths.

How does it work?

The composer.json at the working directory(or the supplied dir) will pe parsed for namespaces(psr 0 and 4), the folders targeted by none-dev namespaces will be searched for classes and test classes will be generated in the appropriate development-autoloading-folder. To determine the testing class to be used, the phpunit dev-dependency is parsed, so you need to supply a constraint to make the generation work.

Generated Test - Example

This is a generated testfile for the actual Controller of this library. The intention is to generate PSR2-compatible code, obviously that might not yet be the case everywhere. If you find one where it fails, feel free to file a bug.

<?php

namespace De\Idrinth\TestGenerator\Test;

use PHPUnit\Framework\TestCase as TestCaseImplementation;
use De\Idrinth\TestGenerator\Controller;

/**
 * this is an automatically generated skeleton for testing Controller
 * @todo actually test
 **/
class ControllerTest extends TestCaseImplementation
{
    /**
     * @return Controller
     * @todo make sure the construction works as expected
     **/
    protected function getInstance()
    {
        return new Controller(
            $this->getMockBuilder('Symfony\Component\Finder\Finder')->getMock(),
            $this->getMockBuilder('De\Idrinth\TestGenerator\Interfaces\ClassReader')->getMock(),
            $this->getMockBuilder('De\Idrinth\TestGenerator\Interfaces\ClassWriter')->getMock(),
            $this->getMockBuilder('De\Idrinth\TestGenerator\Interfaces\Composer')->getMock(),
            null
        );
    }

    /**
     * From Controller
     * @test
     * @todo replace with actual tests
     **/
    public function testInit ()
    {
        $instance = $this->getInstance();
        $return = $instance->init();

        $this->assertInternalType(
            'object',
            $return,
            'Return didn\'t match expected type object'
        );

        $this->assertInstanceOf(
            'De\Idrinth\TestGenerator\Controller',
            $return,
            'Return didn\'t match expected instance De\Idrinth\TestGenerator\Controller'
        );

        $this->markTestIncomplete(
            'This test has not been implemented yet.'
        );
    }

    /**
     * From Controller
     * @test
     * @todo replace with actual tests
     **/
    public function testRun ()
    {
        $instance = $this->getInstance();
        $return = $instance->run();

        $this->assertInternalType(
            'null',
            $return,
            'Return didn\'t match expected type null'
        );

        $this->markTestIncomplete(
            'This test has not been implemented yet.'
        );
    }
}