CodeIgniter Forums
Testing, specifically when having database dependant code - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: Testing, specifically when having database dependant code (/thread-74182.html)



Testing, specifically when having database dependant code - tgix - 08-14-2019

Swimming towards the deep end of the pool and now learning more about testing. 
In CI3 I used kenji's ci-phpunit-test with much success. Database testing has been made with a mysql server with a downloaded copy of the latest live database and making sure I only run non-destructive tests.


With CI4 I try to do the same, but find the following line from the $tests structure in Config\Database.php:67 a bit strange:
PHP Code:
'DBPrefix' => 'db_' // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE. 
 
I have already nuked my DB twice by setting this to blank so I'm really missing something here ;-)


RE: Testing, specifically when having database dependant code - albertleao - 08-14-2019

Couldn't you mock out your db calls?


RE: Testing, specifically when having database dependant code - tgix - 08-15-2019

(08-14-2019, 01:24 PM)albertleao Wrote: Couldn't you mock out your db calls?

I guess so, but wouldn't that defeat the purpose of testing, specially on Controller level?

My current approach is to use the seed function to copy the few tables I need to do testing on from the database backup (virtu12_vtester)  to the testing database (ci4test). Both databases live on the same MySQL server and ci4test user has read access to the database backup:
PHP Code:
class CITestSeeder extends Seeder
{
    public function 
run()
    {
        
$tables = [ 'country''territories' ];
        foreach (
$tables as $t)
        {
            if ( ! 
$this->db->tableExists('db_' $t))
            {
                
$this->db->query("CREATE TABLE IF NOT EXISTS ci4test.db_$t SELECT * FROM virtu12_vtester.$t");
            }
        }
    }




RE: Testing, specifically when having database dependant code - albertleao - 08-15-2019

it depends on what kind of testing you're doing. If you're doing unit tests, then you should be mocking your db, if you're doing integration tests then yea you need to use your db.


RE: Testing, specifically when having database dependant code - tgix - 08-15-2019

(08-15-2019, 07:33 AM)albertleao Wrote: it depends on what kind of testing you're doing. If you're doing unit tests, then you should be mocking your db, if you're doing integration tests then yea you need to use your db.

True. But integration testing needs to be done before the users hit the system.


RE: Testing, specifically when having database dependant code - kilishan - 08-15-2019

(08-14-2019, 02:45 AM)tgix Wrote: With CI4 I try to do the same, but find the following line from the $tests structure in Config\Database.php:67 a bit strange:
PHP Code:
'DBPrefix' => 'db_' // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE. 
 
I have already nuked my DB twice by setting this to blank so I'm really missing something here ;-)

Sorry - that note was for internal tests of the framework. If you're not running those, and just running the ones for your app, then change the prefix to whatever you need it to be.

( I should probably change that comment Smile )


RE: Testing, specifically when having database dependant code - tgix - 08-15-2019

(08-15-2019, 12:34 PM)kilishan Wrote:
(08-14-2019, 02:45 AM)tgix Wrote: With CI4 I try to do the same, but find the following line from the $tests structure in Config\Database.php:67 a bit strange:
PHP Code:
'DBPrefix' => 'db_' // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE. 
 
I have already nuked my DB twice by setting this to blank so I'm really missing something here ;-)

Sorry - that note was for internal tests of the framework. If you're not running those, and just running the ones for your app, then change the prefix to whatever you need it to be.

( I should probably change that comment Smile  )

An extra note that the tests are deleting the tables with this prefix after completion maybe in place (in CIDatabaseTestCase::tearDown()).
If DBPrefix is blank this will delete all tables.