I would like to pass an argument as to define how many records I want to create during database seeding, without having to edit the factory manually.
I have tried different variations on php artisan db:seed --class=UsersTableSeeder [using different args here]
I can't seem to find any documentation, so I don't know if that functionally exists. Does something like that exist?
class UsersTableSeeder extends Seeder
{
public $limit = null;
public function __construct($limit = 1) {
$this->limit = $limit;
}
public function run()
{
echo $this->limit;
}
}
There is no way to directly specify an argument.
If you want to specify a parameter via the command line, you could use an environment variable.
class UsersTableSeeder extends Seeder
{
public function run()
{
$limit = env('SEEDER_LIMIT', 1);
echo $this->limit;
}
}
Call like this:
SEEDER_LIMIT=10 php artisan db:seed --class=UsersTableSeeder
You can set it up this way:
public function run($count = 1)
And then you can pass the argument this way:
$this->call(ClientSeeder::class, false, ['count' => 500]);
From what I know there's no such thing as parameters for seeders, but you could implement it yourself. You could create a new command which accepts parameters and calls a seeder programmatically with those additional parameters.
Something like this should do the trick:
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
public function run(int $limit)
{
echo $limit;
// Seed some stuff
}
}
namespace App\Console\Commands;
use Illuminate\Console\Command;
use UsersTableSeeder;
class SeedCommand extends Command
{
protected $signature = 'app:seed {limit}';
public function handle(UsersTableSeeder $seeder)
{
$limit = $this->argument('limit');
$seeder->run($limit);
}
}
you can ask for that limit before call any other seeders using
// DatabaseSeeder.php
$limit = $this->command->ask('Please enter the limit for creating something !!');
and then you can pass that limit to any additional seeders from 'DatabaseSeeder' like this
//DatabaseSeeder.php
$this->call(AnyAdditionalSeeder::class, false, compact('limit'));
then in 'AnyAdditionalSeeder' you can add parameter and name it $limit to the run() method like this
public function run($limit)
{
// you can access limit variable here
}
then when you run the command php artisan db:seed it will ask you for the limit :)
my solution:
// MemberSeeder.php (example for Members Table)
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Member as ModelsMember;
class MemberSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
static function run(int $nRec=1) {
ModelsMember::factory()->times($nRec)->create();
}
}
Call from a .php file
// Members.php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Member;
use Database\Seeders\MemberSeeder;
class Members extends Component
{
public $members, $name, $email, $phone_number, $status, $member_id;
public $bldModal = '';
...
...
public function generaRecords() {
MemberSeeder::run(2);
}
}
Schema for to create table
Schema::create('members', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('phone_number');
$table->char('status',1);
$table->timestamps();
});
Shortly: to change
public function run() {
To
static function run(int $nRec=1) {
As of Laravel 8 you can use callWith to pass parameters to the run method of your seeders. Something like this:
class UsersTableSeeder extends Seeder
{
public function run($count = 1)
{
User::factory()->count($count)->create();
}
}
And then your could use those seeders in other seeders like this:
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->callWith(UsersTableSeeder::class, ['count' => 10]);
}
}
Note that the parameter array you give to callWith is associative, and its keys should match the run method's parameters, because the call will ordinarily be resolved through the Laravel's application container.
you can pass a parameter as quantity to a seeder like this:
First, create a custom command
php artisan make:command generateFakeCompanyData
generateFakeCompanyData.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Database\Seeders\CreateFakeCompanySeeder;
class generateFakeCompanyData extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'create:fake-comapnies {count}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle(CreateFakeCompanySeeder $seeder)
{
$limit = $this->argument('count');
$seeder->run($limit);
}
}
create seeder file:
php artisan make:seeder CreateFakeCompanySeeder
CreateFakeCompanySeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class CreateFakeCompanySeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run(int $limit)
{
\App\Models\Company\Company::factory($limit)->create();
}
}
create factory file
php artisan make:factory Company\CompanyFactory --model=Company
CompanyFactory.php
<?php
namespace Database\Factories\Company;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Blog;
/**
* #extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Company\Company>
*/
class CompanyFactory extends Factory
{
/**
* Define the model's default state.
*
* #return array<string, mixed>
*/
public function definition()
{
return [
'name' => $this->faker->company,
'email' => $this->faker->unique()->email,
'logo' => $this->faker->imageUrl(640,480),
'website' => Str::slug($this->faker->name).'.com',
// 'website' => $this->faker->text(),
];
}
}
in route: web.php
Route::controller(CompanyController::class)->group(function() {
Route::prefix('company')->group(function () {
Route::post('/store', 'companyInsert')->name('company.add');
});
});
in controller: CompanyController.php
class CompanyController extends Controller{
public function companyInsert(Request $request){
$limit = $request->no_of_company;
\Artisan::call('create:fake-comapnies '.$limit);
return redirect()->back()->with('crudMsg','Total of '.$limit.' Company
Successfully Added');
}
}
I am using Laravel 5.1 and I have the following test class where I am using some model factories in the setUp() method to temporarily seed the database.
class PendingUserWithManualStudentDetailsTest extends TestCase
{
use DatabaseMigrations, WithoutMiddleware;
public function setUp()
{
parent::setUp();
$this->studentDetail = factory(StudentDetail::class)->create();
// ... more factories
}
// ... some tests
}
When running the test I get the following error:
General error: 1 no such table: student_details
When I move the factory calls into a test method they execute just fine. So I am guessing that the order in which the setUp() and my test methods are called are causing the issue. Is there any way I can fix this?
This is my testcase class:
class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* #var string
*/
protected $baseUrl = 'http://myapp.dev';
/**
* Creates the application.
*
* #return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app->setLocale('en');
return $app;
}
}
Currently, I am trying to run some simple unit tests with phpunit on a laravel project with the command line phpunit tests/Unit/ExampleTest. But it is only running the first method. Anyone has a solution ? Thank you !
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
/**
* An other basic test
*
* #return void
*/
public function secondTestBasicTest()
{
$this->assertTrue(true);
}
public function basicTest(){
$response = $this->action('GET', 'DepartementsController#display', ['id' => 1]);
$view = $response->original;
$this->assertEquals(1, $view['$dpt->iddepartement']);
}
}
PHPUnit by default launches only methods beginning with test prefix, so secondTestBasicTest and basicTest do not fall under that condition. You can explicitly annotate your tests as #test in PHPDoc to override this behavior:
/**
* #test
*/
public function basicTest() {
...
}
Usually when seed my database it tells me which table I'm seeding on the command line but because I am passing an argument to my seeder the message has disappeared (makes debugging really hard because I don't know which seeder it's on when it breaks).
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class SeedTestLargeData extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
// Default
$this->call(UsersSeeder::class, 'large_test_data');
Model::reguard();
}
public function call($class, $data_filename = null) {
$this->resolve($class)->run($data_filename);
}
}
Any idea of how to get it back?
Change your call() method like this:
public function call($class, $data_filename = null)
{
$this->resolve($class)->run($data_filename);
if (isset($this->command)) {
$this->command->getOutput()->writeln("<info>Seeded:</info>".$class);
}
}
In the original Seeder class call method has an additional statement, try to add it:
public function call($class) {
//...
if (isset($this->command)) {
$this->command->getOutput()->writeln("<info>Seeded:</info> $class");
}
}
I am using Laravel 5.0 to create phpunit test alongside the actual model.
I get errors in phpunit tests but no errors when controller calls the model and it returned the desired data.
sample.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class sample extends Model {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'sample';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['id','username','details','image'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
public static function test()
{
return "Returned Text.";
}
public static function gettest()
{
return self::test();
}
public static function getItem()
{
return self::orderBy('username','asc')->get();
}
public static function findItem($id)
{
return self::find($id);
}
}
SampleTest.php
<?php namespace App;
use Mockery as m;
class SampleTest extends \PHPUnit_Framework_TestCase {
protected function setUp()
{
$this->mock = m::mock('App\sample')->makePartial();
}
protected function tearDown()
{
m::close();
}
/** #test */
public function should_return_string()
{
$response = $this->mock->test();
var_dump("test() returns :".$response);
}
/** #test */
public function should_return_string_from_test_function()
{
$response = $this->mock->gettest();
var_dump("gettest() returns :".$response);
}
/** #test */
public function should_return_mocked_data()
{
$this->mock->shouldReceive('test')->andReturn('Return Mocked Data');
$response = $this->mock->gettest();
var_dump("gettest() returns :".$response);
}
/** #test */
public function should_return_some_data_using_this_mock()
{
$this->mock->shouldReceive('get')->andReturn('hello');
$response = $this->mock->getItem();
}
}
Problem
When I use controller to call the model, it returned the desired data.
When I run phpunit on command prompt:-
test function is not mocked properly as it still returns the original string
getItem and findItem functions return an error saying
1) App\SampleTest::should_return_some_data_using_this_mock
BadMethodCallException: Static method Mockery_0_App_sample::getItem()
does not exist on this mock object
Question
How can I mock the function properly? Why it is saying the error code as shown above? Where was I doing it wrong?
Any help will be much appreciated.
Note: Test assertions is removed and replaced with var_dump to see the output on the command prompt.