Question

I seem to have a slight problem with executing PHPUnit tests for own modules in the Zend Framework 2.

OS: Mac OS 10.8.3
Zend Framework 2.1.4
PHPUnit Version 3.7.19 (installed via pear)
PHP Version 5.3.15 (xdebug enabled, version 2.1.3)

I followed the instructions of the Zend guideline, to create the module "Album" (http://framework.zend.com/manual/2.1/en/user-guide/modules.html)

Instead of putting the unit tests into the module folders, I want to have them all in one centralized folder. This is the base structure of my application:

-config
-data
-module
-public
-tests
--Modules
---Album
----AlbumTest.php
--Bootstrap.php
--phpunit.xml
-vendor
...(license, readme, composers and init_autoloader.php)

-> The test for the Module Album resides in a folder "Modules/Album" in "tests". The folder "tests" contains also the Bootstrap.php and phpunit.xml.

Here is the content of the files:

Bootstrap.php

<?php

/*
 * Set error reporting to the level to which Zend Framework code must comply.
 */
error_reporting( E_ALL | E_STRICT );

/*
 * The first run required me to fix some constants
 */
defined('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY', 'public key');
defined('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY', 'private key');
defined('TESTS_ZEND_LDAP_PRINCIPAL_NAME') || define('TESTS_ZEND_LDAP_PRINCIPAL_NAME', 'someUser@example.com');
defined('TESTS_ZEND_LDAP_ALT_USERNAME') || define('TESTS_ZEND_LDAP_ALT_USERNAME', 'anotherUser');

chdir(dirname(__DIR__));

/*
 * autoload the application
 */
include __DIR__ . '/../init_autoloader.php';

Zend\Mvc\Application::init(include 'config/test.config.php');

phpunit.xml

<phpunit bootstrap="./Bootstrap.php" colors="true">
    <testsuites>
        <testsuite name="Zend Module Tests">
            <directory>./Modules</directory>
        </testsuite>
    </testsuites>

</phpunit>

AlbumTest.php

<?php

namespace AlbumTest\Model;

use Album\Model\Album;
use PHPUnit_Framework_TestCase;

/**
 * @category   Module
 * @package    Album
 * @subpackage UnitTests
 * @group      Module_Album
 */
class AlbumTest extends PHPUnit_Framework_TestCase {

    public function testAlbumInitialState() {
        $album = new Album();

        $this->assertNull($album->artist, '"artist" should initially be null');
        $this->assertNull($album->id, '"id" should initially be null');
        $this->assertNull($album->title, '"title" should initially be null');
    }

    public function testExchangeArraySetsPropertiesCorrectly() {
        $album = new Album();
        $data = array('artist' => 'some artist',
            'id' => 123,
            'title' => 'some title');

        $album->exchangeArray($data);

        $this->assertSame($data['artist'], $album->artist, '"artist" was not set correctly');
        $this->assertSame($data['id'], $album->id, '"id" was not set correctly');
        $this->assertSame($data['title'], $album->title, '"title" was not set correctly');
    }

    public function testExchangeArraySetsPropertiesToNullIfKeysAreNotPresent() {
        $album = new Album();

        $album->exchangeArray(array('artist' => 'some artist',
            'id' => 123,
            'title' => 'some title'));
        $album->exchangeArray(array());

        $this->assertNull($album->artist, '"artist" should have defaulted to null');
        $this->assertNull($album->id, '"id" should have defaulted to null');
        $this->assertNull($album->title, '"title" should have defaulted to null');
    }

    public function testGetArrayCopyReturnsAnArrayWithPropertyValues() {
        $album = new Album();
        $data = array('artist' => 'some artist',
            'id' => 123,
            'title' => 'some title');

        $album->exchangeArray($data);
        $copyArray = $album->getArrayCopy();

        $this->assertSame($data['artist'], $copyArray['artist'], '"artist" was not set correctly');
        $this->assertSame($data['id'], $copyArray['id'], '"id" was not set correctly');
        $this->assertSame($data['title'], $copyArray['title'], '"title" was not set correctly');
    }

    public function testInputFiltersAreSetCorrectly() {
        $album = new Album();

        $inputFilter = $album->getInputFilter();

        $this->assertSame(3, $inputFilter->count());
        $this->assertTrue($inputFilter->has('artist'));
        $this->assertTrue($inputFilter->has('id'));
        $this->assertTrue($inputFilter->has('title'));
    }

}

NetBeans knows, where to find the phpunit binaries: (I wanted to post images here, don't have the reputation though :), I will try to explain it) The paths to are configured in the options - php - unit testing
/usr/local/pear/bin/phpunit
and /usr/local/pear/bin/phpunit-skelgen

In the properties of the project I set the "Use XML Configuration" and pasted the path to the phpunit.xml. I also checked "Ask for Test Groups Before Running Tests" - I gave the test above the group "Module_Album" - like this I make sure, that PHPUnit finds the right test.

I right click on Project and choose "Test", it shows me the group "Module_Album", I check it and click "Ok". It runs something, tells me it located the right phpunit.xml ("Configuration read from FULL_PATH/tests/phpunit.xml"). After running, it tells me, that it didn't execute any tests.

This is the full output in NetBeans:

PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from FULL_PATH/tests/phpunit.xml



Time: 9 seconds, Memory: 348.25Mb

[2KNo tests executed!
[2K
Generating code coverage report in Clover XML format ... done

Anyway, I can do the same successfully via shell (terminal). It doesn't matter, if I directly mount the tests directory and run phpunit, use the full path to the phpunit binary (to make sure, that I don't use different version), specifying the full path of the phpunit.xml, and so on. Here are some samples:

phpunit
phpunit -c FULL_PATH/tests/phpunit.xml
/usr/local/pear/bin/phpunit -c FULL_PATH/tests/phpunit.xml

All of these commands give me, what I expect:

PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from FULL_PATH/tests/phpunit.xml

.....

Time: 0 seconds, Memory: 12.75Mb

OK (5 tests, 16 assertions)
  • I don't get, why this works in shell but not via NetBeans
  • NetBeans uses 350MB, while in shell it uses only 12.75MB
  • In case I remove the option "Ask for Test Groups Before Running Tests", it seems to try running all ZendTests. Don't know how this is possible, from the phpunit.xml it should not find them.

Any help is appreciated, thanks!

Was it helpful?

Solution

I finally figured out, what is going on. I ran previously the ZendTests with the phpunit.xml, which is inside vendor/zendframework/zendframework/tests
For some reason NetBeans saves the testfile-directory, which it finds in the first test run and doesn't release them, if the a different phpunit.xml is chosen. I had to change the path of my "new" tests directory via "Right click on project" - "Properties" - "Sources" - "Test Folder".

Now NetBeans runs the test and gives exactly the same output like CLI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top