Advertisement
arsel

php unit install

Dec 10th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. 1) To use PHPUnit you need PEAR package. So install latest PEAR package and run the PEAR installer.
  2.  
  3. Download the package from http://pear.php.net/go-pear.phar and keep it in your E:\xampp\php folder
  4.  
  5. cd \xampp\php
  6. php go-pear.phar
  7. 2) Run the E:\xampp\php\PEAR_ENV.reg file to import your PEAR environment variable settings.
  8.  
  9. 3) Restart apache server. This will load the new environment variables into memory. Confirm your settings by running:
  10.  
  11. pear config-show
  12. It should now read: C:\xampp\php\pear.ini
  13.  
  14. 4) Install PHPUnit & Skeleton Generator through PEAR
  15.  
  16. pear config-set auto_discover 1
  17. pear install pear.phpunit.de/PHPUnit
  18. pear install phpunit/PHPUnit_SkeletonGenerator
  19. 5) Check your version of PHPUnit – It should be at least version
  20.  
  21. phpunit --version
  22. Configure PHPUnit with Codeigniter
  23.  
  24. 6) Download Codeignator framework and install it. Once installation is done, create a tests folder in Codeignator project root directory and copy & paste the index.php in there and rename it to bootstrap.php.
  25.  
  26. 7) Also create a phpunit.xml file on following path, this file will have the PHPUnit configuration settings for Codeignator and test cases or test suite you want to run.
  27.  
  28. // tests/phpunit.xml
  29. <phpunit bootstrap="bootstrap.php"
  30. colors="true"
  31. convertErrorsToExceptions="true"
  32. convertNoticesToExceptions="true"
  33. convertWarningsToExceptions="true"
  34. processIsolation="false"
  35. stopOnFailure="false"
  36. syntaxCheck="false"
  37. verbose="true">
  38. </phpunit>
  39. 8) Add following variables in the bootstrap.php file.
  40.  
  41. $system_path = '../system';
  42. $application_folder = '../application';
  43. Write First Test Case
  44.  
  45. 9) To write first test case, I am creating a test module “POST”:
  46.  
  47. <?php // application/models/post.php
  48. class Post extends CI_Model {
  49. public function getAll()
  50. {
  51. return array( array('title'=>'post 1','content'=>'...'),
  52. array('title'=>'post 2','content'=>'...'),
  53. array('title'=>'post 3','content'=>'...'),
  54. array('title'=>'post 4','content'=>'...'),
  55. array('title'=>'post 5','content'=>'...'),
  56. );
  57. }
  58. }
  59. 10) Now create a PostTest.php file in your test folder and add following code:
  60.  
  61. <?php // tests/PostTest.php
  62.  
  63. class PostTest extends PHPUnit_Framework_TestCase {
  64.  
  65. private $CI;
  66. public function setUp() {
  67.  
  68. $this->CI = &get_instance();
  69. }
  70.  
  71. public function testGetAllPosts() {
  72. $this->CI->load->model('post');
  73. $posts = $this->CI->post->getAll();
  74. $this->assertEquals(5, count($posts));
  75. }
  76. }
  77. 11) Let’s try to run our test from the command line. Navigate to the tests folder and execute following commands:
  78.  
  79. cd E:\xampplite\htdocs\CodeIgniter_2.1.3\tests
  80. phpunit .
  81. We should get the following error:
  82.  
  83. Fatal error: Call to a member function item() on a non-object in E:\xampplite\htdocs\CodeIgniter_2.1.3\system\core\Utf8.php on line 47
  84. It’s because we still need to change few things in Codeigniter. Open up the system/core/Utf8.php file and goto line 41, the $CFG variable is marked as global and this is causing issues with PHPUnit. Replace that line by loading the class via CodeIgniter like so:
  85.  
  86. $CFG =& load_class('Config', 'core');
  87. 12) Run the test again. If you are still working in Codeigniter development or live environment you will face another error. To remove the error change your environment to development in config file:
  88.  
  89. //Change the environment on line 21
  90. define('ENVIRONMENT', 'testing');
  91.  
  92. //Enable hooks in the application/config/config.php file on line 94
  93. $config['enable_hooks'] = TRUE;
  94. Add the following to the application/config/hooks.php file:
  95.  
  96. // application/config/hooks.php
  97. $hook['display_override'] = array(
  98. 'class' => 'DisplayHook',
  99. 'function' => 'captureOutput',
  100. 'filename' => 'DisplayHook.php',
  101. 'filepath' => 'hooks'
  102. );
  103. 13) Create a DisplayHook.php file in the application/hooks folder and enter the following code:
  104.  
  105. <?php // application/hooks/DisplayHook.php
  106.  
  107. class DisplayHook {
  108. public function captureOutput() {
  109. $this->CI =& get_instance();
  110. $output = $this->CI->output->get_output();
  111. if (ENVIRONMENT != 'testing') {
  112. echo $output;
  113. }
  114. }
  115. }
  116. This checks the current ENVIRONMENT and only outputs data if we’re not in testing, as we’ve specified this environment in our bootstrap file normal output wont be seen. Go back to the console and run the tests again and if everything has gone smoothly you should see the following:
  117.  
  118. C:\xampplite\htdocs\CodeIgniter_2.1.3\tests>phpunit .
  119. PHPUnit 3.6.11 by Sebastian Bergmann.
  120.  
  121. Configuration read from C:\xampplite\htdocs\CodeIgniter_2.1.3\tests\phpunit.xml
  122.  
  123. .
  124.  
  125. Time: 0 seconds, Memory: 5.25Mb
  126.  
  127. OK (1 test, 1 assertion)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement