2

Does anyone know how to add PHPUnit to an existing CakePHP 2.0 project that wasn't created using composer?

I am trying to add PHPUnit to an existing CakePHP 2.0 project that wasn't created with composer and I cannot get CakePHP to see that PHPUnit is installed. I am using a Windows machine.

Here are the steps I have taken so far:

  1. Installed PHPUnit 3.7.32 globally via composer
  2. Added PHPUnit to my path
  3. Verified the install from the command line, i.e. phpunit --version
  4. Added the location to the PHP include_path in php.ini
  5. Restarted the server
  6. Installed a new CakePHP project using Composer (to test CakePHP could see PHPUnit)
  7. Successfully tested the new project had PHPUnit installed

Thanks in advance.

2
  • Clarify "I cannot get CakePHP to see that PHPUnit is installed.". What exact error are you getting and where are you getting it? Commented Mar 4, 2015 at 21:58
  • The test.php page says that PHPUnit is not installed. Running a test via the cake testsuite command gives an error stating that PHPUnit must be installed. Commented Mar 4, 2015 at 22:38

4 Answers 4

2

I finally was able to add PHPUnit to an existing CakePHP project that wasn't created with composer.

I had a look inside the CakeTestSuiteDispatcher.php file to see where it was trying to find PHPUnit. It looks in the vendors, app/Vendor, and the include_path for a folder called PHPUnit that has the Autoload.php file (thanks to Nick Zinger for his help with this). Once this folder has been copied across, CakePHP will see that PHPUnit is installed but will throw errors when including dependencies (code coverage, timer, etc.). Here are the steps I took to get everything working.

  1. Ensure you have the test database setup in app/config/Database.php otherwise your main database may be overwritten or corrupted
  2. Install a fresh CakePHP (version 2.6.2) project to a new location with PHPUnit (I used version 3.7.38) via Composer
  3. Copy the inner PHPUnit folder with the Autoload.php file (phpunit/phpunit/PHPUnit ) in the new project to the cakephp vendors folder (the folder outside app) of the old project
  4. Copy the following folders from the phpunit folder (from the project created with composer in step 1) to you existing vendors folder (the one outside of app):
    • php-code-coverage
    • php-file-iterator
    • php-text-template
    • php-timer
    • php-token-stream
    • phpunit-mock-objects
  5. Update the require_once paths (I used absolute paths) in the following files in your existing project:
    • PHPUnit/Autoload.php
    • php-code-coverage/PHP/CodeCoverage/Autoload.php

Once these changes were made the CakePHP Test Suite was available and I was able to run my test cases. This worked for my CakePHP Project(v2.0.6) and my colleagues CakePHP project(v2.0.6).

I hope this helps anyone else having the same problem.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're still having issues with this, here is what I did to make it work:

  1. Download the source from https://github.com/sebastianbergmann/phpunit/tree/3.7

  2. Copy the inner PHPUnit folder (the one containing Autoload.php) into a php included path. Make sure that path is included, and that PHPUnit/Autoload.php is reachable directly from that path.

  3. Restart apache

After the above it should work.

8 Comments

Hi Nick, thank you for your answer. I have tried that but unfortunately it's not working for me. I still see the message "CakePHP Test Suite 2.0 PHPUnit is not installed!". Here are the steps I took: 1) Download and extracted the phpunit folder 2) Copied the inner PHPUnit folder to my documents folder 3) Add the following to my php.ini include_path ".;C:\Users\Username\Documents\PHPUnit" 4) Restarted apache
What is the structure like? If the Autoload.php is right in the PHPUnit folder, then your include path should be: ".;C:\Users\Username\Documents"
The PHPUnit folder contains five folders (Extensions, Framework, Runner, TextUI, Util) and Autoload.php and Autoload.php.in. I modified the include_path to ".;C:\Users\Username\Documents" and restarted apache. I checked the include_path setting using phpinfo() and the include_path was updated but I still get the "PHPUnit is not installed" message.
Try copying the PHPUnit folder into one of the other, standard include_paths. Might be a permission issue.
I moved the PHPUnit folder into the standard include_path folder on ZendServer "ZendServer\share\ZendFramework\library\Zend" but I'm still getting the message sating "PHPUnit is not installed". Do I need to add an import to the bootstrap file or some other way of letting CakePHP know that PHPUnit is installed? I have created a new project (CakePHP 2.6) with composer and it is able to find that PHPUnit is installed on the include_path and I can run the core tests, so does composer add something to the CakePHP configuration to add PHPUnit?
|
0

I followed the below steps:

  1. Installed PHPUnit via composer [ I installed PHPUnit ~/CompanyName/Projects/Project/app in this directory ]

curl -sS https://getcomposer.org/installer | php

php composer.phar require --dev phpunit/phpunit:"3.7.38" --ignore-platform-reqs

  1. Remember to have debug level set to 1 in app/Config/core.php

  2. Add a $test database configuration:

public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host'       => 'localhost',
'login'      => 'user',
'password'   => 'password',
'database'   => 'test_database'
);
  1. Created a dummy file here: app/Test/testMyClass.php

class MyClassTest extends PHPUnit_Framework_TestCase {
     public function testCreateMyClass() {
        // Tests are written here
     }
}
?>
  1. Run the test file:

./vendor/bin/phpunit --bootstrap vendor/autoload.php app/Test/testMyClass.php

Comments

-1

You need run the following code in terminal. (path: in your project cakephp)

composer require --dev phpunit/phpunit:"3.7.38"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.