Yaf 프레임워크 PHPUnit 통합 테스트 방법의 자세한 예

小云云
풀어 주다: 2023-03-18 16:54:01
원래의
1653명이 탐색했습니다.

이 기사에서는 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__) . &#39;/../&#39;));
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(&#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 서비스 클래스 예


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 컨트롤러 클래스 예


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);
  }
}
로그인 후 복사

관련 권장 사항;

PHPYaf 실행 프로세스 소스 코드

PHP 7에서 Swoole 및 Yar, Yaf를 설치하는 방법에 대한 튜토리얼 공유

Yaf의 Hello World 예시

위 내용은 Yaf 프레임워크 PHPUnit 통합 테스트 방법의 자세한 예의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!