欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Yaf框架PHPUnit集成测试方法

程序员文章站 2022-05-15 07:51:00
本文介绍了详解yaf框架phpunit集成测试方法,分享给大家,具体如下: 测试目录 test ├── testcase.php ├── bootstrap...

本文介绍了详解yaf框架phpunit集成测试方法,分享给大家,具体如下:

测试目录

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 测试框架入口文件

define("app_path", realpath(dirname(__file__) . '/../'));
date_default_timezone_set("asia/shanghai");
define("test_dir", __dir__);

testcase.php 测试文件基础类

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类例子

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('22');
    $this->assertinternaltype('array', $token);
    $this->assertinternaltype('string', $token['token']);
  }

}

basecontrollertest.php controller类例子

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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。