Yaf中PHPUnit单元测试最佳实践

public同级目录建立tests目录

tests/phpunit.xml

<phpunit bootstrap="./index.php">
	<testsuites>
		<testsuite name="controllers">
			<file>./controllers/TestIndex.php</file>
		</testsuite>
	</testsuites>
</phpunit>

tests/index.php

<?php
define ( 'APP_PATH', dirname ( __DIR__ ) . '/app', true );
(new Yaf_Application ( APP_PATH . '/conf/app.ini' ))->bootstrap ();

tests/controllers/TestIndex.php

class TestIndex extends PHPUnit_Framework_TestCase {
	public function testIndex() {
		$request=new Yaf_Request_Simple('CLI','','Index','test');
		$res=Yaf_Application::app()->getDispatcher()->returnResponse(true)->dispatch($request);
		$valid='test string';
		$this->assertEquals ( $valid, $res->getBody());
	}
}

APP_PATH.'/controllers/Index.php'

class IndexController extends Yaf_Controller_Abstract {
	function testAction(){
		$this->getResponse()->setBody('test string');
	}
}

运行

[root@ares tests]# phpunit 
PHPUnit 4.8.6 by Sebastian Bergmann and contributors.

.

Time: 105 ms, Memory: 4.00Mb

OK (1 test, 1 assertion)

 

参考:

http://www.crackedzone.com/phpunit-yaf-controller.html

发表评论

电子邮件地址不会被公开。

*