This article mainly introduces the detailed YAF framework PHPUnit integration test method, small series feel very good, now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
This article introduced the detailed YAF framework PHPUnit integration test method, share to everybody, concrete as follows:
Test Catalog
test├──testcase.php├──bootstrap.php├──controller│ ├──basecontrollertest.php│ └── Indexcontrollertest.php├──model├──phpunit.xml└──service └──tokenservicetest.php
Phpunit.xml
<?xml version= "1.0" encoding= "UTF-8"? ><phpunit xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= "https://schema.phpunit.de/6.2/phpunit.xsd" extensionsdirectory= " Dbunit.phar "bootstrap="./bootstrap.php "></phpunit>
bootstrap.php Test Framework Entry file
Define ("App_path", Realpath (DirName (__file__). '/.. /'));d ate_default_timezone_set ("Asia/shanghai");d efine ("Test_dir", __dir__);
testcase.php test file base class
Namespace Test;use phpunit\framework\testcase as Test;use yaf\application;class TestCase extends test{ protected static $_application = null; protected function setUp () { self::$_application = $this->getapplication (); Parent::setup (); } Public Function Testapppath () { $this->assertequals ('/users/xiong/sites/kyyaf ', App_path); } Public Function testApp () { $this->assertequals (Application::app (), self::$_application); } Public Function TestApplication () { $this->assertnotnull (self::$_application); } Public Function getapplication () { if (self::$_application = = null) { $this->setapplication (); } return self::$_application; } Public Function setapplication () { $application = new Application (App_path. '/conf/application.ini '); $application->bootstrap (); Self::$_application = $application; }}
Tokenservicetest.php Service Class Example
Namespace Service;use test\testcase;include Test_dir. '/testcase.php '; include App_path. '/application/library/service/baseservice.php '; include App_path. '/application/library/service/tokenservice.php '; class Tokenservicetest extends testcase{ /** * @var Tokenservice */ protected static $tokenService; Public Function setUp () {self :: $tokenService = Tokenservice::getinstance (); Parent::setup (); } Public Function Testcreatetoken () { $token = self:: $tokenService->createtoken (' n '); $this->assertinternaltype (' array ', $token); $this->assertinternaltype (' string ', $token [' token ']);} }
Basecontrollertest.php Controller Class Example
Namespace Test\controller;include test_dir. ' /testcase.php '; use Test\testcase;class basecontrollertest extends testcase{public function Testgetconfigaction ( { $request = new Simple (' CLI ', ' ', ' Index ', ' getconfig '); $response = Self::$_application->getdispatcher ()->returnresponse (True)->dispatch ($request); $contents = $response->getbody (); $data = Json_decode ($contents, true); $this->assertinternaltype (' array ', $data); }}