|
1 | | -# codeigniter-phpunit |
2 | | -CodeIgniter 3 PHPUnit test library |
| 1 | +<p align="center"> |
| 2 | + <a href="https://codeigniter.com/" target="_blank"> |
| 3 | + <img src="https://codeigniter.com/assets/images/ci-logo-big.png" height="100px"> |
| 4 | + </a> |
| 5 | + <h1 align="center">CodeIgniter PHPUnit Test</h1> |
| 6 | + <br> |
| 7 | +</p> |
| 8 | + |
| 9 | +CodeIgniter 3 PHPUnit Test extension library |
| 10 | + |
| 11 | +[](https://packagist.org/packages/yidas/codeigniter-phpunit) |
| 12 | +[](https://packagist.org/packages/yidas/codeigniter-phpunit) |
| 13 | +[](https://packagist.org/packages/yidas/codeigniter-phpunit) |
| 14 | + |
| 15 | +FEATURES |
| 16 | +-------- |
| 17 | + |
| 18 | +- ***PHPUnit Test** in **Codeigniter 3** Framework* |
| 19 | + |
| 20 | +- *Easy to install into your Codeigniter project by Composer* |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +OUTLINE |
| 25 | +------- |
| 26 | + |
| 27 | +- [Requirements](#requirements) |
| 28 | +- [Installation](#installation) |
| 29 | +- [Directory Structure](#directory-structure) |
| 30 | +- [Configuration](#configuration) |
| 31 | +- [Usage](#usage) |
| 32 | +- [Test Case](#test-case) |
| 33 | + |
| 34 | + |
| 35 | +REQUIREMENTS |
| 36 | +------------ |
| 37 | + |
| 38 | +This library requires the following: |
| 39 | + |
| 40 | +- PHP 5.3.0+ |
| 41 | +- CodeIgniter 3.0.0+ |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +INSTALLATION |
| 46 | +------------ |
| 47 | + |
| 48 | +Run Composer in your Codeigniter project under the folder `\application`: |
| 49 | + |
| 50 | + composer require yidas/codeigniter-phpunit |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +DIRECTORY STRUCTURE |
| 55 | +------------------- |
| 56 | + |
| 57 | +``` |
| 58 | +codeigniter/ |
| 59 | +└── application/ |
| 60 | + ├── tests/ Test cases |
| 61 | + ├── vendor/ Vendor included yidas/codeigniter-phpunit |
| 62 | + └── phpunit.xml PHPUnit XML |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +CONFIGURATION |
| 68 | +------------- |
| 69 | + |
| 70 | +According to [Directory Structure](#directory-structure), create and configure `phpunit.xml` under `application` directory: |
| 71 | + |
| 72 | +```xml |
| 73 | +<?xml version="1.0" encoding="UTF-8" ?> |
| 74 | +<phpunit bootstrap="vendor/yidas/codeigniter-phpunit/bootstrap.php"> |
| 75 | + <testsuites> |
| 76 | + <testsuite name="TestSuite"> |
| 77 | + <directory>tests</directory> |
| 78 | + </testsuite> |
| 79 | + </testsuites> |
| 80 | +</phpunit> |
| 81 | +``` |
| 82 | + |
| 83 | +For this `phpunit.xml` template, the test cases directory is `application/test`, make sure you would create every test cases under it. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +USAGE |
| 88 | +----- |
| 89 | + |
| 90 | +In the `application` directory of this library, run `phpunit` from vendor: |
| 91 | + |
| 92 | +``` |
| 93 | +./vendor/bin/phpunit |
| 94 | +``` |
| 95 | + |
| 96 | +Then the result would like: |
| 97 | + |
| 98 | +``` |
| 99 | +PHPUnit 5.7.27 by Sebastian Bergmann and contributors. |
| 100 | +
|
| 101 | +
|
| 102 | +
|
| 103 | +Time: 40 ms, Memory: 2.75MB |
| 104 | +
|
| 105 | +No tests executed! |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +TEST CASE |
| 111 | +--------- |
| 112 | + |
| 113 | +With this extension libaray, you could write test cases with loading Codeigniter framework. |
| 114 | + |
| 115 | +For example, write a test case `application/tests/CodeigniterTest.php` for testing Codeigniter config component: |
| 116 | + |
| 117 | +```php |
| 118 | +<?php |
| 119 | + |
| 120 | +use PHPUnit\Framework\TestCase; |
| 121 | + |
| 122 | +final class CodeigniterTest extends TestCase |
| 123 | +{ |
| 124 | + function __construct() |
| 125 | + { |
| 126 | + parent::__construct(); |
| 127 | + // Load CI application |
| 128 | + $this->CI = & get_instance(); |
| 129 | + } |
| 130 | + |
| 131 | + public function testConfigItem() |
| 132 | + { |
| 133 | + $indexPage = $this->CI->config->item('index_page'); |
| 134 | + |
| 135 | + $this->assertSame('index.php', $indexPage); |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +Then you would get the result `OK (1 test, 1 assertion)` by running PHPUnit test. |
0 commit comments