-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Use RefreshDatabase create Call to a member function beginTransaction() on null error #1475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Did you fixed it? @theminer3746 |
Would love to know if some of you achieved a solution... |
@lscarneiro I created a trait that resets the db <?php
namespace Tests;
use DB;
use Jenssegers\Mongodb\Schema\Blueprint;
trait RefreshDatabase
{
/**
* Set up function.
*/
public function setUp(): void
{
parent::setUp();
if ($this->hasDependencies()) {
return;
}
$this->dropAllCollections();
}
/**
* Drop all collections.
*/
protected function dropAllCollections(): void
{
$mongo = DB::connection('mongodb');
foreach ($mongo->listCollections() as $collection) {
if (starts_with($name = (string) $collection->getName(), 'system')) {
continue;
}
(new Blueprint($mongo, $name))->drop();
}
}
} |
Thanks @LucasLeandro1204. I wanted a more "generic" approach, so what I did was create a Trait in app\Traits (new folder in my case) with:
And in my test class I use both
That tip I got from this StackOverflow post I consider it a Hack, because it does not solve the original problem, which is execute transactions with this MongoDB Laravel driver. The code from |
Just thinking a little further. An even cleaner approach is to instead of overriding Just tested here and it works like a charm! in
And on the test class:
That way we're achieving the exact "hack" that we want, skip the transaction part (that breaks the test). I created this Gist containing an example of implementation of the Hack |
Just discovered that in my case, using |
@lscarneiro , how to override |
@prashant-pokhriyal sorry for the delay. If you're talking about the specific Which behavior are you targetting specifically? |
@lscarneiro Actually I'm using a library which internally uses Transactions. Since laravel-mongodb does not have Transaction, it throws an error: |
I don't think I have a solution for your case, maybe you could try some help with the library issues page, here in my case I didn't change laravel-mongodb code itself, but rather the code that "depends" on transactions per se. |
Any news on this? |
When testing database laravel's documentation suggest using
RefreshDatabase
trait.But that cause
Error: Call to a member function beginTransaction() on null
The text was updated successfully, but these errors were encountered: