<?php class RemoteConnect { public function connectToServer($serverName = null) { if ($serverName == null) { throw new Exception("That's not a server name!"); } $fp = fsockopen($serverName, 80); return ($fp) ? true : false; } public function returnSampleObject() { return $this; } }
新建单元测试类文件test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php require_once('sample.php'); class RemoteConnectTest extends PHPUnit_Framework_TestCase { public function setUp() { } public function tearDown() { } public function testConnectionIsValid() { // test to ensure that the object from an fsockopen is valid $connObj = new RemoteConnect(); $serverName = 'www.a.com'; $this->assertTrue($connObj->connectToServer($serverName) == false); } }