Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 1 | # Do not merge to default until this is blank # |
| 2 | |
| 3 | - Clean up naming conventions |
| 4 | - Figure out config stuff |
| 5 | - Figure out database testing |
| 6 | |
| 7 | |
| 8 | |
| 9 | # -------------- CodeIgniter Testing (4/20/2011) -------------- # |
| 10 | |
| 11 | |
| 12 | # Introduction: |
| 13 | |
| 14 | This is the preliminary CodeIgniter testing documentation. It |
| 15 | will cover both internal as well as external APIs and the reasoning |
| 16 | behind their implemenation, where appropriate. As with all CodeIgniter |
| 17 | documentation, this file should maintain a mostly human readable |
| 18 | format to facilitate clean api design. [see http://arrenbrecht.ch/testing/] |
| 19 | |
| 20 | *FIRST PUBLIC DRAFT: EVERYTHING IS SUBJECT TO CHANGE* |
| 21 | |
Greg Aker | 25a6690 | 2011-04-21 09:24:16 -0500 | [diff] [blame^] | 22 | # Requirements |
| 23 | |
| 24 | 1. PHP Unit |
| 25 | - pear channel-discover pear.phpunit.de |
| 26 | - pear install phpunit/PHPUnit |
| 27 | |
| 28 | 2. vfsStream |
| 29 | - pear channel-discover pear.php-tools.net |
| 30 | - pear install pat/vfsStream-alpha |
| 31 | |
| 32 | |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 33 | # Test Suites: |
| 34 | |
| 35 | CodeIgniter bootstraps a request very directly, with very flat class |
| 36 | hierarchy. As a result, there is no main CodeIgniter class until the |
| 37 | controller is instantiated. |
| 38 | |
| 39 | This has forced the core classes to be relatively decoupled, which is |
| 40 | a good thing. However, it makes that portion of code relatively hard |
| 41 | to test. |
| 42 | |
| 43 | Right now that means we'll probably have two core test suites, along |
| 44 | with a base for application and package tests. That gives us: |
| 45 | |
| 46 | 1. Bootstrap Test - test common.php and sanity check codeigniter.php [in planning] |
| 47 | 2. System Test - test core components in relative isolation [in development] |
| 48 | 3. Application Test - bootstrapping for application/tests [not started] |
| 49 | 4. Package Test - bootstrapping for <package>/tests [not started] |
| 50 | |
| 51 | |
| 52 | ## 1. Bootstrap Test |
| 53 | |
| 54 | Testing common.php should be pretty simple. Include the file, and test the |
| 55 | functions. May require some tweaking so that we can grab the statics from all |
| 56 | methods (see is_loaded()). Testing the actual CodeIgniter.php file will most |
| 57 | likely be an output test for the default view, with some object checking after |
| 58 | the file runs. Needs consideration. |
| 59 | |
| 60 | |
| 61 | ## 2. System Test |
| 62 | |
| 63 | Testing the core system relies on being able to isolate the core components |
| 64 | as much as possible. A few of them access other core classes as globals. These |
| 65 | should be mocked up and easy to manipulate. |
| 66 | |
| 67 | All functions in common.php should be a minimal implementation, or and mapped |
| 68 | to a method in the test's parent class to gives us full control of their output. |
| 69 | |
| 70 | |
Pascal Kriete | 34bc089 | 2011-04-21 01:22:23 -0400 | [diff] [blame] | 71 | ### CI_TestCase Documentation |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 72 | |
| 73 | |
Pascal Kriete | 34bc089 | 2011-04-21 01:22:23 -0400 | [diff] [blame] | 74 | Test cases should extend CI_TestCase. This internally extends |
| 75 | PHPUnit_Framework_TestCase, so you have access to all of your |
| 76 | usual PHPUnit methods. |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 77 | |
| 78 | We need to provide a simple way to modify the globals and the |
| 79 | common function output. We also need to be able to mock up |
| 80 | the super object as we please. |
| 81 | |
| 82 | Current API is *not stable*. Names and implementations will change. |
| 83 | |
Pascal Kriete | 34bc089 | 2011-04-21 01:22:23 -0400 | [diff] [blame] | 84 | $this->ci_set_config($key, $val) |
| 85 | Set the global config variables. If key is an array, it will |
| 86 | replace the entire config array. They are _not_ merged. |
| 87 | |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 88 | $this->ci_instance($obj) |
| 89 | set the object to use as the "super object", in a lot |
| 90 | of cases this will be a simple stdClass with the attributes |
Pascal Kriete | 34bc089 | 2011-04-21 01:22:23 -0400 | [diff] [blame] | 91 | you need it to have. If no parameter, will return the instance. |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 92 | |
Pascal Kriete | 34bc089 | 2011-04-21 01:22:23 -0400 | [diff] [blame] | 93 | $this->ci_instance_var($name, $val) |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 94 | add an attribute to the super object. This is useful if you |
| 95 | set up a simple instance in setUp and then need to add different |
| 96 | class mockups to your super object. |
| 97 | |
| 98 | $this->ci_core_class($name) |
| 99 | Get the _class name_ of a core class, so that you can instantiate |
| 100 | it. The variable is returned by reference and is tied to the correct |
| 101 | $GLOBALS key. For example: |
| 102 | $cfg =& $this->ci_core_class('cfg'); // returns 'CI_Config' |
| 103 | $cfg = new $cfg; // instantiates config and overwrites the CFG global |
| 104 | |
Pascal Kriete | 34bc089 | 2011-04-21 01:22:23 -0400 | [diff] [blame] | 105 | $this->ci_set_core_class($name, $obj) |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 106 | An alternative way to set one of the core globals. |
| 107 | |
Pascal Kriete | 34bc089 | 2011-04-21 01:22:23 -0400 | [diff] [blame] | 108 | $this->ci_get_config() __internal__ |
| 109 | Returns the global config array. Internal as you shouldn't need to |
| 110 | call this (you're setting it, after all). Used internally to make |
| 111 | CI's get_config() work. |
| 112 | |
| 113 | CI_TestCase::instance() __internal__ |
| 114 | Returns an instance of the current test case. We force phpunit to |
| 115 | run with backup-globals enabled, so this will always be the instance |
| 116 | of the currently running test class. |
Pascal Kriete | ba2430b | 2011-04-20 21:44:13 -0400 | [diff] [blame] | 117 | |
| 118 | ## 3. Application Test: |
| 119 | |
| 120 | Not sure yet, needs to handle: |
| 121 | - Libraries |
| 122 | - Helpers |
| 123 | - Models |
| 124 | - MY_* files |
| 125 | - Controllers (uh...?) |
| 126 | - Views? (watir, selenium, cucumber?) |
| 127 | |
| 128 | - Database Testing |
| 129 | |
| 130 | |
| 131 | ## 4. Package Test: |
| 132 | |
| 133 | I don't have a clue how this will work. |
| 134 | |
| 135 | Needs to be able to handle packages |
| 136 | that are used multiple times within the application (i.e. EE/Pyro modules) |
| 137 | as well as packages that are used by multiple applications (library distributions) |