also stuck at phpunit testing on page 103

Home Forums Learn ZF2 also stuck at phpunit testing on page 103

Tagged: 

This topic contains 3 replies, has 2 voices, and was last updated by  david@davidmintz.org 1 year, 9 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #808

    Hi

    I used ‘git checkout ch-unittest’ to get the source into a folder outside my project, and copied the relevant files into my own project folder (I am writing some stuff by hand and copying other stuff to follow along with the book).
    when I run:
    ZF2_PATH=$(pwd)/vendor/zendframework/zendframework/library php
    vendor/bin/phpunit -c module/User/tests/

    I get:
    PHP Fatal error: Class ‘UserTestFrameworkTestCase’ not found in /opt/www/learnzf2/module/User/tests/User/SampleTest.php on line 13

    so I figure autoloading is not happening for some reason. When I try cding into the module/User/tests folder and going:

    phpunit User/Controller/AccountControllerTest.php

    I then get:
    There was 1 error:

    1) UserTestControllerAccountControllerTest::testMeAction
    ZendServiceManagerExceptionServiceNotCreatedException: An exception was raised while creating “database”; no instance returned
    [stack trace omitted]
    /opt/www/learnzf2/module/User/tests/User/Controller/AccountControllerTest.php:20

    Caused by
    Undefined index: db

    so I figure there is something wrong with my configuration. Been playing with this for a while, and I am tempted to try another approach:

    http://framework.zend.com/manual/current/en/tutorials/unittesting.html

    I guess it bears mentioning that my controllers/actions are looking good when invoked through a web browser. I am on Ubuntu 14.04 and using the php built-in server, PHP 5.5.9, ZF 2.4.5

    Any thoughts?

    Thanks.

    • This topic was modified 1 year, 9 months ago by  david@davidmintz.org. Reason: add version information re PHP and ZF, fwiw
    #810

    Slavey Karadzhov
    Participant

    Hi David,
    > ZF2_PATH=$(pwd)/vendor/zendframework/zendframework/library php
    vendor/bin/phpunit -c module/User/tests/

    You have to change the current directory to be the directory where the ZF2 project resides, let’s say it is called “learnzf2”.

    cd </path/to/>/learnzf2

    Then you only need to execute
    vendor/bin/phpunit -c module/User/tests/

    To run the tests for the User module.

    > PHP Fatal error: Class ‘UserTestFrameworkTestCase’ not found in /opt/www/learnzf2/module/User/tests/User/SampleTest.php on line 13

    I downloaded the latest copy of the code, checked out ch-unittest, ran composer install and at least for me running the command, that I recommended above, did not gave fatal error.

    > so I figure autoloading is not happening for some reason.

    Very good guess. Check the module/User/autoload_classmap.php. If needed add the class that cannot be resolved and try again.

    > so I figure there is something wrong with my configuration. Been playing with this for a while, and I am tempted to try another approach:

    You are absolutely right and I would like to thank you for pointing this out. I will take care to change the source code and the book content to make things work out of the box. The issue was caused by the following:

    In the module/User/tests the database service is requested. This service will work IF there is a “db” key in the configuration of the application. And in your current setup the module/User/config/database.local.php.dist file is copied neither to config/autoload/ nor to module/User/tests/config/test/. To fix this issue you should copy the database.local.php.dist file to one of the above directories but with a name database.local.php (without the .dist extension).

    I will prepare a text that makes that part clear to understand and if you are interested I can give it to you for revision.

    Regards,
    Slavey

    #811

    ok thanks, I will go over all of the above carefully and try again, and let you know.

    In the meantime what I had done was (1) steal a different bootstrap file from http://framework.zend.com/manual/current/en/tutorials/unittesting.html; (2) edit phpunit.xml to use their Bootstrap.php (capital B, a conveniently slight difference from yours so they can coexist), and (3) just run a simple ‘phpunit <relative/path/to/a/Test.php> from within the module/User/tests folder — since there’s already a phpunit installation in my $PATH. In this way I was able to run AccountControllerTest.php successfully but SampleTest.php was still blowing up.

    #812

    ah, relief! green is beautiful.

    I copied the db config file as you suggested and resolved that issue, but was still getting “PHP Fatal error: Class ‘UserTestFrameworkTestCase’ not found in…” until I finally tracked that down to the fact that my classmap file was not getting loaded, which in turn was because my getAutoloaderConfig() in my User module’s Module.php was not working hard enough, i.e., returning an array that included

    ‘ZendLoaderClassMapAutoloader’ => array(
    __DIR__ . ‘/autoload_classmap.php’,
    ),

    I must have gotten out of sync with your code because I am making myself do some of the work by hand, and that’s error-prone. But the learning experience of screwing it up and then figuring out what I did wrong is valuable, no doubt.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.