Chapter 4, Page 55, phpunit command

Chapter 4, Page 55, phpunit command

When I run:

phpunit unit/DbTest.php

I get this error:


'phpunit' is not recognized as an internal or external command, 

operable program or batch file.

Like the book states, I am located at: /WebRoot/trackstar/protected/tests

And I’ve created the DbTest.php file with this code:


<?php

class DbTest extends CTestCase

{

public function testConnection()

{

$this->assertTrue(true);

}

}

The only file in this directory resembling the ‘phpunit’ command is a phphunit.xml. What executable is this supposed to call?

Looks like I need phpunit installed, but to do that I need Pear installed, but am failing at that:


C:\Program Files\EasyPHP-5.3.3\php>go-pear.bat

phar "C:\Program Files\EasyPHP-5.3.3\php\PEAR\go-pear.phar" does not have a signature

Warning: require_once(phar://go-pear.phar/index.php): failed to open stream: phar error: invalid url or non-existent phar "phar://go-pear.phar/index.php" in C:\Program Files\EasyPHP-5.3.3\php\PEAR\go-pear.phar on line 1236

Press any key to continue . . . 

hello toolman,

in order to run your testcase u need phpunit installed - covered somewhere in chapter 3 of the book.

check http://www.phpunit.de/manual/current/en/installation.html for further infos.

using the recommended pear installer (pear.php.net) u would succeed with entering the following into your console:

pear channel-discover pear.phpunit.de

pear channel-discover components.ez.no

pear channel-discover pear.symfony-project.com

pear install phpunit/PHPUnit

if u already installed phpunit try upgrading it since there was a related bug prior to version 3.4.

pear channel-update

pear install phpunit/PHPunit

… would do the trick.

if u cant resolve your issue report back and post the operating system your using

bye

Thanks beatmox, but I’m still a couple steps in the hole yet, trying to debug why Pear won’t install.

I tried this, but it just echoed back the contents of the bat file.


C:\Program Files\EasyPHP-5.3.3\php>php go-pear.bat

@ECHO OFF

set PHP_BIN=php.exe

%PHP_BIN% -d output_buffering=0 PEAR\go-pear.phar

pause

PS. I’m on Windows 7.

I’m trying to read this but it’s not helping:

http://pear.php.net/manual/en/installation.getting.php

hey,

open your php.ini and locate the phar.require_hash entry. make sure its set to

phar.require_hash = Off and dont forget and remove the semicolon to uncomment the line.

run ur go-pear.bat again

:wink:

More hacks, awesome!

All right, I installed pear, it seems.

So this is what I got when trying to install phpunit.

But the console still doesn’t recognize the phpunit command.




C:\Program Files\EasyPHP-5.3.3\php>pear channel-discover pear.phpunit.de

Adding Channel "pear.phpunit.de" succeeded

Discovery of channel "pear.phpunit.de" succeeded


C:\Program Files\EasyPHP-5.3.3\php>pear channel-discover components.ez.no

Adding Channel "components.ez.no" succeeded

Discovery of channel "components.ez.no" succeeded


C:\Program Files\EasyPHP-5.3.3\php>pear channel-discover pear.symfony-project.com

Adding Channel "pear.symfony-project.com" succeeded

Discovery of channel "pear.symfony-project.com" succeeded


C:\Program Files\EasyPHP-5.3.3\php>pear install phpunit/PHPUnit

phpunit/PHPUnit requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHPUnit can optionally use PHP extension "dbus"

phpunit/PHPUnit can optionally use PHP extension "soap"

phpunit/DbUnit requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/File_Iterator requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/Text_Template requires PEAR Installer (version >= 1.8.1), installed version is 1.8.0

phpunit/PHP_CodeCoverage requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHP_CodeCoverage requires package "phpunit/File_Iterator" (version >= 1.2.2)

phpunit/PHP_CodeCoverage requires package "phpunit/Text_Template" (version >= 1.0.0)

phpunit/PHP_CodeCoverage can optionally use PHP extension "xdebug" (version >= 2.0.5)

phpunit/PHP_Timer requires PEAR Installer (version >= 1.9.0), installed version is 1.8.0

phpunit/PHPUnit_MockObject requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHPUnit_MockObject requires package "phpunit/Text_Template" (version >= 1.0.0)

phpunit/PHPUnit_Selenium requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHP_TokenStream requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

downloading YAML-1.0.3.tgz ...

Starting to download YAML-1.0.3.tgz (9,794 bytes)

.....done: 9,794 bytes

downloading ConsoleTools-1.6.1.tgz ...

Starting to download ConsoleTools-1.6.1.tgz (869,994 bytes)

...done: 869,994 bytes

downloading Base-1.8.tgz ...

Starting to download Base-1.8.tgz (236,357 bytes)

...done: 236,357 bytes

install ok: channel://pear.symfony-project.com/YAML-1.0.3

install ok: channel://components.ez.no/Base-1.8

install ok: channel://components.ez.no/ConsoleTools-1.6.1



we are headin the right way it seems xD

but installing phpunit didnt succeed as your console output suggests:

u need to update your pear installation: hit ur console with pear install PEAR-1.9.1 and run pear install phpunit/PHPunit again

No go,

and of course they won’t tell you what the PHP_PEAR_PHP_BIN variable should be set to.





C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests>pear channel-update

PHP_PEAR_PHP_BIN is not set correctly.

Please fix it using your environment variable or modify

the default value in pear.bat

The current value is:

.\php.exe

Find where is located php.exe and set PHP_PEAR_PHP_BIN to it.

for example if php.exe is in c:\php5\bin\php.exe

set PHP_PEAR_PHP_BIN=c:\php5\bin\php.exe

hey mdomba,

My question is now, "How do I update the PEAR Installer?"

Here is my Current Error Roadblock:


C:\Program Files\EasyPHP-5.3.3\php>pear channel-update

No channel file specified


C:\Program Files\EasyPHP-5.3.3\php>pear update-channels

Updating channel "components.ez.no"

Channel "components.ez.no" is up to date

Updating channel "doc.php.net"

Channel "doc.php.net" is up to date

Updating channel "pear.php.net"

Channel "pear.php.net" is up to date

Updating channel "pear.phpunit.de"

Channel "pear.phpunit.de" is up to date

Updating channel "pear.symfony-project.com"

Channel "pear.symfony-project.com" is up to date

Updating channel "pecl.php.net"

Channel "pecl.php.net" is up to date


C:\Program Files\EasyPHP-5.3.3\php>pear install phpunit/PHPUnit

phpunit/PHPUnit requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHPUnit can optionally use PHP extension "dbus"

phpunit/PHPUnit can optionally use PHP extension "soap"

phpunit/DbUnit requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/File_Iterator requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/Text_Template requires PEAR Installer (version >= 1.8.1), installed version is 1.8.0

phpunit/PHP_CodeCoverage requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHP_CodeCoverage requires package "phpunit/File_Iterator" (version >= 1.2.2)

phpunit/PHP_CodeCoverage requires package "phpunit/Text_Template" (version >= 1.0.0)

phpunit/PHP_CodeCoverage can optionally use PHP extension "xdebug" (version >= 2.0.5)

phpunit/PHP_Timer requires PEAR Installer (version >= 1.9.0), installed version is 1.8.0

phpunit/PHPUnit_MockObject requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHPUnit_MockObject requires package "phpunit/Text_Template" (version >= 1.0.0)

phpunit/PHPUnit_Selenium requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

phpunit/PHP_TokenStream requires PEAR Installer (version >= 1.9.1), installed version is 1.8.0

No valid packages found

install failed

My Current Settings:

manual change in pear.bat file:


IF "%PHP_PEAR_PHP_BIN%"=="" SET "PHP_PEAR_PHP_BIN=C:\Program Files\EasyPHP-5.3.3\php\php.exe"

set environment variable:


C:\Program Files\EasyPHP-5.3.3\php>set php_pear_php_bin

PHP_PEAR_PHP_BIN=C:\Program Files\EasyPHP-5.3.3\php\pear\php.exe

My Environmental Variables:




C:\Program Files\EasyPHP-5.3.3\php>set

ALLUSERSPROFILE=C:\ProgramData

CommonProgramFiles=C:\Program Files\Common Files

ComSpec=C:\windows\system32\cmd.exe

configsetroot=C:\windows\ConfigSetRoot

FP_NO_HOST_CHECK=NO

HOMEDRIVE=C:

NUMBER_OF_PROCESSORS=4

OS=Windows_NT

Path=C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\EasyPHP-5.3.3\php;C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

PHP_BIN=php.exe

PHP_PEAR_BIN_DIR=C:\Program Files\EasyPHP-5.3.3\php

PHP_PEAR_INSTALL_DIR=C:\Program Files\EasyPHP-5.3.3\php\pear

PHP_PEAR_PHP_BIN=C:\Program Files\EasyPHP-5.3.3\php\php.exe

PMTPMTPMT=@include_path@

PROCESSOR_ARCHITECTURE=x86

PROCESSOR_IDENTIFIER=x86 Family 6 Model 37 Stepping 2, GenuineIntel

PROCESSOR_LEVEL=6

PROCESSOR_REVISION=2502

ProgramData=C:\ProgramData

ProgramFiles=C:\Program Files

PROMPT=$P$G

PSModulePath=C:\windows\system32\WindowsPowerShell\v1.0\Modules\

PUBLIC=C:\Users\Public

SESSIONNAME=Console

SystemDrive=C:

SystemRoot=C:\windows

TMPTMPTMPTMPT=@includ

windir=C:\windows



pear upgrade pear

Ok, that worked, I’m able to trudge ahead another page in the book.

But I’m still stuck in Chapter 4.

I ran the command:

phpunit unit/DbTest.php

and it passed with the line: "$this->assertTrue(true);"

but after changing the line to: " $this->assertNotEquals(NULL, Yii::app()->db);"

I can’t get the test to pass.

And my DB credentials are absolutely correct. (I’ve renamed them for posting here as (A,B,C,D)


<?php

class DbTest extends CTestCase

{

    public function testConnection()

    {

       // $this->assertTrue(true);

       $this->assertNotEquals(NULL, Yii::app()->db);

    }

}


       /*

		'db'=>array(

			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

		),

        */

        

        




'db'=>array(

'connectionString' => 'mysql:host=A;dbname=B',

'emulatePrepare' => true,

'username' => 'C',

'password' => 'D',

'charset' => 'utf8',

),






C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests>phpunit unit/DbTest.php

PHP Notice:  Please no longer include "PHPUnit/Framework.php". in C:\Program Files\EasyPHP-5.3.3\php\PEAR\PHPUnit\Framework.php on line 50


Notice: Please no longer include "PHPUnit/Framework.php". in C:\Program Files\EasyPHP-5.3.3\php\PEAR\PHPUnit\Framework.php on line 50

PHPUnit 3.5.0 by Sebastian Bergmann.


E


Time: 0 seconds, Memory: 4.75Mb


There was 1 error:


1) DbTest::testConnection

CDbException: CDbConnection failed to open the DB connection.


C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:275

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:242

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:221

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\base\CModule.php:363

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\base\CModule.php:86

C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests\unit\DbTest.php:7


FAILURES!

Tests: 1, Assertions: 0, Errors: 1.



I’ve tried to connect to a local dev mysql server.

I can connect fine to the db server using the Sqlyog db administration tool.

I can connect fine with phpMyAdmin at http://127.0.0.1/home/mysql/

I created the database and a table fine.

I’ve also tried using host=localhost and no go.




'db'=>array(

'connectionString' => 'mysql:host=127.0.0.1;dbname=trackstar_dev',

'emulatePrepare' => true,

'username' => 'root',

'password' => '',

'charset' => 'utf8',

),

But I’m still getting this error:





C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests>phpunit unit/DbTest.php

PHP Notice:  Please no longer include "PHPUnit/Framework.php". in C:\Program Files\EasyPHP-5.3.3\php\PEAR\PHPUnit\Framework.php on line 50


Notice: Please no longer include "PHPUnit/Framework.php". in C:\Program Files\EasyPHP-5.3.3\php\PEAR\PHPUnit\Framework.php on line 50

PHPUnit 3.5.0 by Sebastian Bergmann.


E


Time: 0 seconds, Memory: 4.75Mb


There was 1 error:


1) DbTest::testConnection

CDbException: CDbConnection failed to open the DB connection.


C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:275

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:242

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:221

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\base\CModule.php:363

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\base\CModule.php:86

C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests\unit\DbTest.php:7


FAILURES!

Tests: 1, Assertions: 0, Errors: 1.




hey toolman,

i assume that you are overwriting the db application component with insufficient parameters. this will hopefully solve your issue:

unit tests deploy the bootstrap.php located in webroot\trackstar\protected\tests\.




webroot/trackstar/protected/tests/bootstrap.php

<?php


// change the following paths if necessary

$yiit=dirname(__FILE__).'/../../../framework/yiit.php';

$config=dirname(__FILE__).'/../config/test.php';           //<- DIFFERENT CONFIG


require_once($yiit);

require_once(dirname(__FILE__).'/WebTestCase.php');


Yii::createWebApplication($config);



you notice that not the usual ‘/../config/main.php’ config is specified but test.php.

test.php holds specific configurations (for example a different db application component) for the unittesting, and


CMap::mergeArray()

will merge this with ‘/../config/main.php’, overwriting or adding new parameters.

long story short -> have a look @ your ‘/../config/test.php’:




webroot/trackstar/protected/config/test.php

<?php


return CMap::mergeArray(

    require(dirname(__FILE__) . '/main.php'),

    array(

        'components' => array(

            'fixture' => array(

                'class' => 'system.test.CDbFixtureManager',

            ),


/*            'db' => array(

                'connectionString' => 'mysql:host=localhost;dbname=trackstar_test',

                'emulatePrepare' => true,

                'username' => 'root',

                'password' => 'yourpasswordhere',

                'charset'  => 'utf8'

            ),*/

        ),

    )

);



and make sure you comment the db component or supply it with valid information for your database.

let us know ;)

p.s.:chapter 5 "Specifying a test database" covers the motivations why specifying a different db for the testing framework comes in handy.

Thanks for trying to help

My default test.php looks like this:




<?php


return CMap::mergeArray(

	require(dirname(__FILE__).'/main.php'),

	array(

		'components'=>array(

			'fixture'=>array(

				'class'=>'system.test.CDbFixtureManager',

			),

			/* uncomment the following to provide test database connection

			'db'=>array(

				'connectionString'=>'DSN for test database',

			),

			*/

		),

	)

);



I changed it to this, but it didn’t help:





<?php


return CMap::mergeArray(

	require(dirname(__FILE__).'/main.php'),

	array(

		'components'=>array(

			'fixture'=>array(

				'class'=>'system.test.CDbFixtureManager',

			),

            

            

            

            

'db'=>array(

'connectionString' => 'mysql:host=localhost;dbname=trackstar_dev;port=3306',

'emulatePrepare' => true,

'username' => 'root',

'password' => '',

'charset' => 'utf8',

),

                                                                        

			/* uncomment the following to provide test database connection

			'db'=>array(

				'connectionString'=>'DSN for test database',

			),

			*/

		),

	)

);




I get the same error:





C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests>phpunit unit/DbTest.php

PHP Notice:  Please no longer include "PHPUnit/Framework.php". in C:\Program Files\EasyPHP-5.3.3\php\PEAR\PHPUnit\Framework.php on line 50


Notice: Please no longer include "PHPUnit/Framework.php". in C:\Program Files\EasyPHP-5.3.3\php\PEAR\PHPUnit\Framework.php on line 50

PHPUnit 3.5.0 by Sebastian Bergmann.


E


Time: 1 second, Memory: 4.75Mb


There was 1 error:


1) DbTest::testConnection

CDbException: CDbConnection failed to open the DB connection.


C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:275

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:242

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\db\CDbConnection.php:221

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\base\CModule.php:363

C:\Program Files\EasyPHP-5.3.3\www\yii-1.1.4.r2429\framework\base\CModule.php:86

C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests\unit\DbTest.php:7


FAILURES!

Tests: 1, Assertions: 0, Errors: 1.



Plus I know it’s reading the db key in main.php because if I force a syntax error I get this:




C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\tests>phpunit unit/DbTest.php

PHP Parse error:  syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\config\main.php on line 59


Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in C:\Program Files\EasyPHP-5.3.3\www\trackstar\protected\config\main.php on line 59




If it’s reading the configuration… than it must be something with the data you are providing…

check and double check the database name, the port…

could be that login without password is prevented… try to set a password…

if nothing else is working… try to make a PHP script that access the database the old way (mysql_connect()) to see if you get any error that could tell you what is wrong…

guys im having the same issue. CDbConnection failed to open db connection.

im using XAMPP on mac lion

  • created the trackstar_dev database using phpmyadmin()

  • i already configure my /protected/config/main.php

  • i can connect to mysql using just php mysql_connect() to check if mysql is working properly

is there something i missed?