Skelgen **AS** in phpunit-skelgen

In the quest to add a file to my project using TDD techinques properly I’ve created the tests way before creating the test subject.
I’m using codeception for all my testing and went on to create a PHPUnit test case for the FilePathResolver class (still not even existing)

$ codecept generate:phpunit unit FilePathResolver

and went on to write the tests using them to brainstorm and outline the class methods

<?php
use \TAD\MVC\helpers\FilePathResolver as Sut;
class FilePathResolverTest extends \PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        $this->baseFilePath = dirname(dirname(__FILE__)) . '/mvc_helpers';
        $this->mainFilePath = $this->baseFilePath . '/TestMain.php';
        $this->controllerFilePath = $this->baseFilePath . '/controllers/FirstController.php';
        $this->viewFilePath = $this->baseFilePath . '/views/FirstView.php';
        $this->scriptFilePath = $this->baseFilePath . '/scripts/FirstScript.js';
        $this->styleFilePaht = $this->baseFilePath . '/styles/FirstStyle.css';
        $this->minScriptFilePath = $this->baseFilePath . '/scripts/FirstScript.min.js';
        $this->minStyleFilePaht = $this->baseFilePath . '/styles/FirstStyle.min.css';
        $this->sut = new Sut($this->mainFilePath);
    }
    protected function tearDown()
    {
    }
    // tests
    public function testWillThrowForBadConstructorArguments()
    {
        $badArgs = array(
            5,
            'some string',
            '/absolute/path/to/fake/file.php',
            'relative/path/to/fake/file.php',
            array() ,
            array(
                'string'
            ) ,
            null
        );

        foreach ($badArgs as $badArg) {
            $this->setExpectedException('InvalidArgumentException', null, 1);
            new Sut($badArg);
        }
    }
    public function testWillThrowForNonMvcComponentFilePath()
    {
        $this->setExpectedException('InvalidArgumentException', null, 2);
        new Sut(__FILE__);
    }
    public function testGetMainFilePath()
    {
        $this->assertEquals($this->mainFilePath, $this->sut->getMainFilePath());
    }
    public function testGetControllerFilePathForElement()
    {
        $this->assertEquals($this->controllerFilePath, $this->sut->getControllerFilePathForElement('First'));
    }
    public function testGetViewFilePathForElement()
    {
        $this->assertEquals($this->viewFilePath, $this->sut->getViewFilePahtForElement('First'));
    }
    public function testGetScriptFilePathForMain()
    {
        $this->assertEquals($this->scriptFilePath, $this->sut->getScriptFilePahtForMain($min = false));
    }
    public function testGetMinScriptFilePathForMain()
    {
        $this->assertEquals($this->scriptFilePath, $this->sut->getScriptFilePahtForMain($min = true));
    }
    public function testGetScritpFilePathForElement()
    {
        $this->assertEquals($this->scriptFilePath, $this->sut->getScriptFilePahtForElement('First', $min = false));
    }
    public function testGetMinScritpFilePathForElement()
    {
        $this->assertEquals($this->scriptFilePath, $this->sut->getScriptFilePahtForElement('First', $min = true));
    }
    public function testGetStyleFilePathForMain()
    {
        $this->assertEquals($this->styleFilePath, $this->sut->getStyleFilePahtForMain($min = false));
    }
    public function testGetStyleFilePathForElement()
    {
        $this->assertEquals($this->styleFilePath, $this->sut->getStyleFilePahtForElement('First', $min = false));
    }
    public function testGetMinStyleFilePathForMain()
    {
        $this->assertEquals($this->styleFilePath, $this->sut->getStyleFilePahtForMain($min = true));
    }
    public function testGetMinStyleFilePathForElement()
    {
        $this->assertEquals($this->styleFilePath, $this->sut->getStyleFilePahtForElement('First', $min = true));
    }
}

Does not come with phpunit-skelgen

codeception does not come with phphunit-skelgen baked into it and composer will not offer any option. To pear then!

$ pear channel-discover components.ez.co
$ pear install phpunit/PHPUnit_SkeletonGenerator

and then the phpunit-skelgen will be available in the command line. As detailed in PHPUnit site I’m using here the skeleton generator to generate the class, FilePathResolver, from the test and not the other way round. How TDD.

$ cd tests/unit
$ phpunit-skelgen --class FilePathResolverTest

And this will lead to the generation of the following code

<?php
/**
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-01-08 at 08:34:42.
 */
class FilePathResolver
{}

Deluding. What did I get wrong?

Skelgen does not like “as” statements

Simply changing the use declaration of the FilePathResolverTest class from

use \TAD\MVC\helpers\FilePathResolver as Sut;

to

use \TAD\MVC\helpers\FilePathResolver;

made the magic happen and the generated code

<?php
/**
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-01-08 at 08:48:45.
 */
class FilePathResolver
{
    /**
     * @todo Implement getControllerFilePathForElement().
     */
    public function getControllerFilePathForElement()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }

    /**
     * @todo Implement getMainFilePath().
     */
    public function getMainFilePath()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }

    /**
     * @todo Implement getScriptFilePahtForElement().
     */
    public function getScriptFilePahtForElement()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }

    /**
     * @todo Implement getScriptFilePahtForMain().
     */
    public function getScriptFilePahtForMain()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }

    /**
     * @todo Implement getStyleFilePahtForElement().
     */
    public function getStyleFilePahtForElement()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }

    /**
     * @todo Implement getStyleFilePahtForMain().
     */
    public function getStyleFilePahtForMain()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }

    /**
     * @todo Implement getViewFilePahtForElement().
     */
    public function getViewFilePahtForElement()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }
}

which is way better a starting point.

2 thoughts on “Skelgen **AS** in phpunit-skelgen

  1. Thank you for the insight, I’m finding skelgen is still not quite ready for primetime.. at least I cannot figure it out. Just keep getting errors even after removing the ‘as’ entry in my testcase.

I appreciate your input