Detailed example of Yaf framework PHPUnit integration testing method

小云云
Release: 2023-03-18 16:54:01
Original
1664 people have browsed it

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
Copy after login

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>
Copy after login

bootstrap.php test framework entry file


define("APP_PATH", realpath(dirname(__FILE__) . &#39;/../&#39;));
date_default_timezone_set("Asia/Shanghai");
define("TEST_DIR", __DIR__);
Copy after login

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;
  }
}
Copy after login

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

}
Copy after login

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);
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!