Welcome Guest, Not a member yet? Register   Sign In
How to: CI - PHPUNIT - DATAMAPPER
#1

[eluser]insert_hilarity_here[/eluser]
Firstly this is not the ideal way to setup unit testing, I have been using this approach and it seems to be working well for me, however the my-ciunit project by kenjis is a much more professional approach (http://d.hatena.ne.jp/Kenji_s/20120117/1326763908)

However because I use Datamapper this bug https://bitbucket.org/kenjis/my-ciunit/i...21-in-test caused me too may difficulties so this is why I am not using my-ciunit.

This is my approach:

1) Download https://github.com/reggi/cidip/tree/mast...hird_party
2) Modify the $system_path and $application_folder to be relative if needed
3) Above where the code is commented out (//require_once BASEPATH.'core/CodeIgniter.php'Wink on line 202 add in the datamapper bootstrap file so it looks like
Code:
/*
* --------------------------------------------------------------------
* LOAD THE DATAMAPPER BOOTSTRAP FILE
* -------------------------------------------------------------------- *
*/
require_once APPPATH.'third_party/datamapper/bootstrap.php';

/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
//require_once BASEPATH.'core/CodeIgniter.php';

4) Modify the routing setion from this:
Code:
/*
* ------------------------------------------------------
*  Instantiate the routing class and set the routing
* ------------------------------------------------------
*/
$RTR =& load_class('Router', 'core');
$RTR->_set_routing();

// Set any routing overrides that may exist in the main index file
if (isset($routing))
{
  $RTR->_set_overrides($routing);
}
to

Code:
$RTR =& load_class('Router', 'core');
//$RTR->_set_routing();

// Set any routing overrides that may exist in the main index file
if (isset($routing))
{
  $RTR->_set_overrides($routing);
}
        
        $alt_default_controller = 'cdip';
        if (isset($alt_default_controller))
        {
            $RTR->default_controller = $alt_default_controller;
            $RTR->_set_default_controller();
        }

5) create a tests folder and in it your phpunit bootstrap file should contain a require_once path to the dip file you have just editted

Code:
<?php
ob_start();
require_once getcwd().'/../application/third_party/dip.php';
ob_end_clean();

6) Create a controller called cdip (you can all it anything you want really just make sure it matches $alt_default_controller), the controller should look like

Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Cdip extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
      
    }
}

its just a dummy controller that does nothing.

7) Say you have a library called "test_library" so in the setup function you can now use $ci =& get_instance(); to load that library so you would have something like:

Code:
<?php
/**
* Generated by PHPUnit_SkeletonGenerator on 2012-06-28 at 12:58:23.
*/
class test_libraryTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var test_library
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $ci =& get_instance();
        $ci->load->library('test_library');
        $this->object = new test_library;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

8) You can now start writing your unit tests for your library or model and so on. But be aware you are testing CI as well.

9) Using the environment setting http://ellislab.com/codeigniter/user-gui...vironments you will probably want to use this to load a test version of your database in the dip.php file, so your create, update, delete tests are done on test data.

10) For testing controllers can use an autoloader or add in include_once(APPPATH."controllers/".strtolower($class).EXT); where $class is the class name of the controller into the setUp function, for everything else you can load using $ci variable.




Theme © iAndrew 2016 - Forum software by © MyBB