2

In a php project that I maintain I have a database structure without migrations, hence no way to reproduce it or make on the fly a test database.

And the queries used to be performed on them are rather complex ones. The pattern in code used with them is (I try to show a generelized preview of the problem):

public function someMethod(PDO $connection, string $param) {

  $sql="^some complex sql query^";
  $pdo->prepare($sql);
  $pdo->bindParam('param',$param);
  $stmt->execute();

  return $stmt->fetchAll();
}

In order to test this function I created a duplicate database and from that I deleted all the production data keeping the least minimal. I use the following testing approach when I write phphunit test

  1. Populate the database with test data.
  2. Run the method.
  3. Do my Assertions.
  4. Delete the test data manually using Delete sql statements.

Did this approach creates a Unit test for my application or an Integration one. Keep in mind that the whole logic is implemented in an SQL query instead on the php layer.

For example a query like that:

Select
    value,
    CASE 
     WHEN "time"::time < '12:00' and "time"::time > '00:00' then true 
    ELSE 
      false end as observed_in_morning  
from 
  observations

The example above the logic that indicates whether an observation happened in me morning or not is written on the sql statement, clearly a business logic.

So a test testing whether an observation happened in the morning is a unit test or an integration one?

  • Its an integration test because it needs the database AND the code – Ewan Jan 2 at 15:55
7

Purists won't call any test a "unit test" which involves a database. Other purists won't call a test an "integration test" when there are not at least two components involved (and a platform like the SQL engine of the DB usually does not count as a component).

And some not so puristic people call everything a unit test which gets started by an automatic testing tool like "phpunit". Other people call everything an integration test which does involve a database.

Honestly, these names don't really matter as long as in the group of people you are working with you don't create too much confusion. If you want to be safe, call this neither unit test nor integration test, but an automated functional test.

  • Just don't call it that in front of functional programmers :) – candied_orange Jan 2 at 17:51
  • @candied_orange: I guess you are aware that functional test is an idiomatic phrase which is not directly related to "functional programming"? – Doc Brown Jan 2 at 22:37
  • 1
    yes, that is the joke. – candied_orange Jan 2 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.