You are not logged in.
Building on the Introduction to Doctrine 1.2 video, this video will show how you can easily test the persistence of Doctrine models within the Zend_Test environment.
I also touch briefly on how to setup the latest version of MAMP with phpunit.
Enjoy!
Offline
You can show how setup UTF8 for another languages in Doctrine ?
I try replace in UserTest with:
$u->name = "Jon čertův kopeček";
And in database (phpmyadmin) have mishmash characters.
SOLVED by change in bootstrap.php
$conn = Doctrine_Manager::connection($doctrine['dsn'], 'doctrine');
$conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
+ $conn->setCharset('utf8');
I try setCharset to manager, isn't work. This solution works!
Thanks for your tutorial, i'm waiting for next episode.
Last edited by abtris (2009-12-02 14:40:23)
Offline
thanks for mentioning this abtris!
Offline
this is more related to phpunit actually.
what does
<log type="coverage-html" target="./log/report" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="35" highLowerBound="70"/>
<log type="testdox-html" target="/log/testdox.html"/>mean? isit ... :
* type="coverage-html" -> i want a report in html
* yui="true" -> whats this? using yahoo UI?
* highlight="true" -> whats this?
* lowUpperBound="35" highLowerBound="70" -> and these?
* <log type="testdox-html" target="/log/testdox.html"/> -> a 2nd report in testdox-html? whats this abt?
when i try to run phpunit i get
D:\Projects\ZF\doctrine\tests>phpunit
create-db - Successfully created database for connection named 'Doctrine'
PHPUnit 3.4.3 by Sebastian Bergmann.
PHP Warning: fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEA
R\PHPUnit\Util\Printer.php on line 107
Warning: fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEAR\PHP
Unit\Util\Printer.php on line 107
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163
Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163
Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163
Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 123
Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 123
Time: 1 second
←[30;42m←[2KOK (0 tests, 0 assertions)
←[0m←[2K
D:\Projects\ZF\doctrine\tests>seems like i dont have some pear library installed?
when i try to run phpunit i get
D:\Projects\ZF\doctrine\tests>phpunit create-db - Successfully created database for connection named 'Doctrine' PHPUnit 3.4.3 by Sebastian Bergmann. PHP Warning: fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEA R\PHPUnit\Util\Printer.php on line 107 Warning: fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEAR\PHP Unit\Util\Printer.php on line 107 PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P rinter.php on line 163 Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe r.php on line 163 PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P rinter.php on line 163 Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe r.php on line 163 PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P rinter.php on line 163 Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe r.php on line 163 PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P rinter.php on line 123 Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe r.php on line 123 Time: 1 second ←[30;42m←[2KOK (0 tests, 0 assertions) ←[0m←[2K D:\Projects\ZF\doctrine\tests>seems like i dont have some pear library installed?
i got it working already ... its because i copied code from the phpunit manual ...
<log type="testdox-html" target="/log/testdox.html"/>
vs
<log type="testdox" target="/log/testdox.html"/>
but when do i use various options eg. testdox vs testdox-html vs testdox-txt etc etc. the manual did not specify whats what
Hi, I tried to follow the tutorial but I allways get a
Cannot redeclare class PHPUnit_Util_FilterIterator in /path/to/library/PHPUnit/Util/FilterIterator.php on line 163
I use Zend Framework 1.9 and PHPUnit 3.4.4 could you give me a tip what I doing wrong?
best regards
aruss
Hello,
I have followed your tutorial. Preveiously I have created my own form validator in the Zend Framework which was working correctly. But now by using phpunit i get the following error:
Time: 0 seconds, Memory: 4.50Mb
OK (0 tests, 0 assertions)
Generating code coverage report, this may take a moment.
Fatal error: Class 'Zend_Validate_Abstract' not found in /var/www/nrka/library/JS/Validate/CompareDates.php on line 2
Call Stack:
0.0004 63840 1. {main}() /usr/bin/phpunit:0
0.2052 4486164 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:52
0.2053 4486896 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:147
0.2082 4493296 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:214
0.3762 4668380 5. PHPUnit_Util_Report::render() /usr/share/php/PHPUnit/TextUI/TestRunner.php:479
0.3763 4669256 6. PHPUnit_Framework_TestResult->getCodeCoverageInformation() /usr/share/php/PHPUnit/Util/Report.php:98
0.3763 4669368 7. PHPUnit_Util_Filter::getFilteredCodeCoverage() /usr/share/php/PHPUnit/Framework/TestResult.php:624
0.6731 5975076 8. include_once('/var/www/nrka/library/JS/Validate/CompareDates.php') /usr/share/php/PHPUnit/Util/Filter.php:382
Any idea's how to solve this?
I've been watching your vids the last couple of days. My compliments, they contain just enough detail for me.
I was just wondering, I'm going to use your setup for my new project. But should it be easy to convert this setup to the definitive setup from Zend+Doctrine once they release it in Q1 2010?
Thanks!
Offline
Hi, great casts, very helpful!!!
I just wanted to let you now that I managed to speed up the tests in my application, by more than a minute by deleting the lines that exclude the zend library and the .phtml files in the filter in the phpunit.xml file, I don't think they are necessary because you exclude them with the main filter.
Hope this helps
Hi, in this example the database is recreated at the begining of the tests, shouldn't it be recreated for each test? so that tests aren't affected by previous ones.
thanks
Hey Sebastian,
you can certainly do it in that manner, however as you start writing hundreds of tests, you might find nuking your db every time to slow you down. To do it though, you could just modify your startUp and tearDown methods for the model tests.
Offline
Thank you for your reply.
And how about using doctrine mock drivers or sqlite memory database? to solve that problem.
Thanks!
Hey,
When I run the tests, I get the following error:
PHPUnit 3.4.8 by Sebastian Bergmann.
.
Time: 46 seconds, Memory: 32.00Mb
OK (1 test, 6 assertions)
Generating code coverage report, this may take a moment.
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in /usr/share/php/PHPUnit/Util/Report/Node/File.php on line 644
Call Stack:
0.0006 62840 1. {main}() /usr/bin/phpunit:0
0.1008 4498284 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:54
0.1008 4499016 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:146
42.4558 21921616 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:213
46.2234 30628640 5. PHPUnit_Util_Report::render() /usr/share/php/PHPUnit/TextUI/TestRunner.php:478
46.2948 31323220 6. PHPUnit_Util_Report::addItems() /usr/share/php/PHPUnit/Util/Report.php:118
46.2986 31358240 7. PHPUnit_Util_Report::addItems() /usr/share/php/PHPUnit/Util/Report.php:154
46.3014 31385764 8. PHPUnit_Util_Report::addItems() /usr/share/php/PHPUnit/Util/Report.php:154
46.5328 33319972 9. PHPUnit_Util_Report_Node_Directory->addFile() /usr/share/php/PHPUnit/Util/Report.php:146
46.5328 33321944 10. PHPUnit_Util_Report_Node_File->__construct() /usr/share/php/PHPUnit/Util/Report/Node/Directory.php:147
46.5329 33322732 11. PHPUnit_Util_Report_Node_File->loadFile() /usr/share/php/PHPUnit/Util/Report/Node/File.php:168
46.5332 33365436 12. token_get_all() /usr/share/php/PHPUnit/Util/Report/Node/File.php:644
Any idea??
Offline
hey boodito, if its a memory issue, try bumping up your limit in the php.ini file!
Offline
in one of your first videos about zf and doctrine you had a models folder and then the models files and a Base directory with the generated classes for the models.
in the application.ini you defined some settings:
doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = false
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "Model_"If I try to integrate phpunit testing with the setup explained in previous tutorials and the one you provide in this video
example:
class ArticleTest extends Zend_Test_PHPUnit_ControllerTestCase {
.
.
.
public function testFindAll() {
$article = new Model_Article();
$obj = $article->findAll($this->lang);
$this->assertTrue( count($obj) > 0);
}I get this error:
PHPUnit 3.4.9 by Sebastian Bergmann.
PHP Fatal error: Class 'Model_Article' not found in C:\server\www\zend_site\t
ests\application\models\ArticleTest.php on line 23What am I doing wrong?
Offline
hey Yusuf,
I've read several reports a silly bug where models that start with letters before "D" aren't loaded... I'm not sure if that's your issue though...
Offline
you're joking wright? and if not, do you know if there are ways to fix this besides renaming the models? tnx for ur time on this and your excellent screencasts ![]()
It seems like this is the issue, I've created another table, generated the model for it and tested it successfully.
by the way, the new class name is: 'Project'
Last edited by yusufdestina (2010-04-16 21:57:22)
Offline
ridiculous indeed...
Offline
Hi.
I'm trying to integrate ZF and Doctrine in a testable way, and I have some trouble which comes back to what Sebastian Montero wrote. What I fail to see is a way of bootstrapping my test suite so it could be used for testing both controllers and models.
In this screencast Zend_Application is instantiated in /tests/application/bootstrap.php so the CLI code runs only once. Yet in ZC-25 which covers controller testing it's instantiated in setUp method of a ControllerTestCase class.
The problem is that if you create an instance of Zend_Application in bootstrap.php and then you try to test your controllers it's all messed up. Though everything works fine in browser, running $this->dispatch('/uri') ends up with "No default module" error, and when you fix that it cannot find your controller plugins and then it has trouble finding layout resource and so on and so forth. So you basically have to bootstrap your application twice which is no good.
On the other hand, you can move everything into your setUp method. But then you'll have 'build-all-reload' CLI command running for every single test method which also is a bad thing.
What I do is I actually run 'build-all-reload' before instantiating the application:
<?php
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
define('APPLICATION_ENV', 'testing');
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/models'),
get_include_path(),
)));
require_once 'Zend/Application.php';
require_once 'Zend/Config/Ini.php';
require_once 'Doctrine.php';
require_once 'ControllerTestCase.php';
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);
$dbConfig = $config->db->toArray();
$doctrineConfig = $config->doctrine->toArray();
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_AGGRESSIVE);
$manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
$manager->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true);
$dsn = sprintf("mysql://%s:%s@%s/%s",
$dbConfig['username'],
$dbConfig['password'],
$dbConfig['host'],
$dbConfig['dbname']
);
$connection = $manager->connection($dsn);
$connection->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
$cli = new Doctrine_Cli($doctrineConfig);
@$cli->run(array('Doctrine', 'build-all-reload', 'force'));And then I create an instance of Zend_Application normally:
<?php
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $application = NULL;
public function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->application->bootstrap();
}
}I wonder what you guys think about it and if there's a better way doing this.
Offline
Hello,
This source code doesn't work.
When I set up a new Zend Framework project from these sources and run PHPUnit test with Zend Studio, I get several errors :
First, the Zend_Test_PHPUnit_ControllerTestCase class is not known : “Fatal error: Class ‘Zend_Test_PHPUnit_ControllerTestCase’ not found in D:\…\tests\application\models\UserTest.php”.
Ok, I add a require_once ‘Zend/Test/PHPUnit/ControllerTestCase.php’; in my UserTest.php file.
So, I no longer have this error, but a new error for the model on $u = new User(); : “Class ‘User’ not found in D:\…\tests\application\models\UserTest.php”.
I can still not add a require_once for each class of my project.
The bootstrap does not even use.
Do you have any idea of the origin of this problem ?
Is this related to the use of Zend Studio ?
Is there anything else to add or modify to run this project ?
Thank you in advance for your help.
Best regards.
Stéphan.
Last edited by StefC30 (2010-11-11 09:36:34)
Offline
Sorry, I never worked with Zend Studio. But technically there are all the neccesary require_once statements if you of course put your classes in right places. Have you tried running your tests from the command line?
Offline
No, just from Zend Studio.
Offline
I think I found out what's wrong with my source code. Everything works fine if your testcases extend ControllerTestCase class. But if you try to extend PHPUnit_Framework_TestCase it ends up crushing since you've got no Zend_Application instantiated and therefore no autoloader initialized (apart from the Doctrine's one). Thanks for pointing me at a bug like this.
Actually there's a solution shown in the last of Zend Casts (which deals with creating two instances of Zend_Application). Gotta stick to that until there is a better way doing it.
Offline
@Wallgate
a better solution for this is to create a loader class where u do the
Zend_Application bootstrapping.
Then u can create a testhelper class for unittests witch extends PHPUnit_Framework_TestCase and another testhelper
for controllertests witch extends Zend_Test_PHPUnit_ControllerTestCase.
In your setUp method just use something like this.
$loader = new Loader();
$this->application = $loader->loadApplication();
$this->bootstrap = $loader->getBootstrap();Best regards
Christian
Offline