Integration Test of Yaf framework and PHPUnit

Source: Internet
Author: User
Tags constructor php file php framework

Yaf, short for Yet Another Framework, is a PHP Framework written in C language. Yaf has been in contact with Yaf for nearly one year. Yaf has a very good performance, but there are few materials available. I always wanted to use PHPUnit on Yaf. I found very little information about this on the Internet. As a result, I tried this and initially tested the controller and model.

Code can be downloaded directly on github: https://github.com/chenjiebin/yaf-phpunit-test/

First, report the running environment:

PHP: 5.3.13 or 5.4.16
Yaf: 2.2.9
Phpunit: 3.7.29 No problems found in php 5.3.13 and 5.4.16. You must download the corresponding version for Yaf extension.

The following describes the relevant files and code.

The test directory is tests.

Phpunit. xml file

The PHPUnit configuration file contains the following content:

<Phpunit bootstrap = "./bootstrap. php"> </phpunit>

Indicates that bootstrap. php starts to guide the test execution.

Bootstrap. Php file

Some global configurations such as pilot files and declarations of constants for test execution can be done here. The content is as follows:

Define ("APPLICATION_PATH", realpath (dirname (_ FILE __).'/../'));

ApplicationlibraryTestPHPUnitTestCase. Php file
The custom test base class file inherits the class from the controller and model test classes. The code is as follows:

The code is as follows: Copy code

Namespace TestPHPUnit;
/**
* Test base class
*/
Class TestCase extends PHPUnit_Framework_TestCase {
/**
* Yaf running instance
     *
* @ Var YafApplication
*/
Protected $ _ application = null;
/**
* Constructor: Initializes the yaf running instance.
*/
Public function _ construct (){
$ This-> _ application = $ this-> getApplication ();
Parent: :__ construct ();
    }
/**
* Set application
*/
Public function setApplication (){
$ Application = new YafApplication (APPLICATION_PATH. "/conf/application. ini ");
$ Application-> bootstrap ();
YafRegistry: set ('application', $ application );
    }
/**
* Obtain application www.111cn.net
     *
* @ Return YafApplication
*/
Public function getApplication (){
$ Application = YafRegistry: get ('application ');
If (! $ Application ){
$ This-> setApplication ();
        }
Return YafRegistry: get ('application ');
    }
}


Because Yaf can only be instantiated once when running, it is saved to the Yaf registry after the Yaf running instance is initialized to avoid repeated instantiation. In addition, the method for initializing the Yaf running instance is called in the constructor to create a new data model in the test of the data model without importing related files.

ApplicationlibraryTestPHPUnitControllerTestCase. Php file

The controller test base class. All test classes of the controller inherit this class. The content is as follows:

The code is as follows: Copy code

Namespace TestPHPUnit;
Require_once APPLICATION_PATH. '/tests/application/library/Test/PHPUnit/TestCase. Php ';
/**
* Controller test base class
*/
Class ControllerTestCase extends TestPHPUnitTestCase {
  
}


No code for future extension.

ApplicationlibraryTestPHPUnitModelTestCase. Php file

Model test base class. All model test classes inherit this class. The content is as follows:

The code is as follows: Copy code

Namespace TestPHPUnit;
Require_once APPLICATION_PATH. '/tests/application/library/Test/PHPUnit/TestCase. Php ';
/**
* Data model test base class
*/
Class ModelTestCase extends TestPHPUnitTestCase {
  
}


ApplicationcontrollersIndexTest. php homepage controller test file

The controller test example file contains the following content:

The code is as follows: Copy code
Require_once APPLICATION_PATH. '/tests/application/library/Test/PHPUnit/ControllerTestCase. Php ';
/**
* Homepage controller testing
*/
Class IndexTest extends TestPHPUnitControllerTestCase {
/**
* Test the index method.
*/
Public function testIndex (){
$ Request = new YafRequestSimple ("CLI", "Index", "Index", 'index ');
$ Response = $ this-> _ application-> getDispatcher ()
-> ReturnResponse (true)
-> Dispatch ($ request );
$ Content = $ response-> getBody ();
$ This-> assertEquals ('index phpml', $ content );
    }
}


The test controller mainly uses the YafRequestSimple class, and sets the parameter to CLI to run the command line.

ApplicationcontrollersmodelsUserTest. php model test file

The data model test file contains the following content:

The code is as follows: Copy code

Require_once APPLICATION_PATH. '/tests/application/library/Test/PHPUnit/ModelTestCase. Php ';
Class UserTest extends TestPHPUnitModelTestCase {
Public function testGetUserName (){
$ Model = new UserModel ();
$ UserId = 1;
$ Result = $ model-> getUserName ($ userId );
$ This-> assertEquals ('iceup', $ result );

$ UserId = 100;
$ Result = $ model-> getUserName ($ userId );
$ This-> assertFalse ($ result );
    }
}


The new UserModel () can be used directly here, because the Yaf running instance has been initialized in the construction method of the test base class.

Summary

During the test, the most common headache is the automatic file loading. Basically, as long as this problem is solved, the rest are easier to handle. Currently, some practical problems have been encountered in the application of the project, such as outputting data in json format, throwing an exception, and passing parameters in post. The corresponding solutions will be provided in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.