PHPUnit : adding mnet hosts in DB not working

PHPUnit : adding mnet hosts in DB not working

by Simon Vart -
Number of replies: 3

Hi,

I don't understand something. 

I'm creating a new set of PHPUnit tests. I need to create some hosts in DB and get their values later in the code.

So in my setUp() I have a line saying : 

$mnethost->id = $DB->insert_record('mnet_host', $mnethost); // $mnethost being an object with the necessary data)

The returned value (mnet host id) is correct.

But when calling get_hosts() in a test later, in another method from antoher class, with : 

$DB->get_records_sql($sql, array($CFG->mnet_localhost_id, $CFG->mnet_all_hosts_id));

it returns an empty array. 

Is there something special with writing / reading DB in PHPUnit ?

And what if I would like to really write in the DB ?

(The same code in production works great)

Average of ratings: -
In reply to Simon Vart

Re: PHPUnit : adding mnet hosts in DB not working

by Rajesh Taneja -

Hello Simon,


Not sure why this might be a problem. Can you please try following snippet to check if this works for you.

<?php
defined('MOODLE_INTERNAL') || die();
class my_testcase extends advanced_testcase {
    public function setUp() {
        global $DB;
        $this->resetAfterTest();
        $chapter = new stdClass();
        $chapter->title = "Test";
        $chapter->pagenum = 1;
        $chapter->subchapter = 0;
        $chapter->content = "Testing";
        $DB->insert_record('book_chapters', $chapter);
    }
    public function test_chapter() {
        global $DB;
        $rs = $DB->get_records_sql("SELECT * FROM {book_chapters}");
        $this->assertCount(1, $rs);
        //var_dump($rs);
    }
}


In reply to Rajesh Taneja

Re: PHPUnit : adding mnet hosts in DB not working

by Simon Vart -

Thanks, that works. The test returns the data inserted.


I think I have find the issue : during the test, i need to populate more tables than just mnet_hosts : I also need to have mnet_host2service, mnet_service and mnet_application to be populated.

I need to code a mock of full mnet environment.


Thanks for your help. I should have seen this (but you test helps me conclude that PHPUnit is working nicely, so the fault is on me !)