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

2025-05-27 0 76

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

测试目录

?

1

2

3

4

5

6

7

8

9

10
test

├── TestCase.php

├── bootstrap.php

├── controller

│ ├── BaseControllerTest.php

│ └── IndexControllerTest.php

├── model

├── phpunit.xml

└── service

└── TokenServiceTest.php

phpunit.xml

?

1

2

3

4

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

?

1

2

3
define("APP_PATH", realpath(dirname(__FILE__) . '/../'));

date_default_timezone_set("Asia/Shanghai");

define("TEST_DIR", __DIR__);

TestCase.php 测试文件基础类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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

}

}

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

原文链接:http://blog.csdn.net/xiaoxiong_web/article/details/78900463

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解Yaf框架PHPUnit集成测试方法 https://www.kuaiidc.com/71670.html

相关文章

发表评论
暂无评论