您的位置:软件测试 > 开源软件测试 > 开源单元测试工具 > PHPUnit
采用SimpleTest进行PHP单元测试
作者:网络转载 发布时间:[ 2013/4/3 14:08:40 ] 推荐标签:

文件操作类 file.class.php

    <?php
    class File
    {
        var $filename;
       
        function File($file)
        {
            if (emptyempty($file)) {
                die('Filename should not be empty');
            }
           
            $this->filename = $file;
           
            if (!$this->exists() && !$this->create())   die('Unable to find / create the file');
        }
       
        function exists()
        {
            return file_exists($this->filename);
        }
       
        function create()
        {
            if ($this->exists())    return true;
            return $this->putContents(null);
        }
       
        function getContents()
        {
            return @file_get_contents($this->filename);
        }
       
        function putContents($content)
        {
            $fp = @fopen($this->filename, 'wb');
            $write = @fwrite($fp, $content);
            @fclose($fp);
           
            return !($write === false);
        }
       
        function append($content)
        {
            $fp = @fopen($this->filename, 'ab');
            $append = @fwrite($fp, $content);
            @fclose($fp);
           
            return !($append === false);
        }
       
        function delete()
        {
            return @unlink($this->filename);
        }
    }
    ?>

单元测试类 fileTest.php

    <?php
    //include_once '../simpletest/unit_tester.php';
    //include_once '../simpletest/reporter.php';
    include_once('../simpletest/autorun.php');
    include_once 'file.class.php';
    class fileTest extends UnitTestCase
    {
        var $file;
        var $filename;
       
        function fileTest($filename)
        {
            $this->filename = $filename;
            $this->file = new File($this->filename);
            $this->UnitTestCase('File Manipulation Test');
        }
       
        /**
         * 描述:该函数将会在每次测试开始前自动调用,
         * 在编写测试类的时候,是个可选函数,可写不可不写
         */                            
        function setUp()
        {
            $fp = @fopen($this->filename, 'x');
            if (!($fp == false)) {
                fwrite($fp, '');
                fclose($fp);
            }
        }
       
        /**
         * 描述:该函数将会在每次测试结束后自动调用,
         * 在编写测试类的时候,是个可选函数,可写不可不写
         */ 
        function tearDown()
        {
            @unlink($this->filename);
        }
       
        function testFileExists()
        {
            $this->assertTrue($this->file->exists());
            clearstatcache();
            $this->assertTrue(file_exists($this->file->filename));
        }
       
        function testCreate()
        {
            $this->tearDown();
            $this->assertFalse(file_exists($this->file->filename));
            clearstatcache();
           
            $this->assertTrue($this->file->create());
            $this->assertTrue(file_exists($this->file->filename));
        }
       
        function testReadWrite()
        {
            $contents = 'content';
            $this->assertTrue($this->file->putContents($contents));
            $this->assertEqual($this->file->getContents(), $contents);
        }
       
        function testAppend()
        {
            $this->file->putContents('SimpleTest');
            $this->assertTrue($this->file->append('NeverMind'));
            $this->assertWantedPattern('~nevermind$~i', $this->file->getContents());
        }
       
        function testDelete()
        {
            $this->assertTrue($this->file->delete());
            $this->assertFalse(file_exists($this->filename));
        }
    }
    $test = new fileTest('test.txt');
    $test->run(new HtmlReporter());
    ?>

软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd