Integration Test of Yaf framework combined with PHPUnit

Source: Internet
Author: User
Yaf, short for YetAnotherFramework, 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 handled the issue of control.

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 handled the issue of control.






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

No problem was found in php 5.3.13 and 5.4.16, And the Yaf extension should pay attention to downloading the corresponding version.

The following describes the relevant files and code.

The test directory is tests.

Phpunit. xml file

The PHPUnit configuration file contains the following content:

Indicates that the test starts from bootstrap.php when the test is executed.
bootstrap.php file
Some global configuration such as boot files for test execution, declaration of constants can be done here. The content is as follows:
define ("APPLICATION_PATH", realpath (dirname (__ FILE__). '/../'));

Application \ library \ Test \ PHPUnit \ TestCase. php file
The custom Test base class file inherits the class from the Controller and Model Test classes. The Code is as follows:

namespace Test \ PHPUnit;
/ **
  * Test base class
  * /
class TestCase extends \ PHPUnit_Framework_TestCase {
     / **
      * yaf running example
      *
      * @var \ Yaf \ Application
      * /
     protected $ _application = null;
     / **
      * Constructor method, initialize yaf running instance
      * /
     public function __construct () {
         $ this-> _ application = $ this-> getApplication ();
         parent :: __ construct ();
     }
     / **
      * Set application
      * /
     public function setApplication () {
         $ application = new \ Yaf \ Application (APPLICATION_PATH. "/conf/application.ini");
         $ application-> bootstrap ();
         \ Yaf \ Registry :: set ('application', $ application);
     }
     / **
      * Get application
      *
      * @return \ Yaf \ Application
      * /
     public function getApplication () {
         $ application = \ Yaf \ Registry :: get ('application');
         if (! $ application) {
             $ this-> setApplication ();
         }
         return \ Yaf \ Registry :: 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.

Application \ library \ Test \ PHPUnit \ ControllerTestCase. php file

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

namespace Test \ PHPUnit;
require_once APPLICATION_PATH. '/tests/application/library/Test/PHPUnit/TestCase.php';
/ **
  * Controller test base class
  * /
class ControllerTestCase extends \ Test \ PHPUnit \ TestCase {
}

No code for future extension.

Application \ library \ Test \ PHPUnit \ ModelTestCase. php file

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

namespace Test \ PHPUnit;
require_once APPLICATION_PATH. '/tests/application/library/Test/PHPUnit/TestCase.php';
/ **
  * Data model test base class
  * /
class ModelTestCase extends \ Test \ PHPUnit \ TestCase {
}

Application \ controllers \ IndexTest. php homepage Controller Test File

The Controller test example file contains the following content:

require_once APPLICATION_PATH. '/tests/application/library/Test/PHPUnit/ControllerTestCase.php';
/ **
  * Home controller test class
  * /
class IndexTest extends \ Test \ PHPUnit \ ControllerTestCase {
     / **
      * Test the index method
      * /
     public function testIndex () {
         $ request = new \ Yaf \ Request \ Simple ("CLI", "Index", "Index", 'index');
         $ response = $ this-> _ application-> getDispatcher ()
                 -> returnResponse (true)
                 -> dispatch ($ request);
         $ content = $ response-> getBody ();
         $ this-> assertEquals ('index phtml', $ content);
     }
}

The test controller mainly uses the \ Yaf \ Request \ Simple class, and sets the parameter to CLI to run the command line.

Application \ controllers \ models \ UserTest. php Model Test File

The data model test file contains the following content:

require_once APPLICATION_PATH . '/tests/application/library/Test/PHPUnit/ModelTestCase.php';
class UserTest extends \Test\PHPUnit\ModelTestCase {
    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);
    }
}

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.

Reprinted Please note: Happy programming» Yaf framework combined with PHPUnit integration test

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.