Testing PHP MySQL function blocks

I need to check a series of functions that I created using PHP 5 that perform the necessary actions such as the CRUD database (SELECT, UPDATE, INSERT, DELETE) that my web application requires.

I looked at PHP module testing kits, such as Simple Test and PHP Unit, which seem to offer what I need, but I'm not sure how I should achieve this, since separation of equivalence and border analysis are not so clear. Do I just need to enter different variables and change this? This seems pointless since a line that is different may not make any difference.

Any advice on this would be helpful since I haven't come across this before.

+4
source share
2 answers

If you are testing the interaction between your PHP code and your MySQL database, you are doing integration testing, not unit testing.

Below are examples of testing integration with the Improve PHP framework ; it checks the repository class that saves and retrieves the Tenant object.

Instead of working with a pre-populated database, it runs on a completely empty database and creates and destroys tables, as it happens, using a simple table helper. This eliminates the dependence on specific data in the correct state in the test database, which is difficult to save in a 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); } } ?> 
+3
source

Typically, the idea with unit testing is that if you make changes, you can simply run a simple series of tests so as not to disrupt existing functions. Thus, you will need to consider the typical data you expect, as well as border / bound cases, which may include quotation marks (to make sure they are escaped properly), SQL injection attacks (same thing), blank lines , strings of different encoding, NULL, boolean true , etc. Each test should verify that, given the data you entered, you get the expected result (in this case (respectively): inserted row, inserted row with escaped row, inserted blank row, changed or selected another encoding, and then inserted , the error is selected to NULL, the string "true" is inserted, etc.

I did not use the test environment after a few years, but I remember that I have good results with PHPUnit.

+1
source

Source: https://habr.com/ru/post/1347324/


All Articles