Home  >  Article  >  Backend Development  >  Detailed example of Yaf framework PHPUnit integration testing method

Detailed example of Yaf framework PHPUnit integration testing method

小云云
小云云Original
2017-12-27 16:23:131665browse

This article mainly introduces the detailed Yaf framework PHPUnit integration testing method. The editor thinks it is quite good. Now I will share it with you and give it a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Test directory


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__) . &#39;/../&#39;));
date_default_timezone_set("Asia/Shanghai");
define("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(&#39;/Users/xiong/Sites/kyYaf&#39;, 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 . &#39;/conf/application.ini&#39;);
    $application->bootstrap();
    self::$_application = $application;
  }
}

TokenServiceTest.php service class example


namespace Service;
use test\TestCase;
include TEST_DIR . &#39;/TestCase.php&#39;;
include APP_PATH . &#39;/application/library/Service/BaseService.php&#39;;
include APP_PATH . &#39;/application/library/Service/TokenService.php&#39;;
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(&#39;22&#39;);
    $this->assertInternalType(&#39;array&#39;, $token);
    $this->assertInternalType(&#39;string&#39;, $token[&#39;token&#39;]);
  }

}

BaseControllerTest.php controller class example


namespace test\controller;
include TEST_DIR .&#39;/TestCase.php&#39;;
use test\TestCase;
class BaseControllerTest extends TestCase
{
  public function testGetConfigAction()
  {
    $request = new Simple(&#39;CLI&#39;, &#39;&#39;, &#39;Index&#39;, &#39;getConfig&#39;);
    $response = self::$_application->getDispatcher()->returnResponse(true)->dispatch($request);
    $contents = $response->getBody();
    $data = json_decode($contents, true);
    $this->assertInternalType(&#39;array&#39;, $data);
  }
}

Related recommendations;

PHPYaf execution Process source code

Share the tutorial on how to install Swoole, Yar, and Yaf under PHP 7

Yaf’s hello world example

The above is the detailed content of Detailed example of Yaf framework PHPUnit integration testing method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn