doulian7305 2011-04-09 00:58
浏览 52
已采纳

PHP MySQL功能单元测试

I need to test a number of functions that I have created using PHP 5 which carry out the required database CRUD type actions (SELECT, UPDATE, INSERT, DELETE) which are required by my web application.

I have been looking at PHP unit testing suites such as Simple Test and PHP Unit which seem to offer what I need however I am unsure how I am meant to achieve this, as equivalence partitioning and boundary analysis isn't all that clear. Do I just need to input different variables and vary this? This seems rather pointless as a string that is different may not necessarily make any difference.

Any guidance on this would be helpful as I have not encountered this before.

  • 写回答

2条回答 默认 最新

  • duanli9930 2011-08-29 16:41
    关注

    If you are testing the interaction between your PHP code and your MySQL database, you are performing integration testing, rather than unit testing.

    Here are some examples of integration testing with Enhance PHP framework, it tests a repository class that saves and retrieves a Tenant object.

    Instead of running against a pre-populated database, it runs on an entirely empty database and creates and destroys the tables as it goes using a simple table helper. This removes the dependency on particular data being in the right state in a test database, which is hard to keep in step.

    <?php
    class TenantRepositoryTestFixture extends EnhanceTestFixture {
        private $Target;
    
        public function SetUp() {
            $tables = new TableHelper();
            $tables->CreateTenantTable();
            $this->Target = Enhance::GetCodeCoverageWrapper('TenantRepository');
        }
    
        public function TearDown() {
            $tables = new TableHelper();
            $tables->DropTenantTable();
        }
    
        public function SaveWithNewTenantExpectSavedTest() {
            $tenant = new Tenant();
            $tenant->Name = 'test';
    
            $saved = $this->Target->Save($tenant);
            $result = $this->Target->GetById($saved->Id);
    
            Assert::AreNotIdentical(0, $result->Id);
            Assert::AreIdentical($tenant->Name, $result->Name);
        }
    
        public function SaveWithExistingTenantExpectSavedTest() {
            $tenant = new Tenant();
            $tenant->Name = 'test';
            $saved = $this->Target->Save($tenant);
            $saved->Name = 'changed';
            $saved = $this->Target->Save($saved);
    
            $result = $this->Target->GetById($saved->Id);
    
            Assert::AreIdentical($saved->Id, $result->Id);
            Assert::AreIdentical($saved->Name, $result->Name);
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b