PHPUnit Integration with CakePHP 1.3

I was looking for a tutorial that will help me integrate PHPUnit with CakePHP. When looking at using Selenium tests, you also prefer PHPUnit.

I tried to follow the manual of http://cakebaker.42dh.com/2006/03/22/selenium/ , but it does not seem to work. Any good tutorials?

Thanks!

+6
source share
2 answers

Unfortunately, CakePHP is not designed to work with PHPUnit. CakePHP switched to using SimpleTest , and you will have one of two options, reorganize your tests to work with SimpleTest or change the kernel to use PHPUnit.

However, it should be said that Mark Story stated that CakePHP 2.0 will use PHPUnit to test the environment, so if you can wait until then, that might be the best option.

CakePHP 1.3 Test Book

+3
source

It is relatively easy. I am using cake 1.3 from composer installation. This is what my composer.json looks like:

{ "config": { "vendor-dir": "vendors/composer" }, "require": { "phpunit/phpunit": "3.7.*", "cakephp/cakephp-1.3": "1.3", }, "repositories": [ { "type": "package", "package": { "name": "cakephp/cakephp-1.3", "version": "1.3", "source": { "url": "https://github.com/cakephp/cakephp.git", "type": "git", "reference": "1.3" } } } ] } 

Then the phpunit bootstrap.php file in the test directory:

 <?php include('../vendors/composer/autoload.php'); include('../webroot/index.php'); 

This phpunit.xml form the same directory:

 <?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="bootstrap.php" backupStaticAttributes="false" cacheTokens="false" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" forceCoversAnnotation="false" mapTestClassNameToCoveredClassName="false" printerClass="PHPUnit_TextUI_ResultPrinter" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader" strict="false" verbose="false" > <testsuites> <testsuite name="AllTests"> <directory>.</directory> </testsuite> </testsuites> <filter> <blacklist> <directory suffix=".php"></directory> <file></file> <exclude> <directory suffix=".php"></directory> <file></file> </exclude> </blacklist> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php"></directory> <file></file> <exclude> <directory suffix=".php"></directory> <file></file> </exclude> </whitelist> </filter> </phpunit> 

Remember to load application classes into test setup. You can do it in cakephp way. For example, if your controller is called a calendar, your calendarTest.php might look like this:

 <?php /** * Class ComponentsCommonTest * @property calendarController $calendarController */ class CalendarTest extends PHPUnit_Framework_TestCase { /** * @var calendarController $calendarController */ private $calendarController; function setUp() { App::import('Core', array('View', 'Controller', 'Model', 'Router')); App::import('Controller', 'Calendar'); $this->calendarController =& new CalendarController(); $this->calendarController->constructClasses(); $this->calendarController->layout = null; } } 

The same goes for models, vendor classes, etc. Works great for me.

+5
source

Source: https://habr.com/ru/post/888809/


All Articles