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

所属分类: 网络编程 / PHP编程 阅读数: 2027
收藏 0 赞 0 分享

本文介绍了详解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);
  }
}

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

更多精彩内容其他人还在看

php实现二叉树中和为某一值的路径方法

在本篇文章中我们给大家分享了php实现二叉树中和为某一值的路径方法,有需要的朋友们可以参考下。
收藏 0 赞 0 分享

深入理解 PHP7 中全新的 zval 容器和引用计数机制

这篇文章主要介绍了 PHP7 中全新的 zval 容器和引用计数机制的相关知识, 主要侧重于解释新 zval 容器中的引用计数机制。需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中使用CURL发送get/post请求上传图片批处理功能

这篇文章主要介绍了PHP中使用CURL发送get/post请求上传图片批处理 功能,本文通过实例代码给大家介绍的非常详细,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP实现用session来实现记录用户登陆信息

在本篇文章里我们给大家分享了关于PHP如何用session来实现记录用户登陆信息的知识点,有兴趣的朋友们参考下。
收藏 0 赞 0 分享

实现PHP中session存储及删除变量

在本篇文章中我们给大家分享了PHP中session如何存储及删除变量的相关知识点内容,有兴趣的朋友们参考下。
收藏 0 赞 0 分享

PHP使Laravel为JSON REST API返回自定义错误的问题

这篇文章主要介绍了PHP使Laravel为JSON REST API返回自定义错误的问题,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php 读取文件夹下所有图片、文件的实例

今天小编就为大家分享一篇php 读取文件夹下所有图片、文件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

PHP使用glob方法遍历文件夹下所有文件的实例

今天小编就为大家分享一篇PHP使用glob方法遍历文件夹下所有文件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Laravel关联模型中过滤结果为空的结果集(has和with区别)

这篇文章主要介绍了Laravel关联模型中过滤结果为空的结果集(has和with区别),需要的朋友可以参考下
收藏 0 赞 0 分享

django中的ajax组件教程详解

Ajax(Asynchronous Javascript And XML)翻译成英文就是“异步Javascript和XML”。这篇文章主要介绍了django中的ajax组件的教程 ,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多