I tried to make a unit test that makes a database query, but any time I run php artisan test
, PHP starts consuming memory until there is no free RAM on my virtual server.
For testing I have increase the virtual server RAM up to 16 Gb and it consumes all 16 GB and then crashes!
So I got simple test from a Laravel tutorial project and tried creating a unit test on it.
Here is the Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Traits\Timestamp;
use Laravel\Sanctum\HasApiTokens;
class Student extends Model
{
use HasFactory, HasApiTokens;
protected $table = "students";
protected $fillable = [
"name",
"email",
"password",
"phone_no",
];
public $timestamps = false;
}
And a Unit Test:
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use App\Models\Student;
class ExampleTest extends TestCase
{
public function setup() : void
{
$this->setup();
}
/**
* A basic test example.
*
* @return void
*/
public function test_example()
{
$st = new Student();
$st->select("*")->get();
$this->assertTrue(true);
}
}
And the testing result:
Symfony\Component\Process\Exception\ProcessSignaledException
The process has been signaled with signal "9".
at vendor/symfony/process/Process.php:441
437▕ usleep(1000);
438▕ }
439▕
440▕ if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
➜ 441▕ throw new ProcessSignaledException($this);
442▕ }
443▕
444▕ return $this->exitcode;
445▕ }
15 vendor frames
16 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
I tried many thing, change settings and etc, but nothing helped!
Update 1
Just for information:
php -v
PHP 8.1.10 (cli) (built: Sep 18 2022 12:52:19) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies
with Zend OPcache v8.1.10, Copyright (c), by Zend Technologies
Update 2 I create simple minimal project:
https://github.com/vasiliyaltunin/LaravelPhpUnitDatabase
So you can test it yourself, maybe there PHP version problem or other things i try everything already!
Update 3
Thansk to all problem resolved!
I hope test project help ppls to understand how database testing works.
I updated test project by adding seeder for table and use RefreshDatabase, so on every tests run it will be wipe and reseed tables!
You can remove use RefreshDatabase; if you dont want to loose you data every time!
CodePudding user response:
Your setup method is creating an infinite loop:
public function setup() : void
{
$this->setup(); // calling itself infinite times
}
Probably you want to call parent::setup()
instead:
public function setup() : void
{
parent::setup();
}