How to properly close a RedBean database connection?

997 views
Skip to first unread message

Patrick

unread,
Jun 30, 2014, 1:00:11 AM6/30/14
to redbe...@googlegroups.com
I'm using RedBean and PHPUnit for my unit testing.

In the setup for each test class, I've got:

R::setup( 'sqlite::memory:');

Some classes also use another database, so I use:

R::addDatabase( 'active', ...)

In the teardown, I've got:

R::close();

I'm getting an error when running all tests at the setup line stating:

RedBeanPHP\RedException: A database has already be specified for this key.

This leads me to two questions (sorry if I'm missing something obvious):

1. Is there another way that I should/can close the database connection so that I can create a new default one?  The code shows that R::close closes the DB adaptor but it doesn't seem to touch the toolboxes array.
2. Is there an easy way to check to see if the default toolbox has already been created?

I was able to get around it with the following but was assuming something else might be cleaner:

        try {
            R::setup( 'sqlite::memory:' );
        }
        catch ( RedBeanPHP\RedException $e ) {
            R::nuke();
        }

Thanks.

Marcio Almada

unread,
Jun 30, 2014, 1:09:17 AM6/30/14
to redbe...@googlegroups.com
Some options:

Put `R::setup( 'sqlite::memory:');` inside `setupBeforeClass` static method and put R::nuke(); at `tearDown` method.

Or you can just activate phpunit process isolation (very slow)


--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To post to this group, send email to redbe...@googlegroups.com.
Visit this group at http://groups.google.com/group/redbeanorm.
For more options, visit https://groups.google.com/d/optout.

Patrick

unread,
Jun 30, 2014, 10:12:34 AM6/30/14
to redbe...@googlegroups.com, marc...@gmail.com
Thank you.  I actually tried that first and was getting the same result since several tests were using the approach.

I was not familiar with the phpunit process isolation.  Seems that could be another solution but is probably even uglier than what I came up with.

I would think that it would be a clean way to close a RedBean database connection although I readily admit that I don't see myself using it that much.

Marcio Almada

unread,
Jun 30, 2014, 11:59:45 AM6/30/14
to redbe...@googlegroups.com
Process isolation is not an ugly hack, it's quite well supported. Try to add `processIsolation="false"` to your phpunit.xml or use the --process-isolation flag. The only problem is that it can make your test suite slow :)


Patrick

unread,
Jul 2, 2014, 1:16:15 AM7/2/14
to redbe...@googlegroups.com, marc...@gmail.com
Sorry, wasn't meaning to say that process isolation is an ugly hack.

Was meaning that I would think there would be a clean way to close the default RedBean database connection so that a new default connection could be opened later.  I.e., something you could easily call after each test (e.g., in tearDown() ).  Was hoping I was just missing something on figuring out how to do that.

Marcio Almada

unread,
Jul 2, 2014, 1:56:51 AM7/2/14
to redbe...@googlegroups.com
@Patrick,

The only other way I see is to reset all connections from facade like this `R::$toolboxes = []` inside tearDown method. But it's currently not possible because $toolboxes is protected. I'm happy with process isolation but would love to be able to "reboot" the facade too.

You could try a pull request to solve this issue: https://github.com/gabordemooij/redbean/issues/384

:D

Cheers!


2014-07-02 2:16 GMT-03:00 Patrick <patra...@gmail.com>:
Sorry, wasn't meaning to say that process isolation is an ugly hack.

Was meaning that I would think there would be a clean way to close the default RedBean database connection so that a new default connection could be opened later.  I.e., something you could easily call after each test (e.g., in tearDown() ).  Was hoping I was just missing something on figuring out how to do that.

--
Reply all
Reply to author
Forward
0 new messages