<?php class MyTestClass extends PHPUnit_Framework_TestCase { /** * Testing the answer to “do you love unit tests?” */ publicfunction testDoYouLoveUnitTests() { $love=true; $this->assertTrue($love); } } ?>
可以看到,其实一段以/** **/为标记的文字,就可以认为是一种Annotations,但Annotations其实不单单是简单的注释,它是与一个程序元素相关联信息或者元数据的标注,它不影响程序的运行,但相关的软件工具或框架能够将其转换成特殊的元数据标记,以方便开发者以更少的代码去提高效率(比如通过。如果你熟悉Java,则会发现在Java SE 5中及象Spring等框架中,都大量使用了Annotations。
<?php require_once'/path/to/MyMathClass.php'; /** * Test class for MyMathClass. * Generated by PHPUnit on 2011-02-07 at 12:22:07. */ class MyMathClassTest extends PHPUnit_Framework_TestCase { /** * @var MyMathClass */ protected$object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protectedfunction setUp() { $this->object=new MyMathClass; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protectedfunction tearDown() { } /** * @todo Implement testAddValues(). */ publicfunction testAddValues() { // Remove the following lines when you implement this test. $this->markTestIncomplete( 'This test has not been implemented yet.' ); } } ?>
<?php class MyMathClass { /** * Add two given values together and return sum * @assert (1,2) == 3 */ publicfunction addValues($a,$b) { return$a+$b; } } ?>
<?php /** * Testing enforcing the type to "array" like the "enforceTypes" * method does via type hinting */ publicfunction ttestReallyLongRunBuilderConstraint() { $stub=$this->getMock('Database',array('reallyLongTime')); $stub->expects($this->any()) ->method('reallyLongTime') ->with($this->isType('array')); $arr=array('test'); $this->assertTrue($stub-> reallyLongTime ($arr)); } ?>