Connecting to Multiple Databases Using Acceptance Tests Using Code

I am trying to figure out how to connect to multiple databases using Codeception. I even tried to instantiate a new PDO instance directly, but then the command codept run

ends abruptly with no error message.

I can connect to one database using the Db module to use features like $I->seeInDatabase()

, but I'm stuck trying to connect to two. Attempting to reconfigure the Db module at runtime with $this->getModule('Db')->_reconfigure()

also just stops the tests without a message.

Any ideas?

+3


source to share


3 answers


I think this is what you want: https://github.com/Codeception/Codeception/issues/1634 ... but its not implemented yet - maybe you can push this issue?



+2


source


Well! if you are not working with database connections you can do this, which I implemented once in symfony when using code.

  • Hijacking Doctrine with

    $doctrine = $I->grabService('Doctrine').
    
          

  • Now grab the database connection as you pointed out in the ORM section of the doctrine.

    $connection = $doctrine->getConnection('DB_Name_as_in_ORM')
    
          

  • You are now connected to the required database and you can now perform queries on a table in that database using the dbal connection doctrine methods.


I think this is what you were looking for, maybe!

0


source


I was able to achieve this with the following:

Package config file:

actor: API_Tester
lint: true
colors: true
modules:
    enabled:
        - \Helper\Api
        - Asserts
        - REST:
            url: http://localhost
            depends: PhpBrowser
            part: Json
            headers:
              Accept: application/json
              Content-Type: application/json
        - Db:
            dsn: 'mysql:host=localhost;dbname=DATABASE1'
            user: 'DBUSER'
            password: 'DBPASSWORD'
            dump: 'tests/populate-DATABASE1.sql'
            populate: true
            cleanup: false
            reconnect: true
            populator: 'mysql -u $user -p$password -h $host $dbname < $dump'
            databases:
              DATABASE2_USE_THIS_NAME_IN_YOUR_CODE:
                dsn: 'mysql:host=localhost;dbname=DATABASE2'
                user: 'DBUSER'
                password: 'DBPASSWORD'
                dump: 'tests/populate-DATABASE2.sql'
                populate: true
                cleanup: false
                reconnect: true
                populator: 'mysql -u $user -p$password -h $host $dbname < $dump'

      

PHP test code:

        //Connect to the second database
        $I -> amConnectedToDatabase ( 'DATABASE2_USE_THIS_NAME_IN_YOUR_CODE' );
        $I -> seeNumRecords ( 1, 'users' );
        $I -> seeInDatabase ( 'users', [ 'email' => 'john@example.com' ] );
        //Connect back to the default database
        $I -> amConnectedToDatabase ( Codeception\Module\Db::DEFAULT_DATABASE );
        $I -> seeNumRecords ( 1, 'sessions' );
        $I -> seeInDatabase ( 'sessions', [ 'uniqueness' => $this -> uuid ] );

      

Result:

I am connected to database "DATABASE2_USE_THIS_NAME_IN_YOUR_CODE"
I see num records 1,"users"
I see in database "users",{"email":"john@example.com"}
I am connected to database "default"
I see num records 1,"sessions"
I see in database "sessions",{"uniqueness":"635c798c-05cd-4d9d-b79a-9643...}

      

0


source







All Articles