Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL Function Unit Testing

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.

like image 336
Daniel West Avatar asked Apr 09 '11 00:04

Daniel West


2 Answers

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);
    }
}
?>
like image 174
Fenton Avatar answered Sep 22 '22 02:09

Fenton


Generally the idea with unit testing is to ensure that, if you make a change, you can simply run a simple series of tests to ensure no existing functionality will break. So that said, you'll want to cover the typical data you're expecting, as well as edge/boundary cases, which might include strings with quotes (to verify that they're being escaped properly), SQL injection attacks (same), empty strings, strings of different encoding, NULL, a boolean true, etc. Each test should verify that, given the data you input, you're getting the expected result, in this case (respectively): escaped string inserted, escaped string inserted, empty string inserted, different encoding converted or thrown out then inserted, an error thrown on a NULL value, the string 'true' inserted, etc.

I haven't used either test framework in a few years, but I remember having good results with PHPUnit.

like image 32
Jimmy Sawczuk Avatar answered Sep 22 '22 02:09

Jimmy Sawczuk