Skip to content
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

Adding Factories #106

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions app/Models/Actie.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Jenssegers\Date\Date;
Expand All @@ -11,6 +12,7 @@
class Actie extends Model
{
use Spatial;
use HasFactory;

protected $spatial = ['location'];

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
use HasFactory;
public static function boot()
{
parent::boot();
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Organizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Organizer extends Model
{
use HasFactory;

protected $appends = [
'link',
'website_human',
Expand Down
13 changes: 10 additions & 3 deletions app/Models/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use TCG\Voyager\Traits\Spatial;
Expand All @@ -10,7 +11,8 @@
class Report extends Model
{
use Spatial;

use HasFactory;

protected $spatial = ['location'];

/**
Expand Down Expand Up @@ -80,8 +82,13 @@ public function actie()
}

public function setExterneLinkAttribute($value)
{
$this->attributes['externe_link'] = implode(",", $value);
{
// check if value is array, then implode, else use as is
if (is_array($value)) {
$this->attributes['externe_link'] = implode(",", $value);
} else {
$this->attributes['externe_link'] = $value;
}
}

public function getExterneLinkAttribute($value)
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Theme extends Model
{
use HasFactory;
public static function boot()
{
parent::boot();
Expand Down
130 changes: 130 additions & 0 deletions database/factories/ActieFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Database\Factories;

use App\Models\Actie;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\DB;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Actie>
*/
class ActieFactory extends Factory
{

// Setting model
protected $model = Actie::class;

// Setting dummy types
protected static $titles_map = [
'Klimaatdemo',
'Black Lives Matter',
'Kick Out Zwarte Piet',
'Woonprotest',
'Bio-industrie Protest',
];

protected static $externe_link_map = [
'https://utrecht.nl',
'https://partijvoordedieren.nl',
'https://nijmegen.nl',
];

protected static $location_human_map = [

'De Dam, Amsterdam',
'Jaarbeursplein, Utrecht',
'Museumplein, Amsterdam',
'Valkhofpark, Nijmegen',
];

protected static $slug_map = [
'bio-industrie-protest',
'black-lives-matter-amsterdam',
'black-lives-matter-nijmegen',
'black-lives-matter-utrecht',
'kick-out-zwarte-piet',
'klimaatdemo-amsterdam',
'klimaatdemo-nijmegen',
'klimaatdemo-utrecht',
'woonprotest-amsterdam',
];

protected static $status_map = [
'PUBLISHED',
'DRAFT',
'PENDING',
];

protected static $image_map = [
null,
'acties/pexels-karolina-grabowska-8106775-resize-500.jpg',
'acties/pexels-markus-spiske-3039036-resize-500.jpg',
'acties/pexels-paddy-o-sullivan-2369217-resize-500.jpg',
];

protected static $keywords_map = [
null,
];

protected static $location_map = [
'POINT (5.1065183336479 52.088538312124)',
null,
];

public function definition(): array
{

// Picking a random action name
$actie_name = $this->faker->randomElement(static::$titles_map);

// Fake an address for the location_human field
$location_human = $this->faker->address();

// Extracting Street from location (used to make slug unique)
preg_match("/^\D*(?=\d)/", $location_human, $m);
$pos = isset($m[0]) ? strlen($m[0]) : false;
$street = substr($location_human, 0, $pos);
$street = str_replace(" ", "", $street);

// Calculating the first date
$start_date_obj = $this->faker->dateTimeBetween('-2year', now());
// Calculating the end date from the start date
$end_date_obj = $this->faker->dateTimeBetween($start_date_obj, now());

// Calculating create date from start date
$created_at = $this->faker->dateTimeBetween("-2years", $start_date_obj)->format("Y-m-d H:i:s");
// Calculating updated date from create date
$updated_at = $this->faker->dateTimeBetween($created_at, now())->format("Y-m-d H:i:s");

// Calculating random location and generating point string
$latitude = $this->faker->latitude($min = 51, $max = 53);
$longitude = $this->faker->longitude($min = 4, $max = -7);
$point_string = sprintf("ST_GeomFromText('POINT (%f %f)')", $latitude, $longitude);

return [
// To mantain integrity with other seeds, for example with ReferentiesTableSeeder
'id' => $this->faker->unique()->randomElement([11, 12, 16, 17, 19, 20, 23, 24, 25]),
'user_id' => $this->faker->randomElement([1]),
'title' => $actie_name,
'excerpt' => $this->faker->text(124),
'body' => '<p>' . $this->faker->text(124) . ' <strong>' . $this->faker->text(146) . '</strong> ' . $this->faker->text(236) . '<em>Integer ' . $this->faker->text(34) . ' </em></p>',
'externe_link' => $this->faker->randomElement(static::$externe_link_map),
'start_date' => $start_date_obj->format('Y-m-d'),
'start_time' => $start_date_obj->format('H:i:s'),
'end_date' => $end_date_obj->format('Y-m-d'),
'end_time' => $end_date_obj->format('H:i:s'),
'location' => DB::raw($point_string),
'location_human' => $location_human,
'image' => $this->faker->randomElement(static::$image_map),
'slug' => $actie_name . '-' . $street,
'keywords' => $this->faker->randomElement(static::$keywords_map), // slug fied is unique. This is a provisory solution
'disobedient' => (int) $this->faker->boolean(),
'pageviews' => $this->faker->numberBetween(30, 1000),
'status' => $this->faker->randomElement(static::$status_map),
'created_at' => $created_at,
'updated_at' => $updated_at,
];
}

}
43 changes: 43 additions & 0 deletions database/factories/CategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Category;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
*/
class CategoryFactory extends Factory
{
protected $model = Category::class;
protected static $names_map =[
'Demonstratie',
'Petitie',
];

public function definition(): array
{

# This number is the same in the count parameter in the seeder file
$tot_elements = $this->count;

$name = $this->faker->unique()->randomElement(static::$names_map);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this variable $name is used only once, it might be good to just call $this->faker->unique()->randomElement(static::$names_map) directly in line 37

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using the $name to generate the slug field as well, in the line 38.


#Calculating create date from start date
$create_at_obj = $this->faker->dateTimeBetween( "-2years",now()) ;
$create_at = $create_at_obj->format("Y-m-d H:i:s");

#Calculating updated date from create date
$update_at_obj = $this->faker->dateTimeBetween($create_at_obj,now()) ;
$update_at = $update_at_obj->format("Y-m-d H:i:s");

return [
'created_at' => $create_at,
'id' => $this->faker ->unique()-> numberBetween(1,$tot_elements),
'name' => $name,
'slug' => strtolower($name),
'updated_at' => $update_at,

];
}
}
62 changes: 62 additions & 0 deletions database/factories/OrganizerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Organizer;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Organizer>
*/
class OrganizerFactory extends Factory
{
protected $model = Organizer::class;
protected $qt;
protected static $names_map = [
'Greenpeace',
'Kick Out Zwarte Piet',
'Nederland Wordt Beter',
'Woonprotest',

];

protected static $status_map = [
'PENDING',
'APPROVED',
'REJECTED',
'PUBLISHED'
] ;


public function definition(): array
{
# This number is the same in the count parameter in the seeder file
$tot_elements = $this->count;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the better function to use in line 48 would be $faker->numberBetween(1,$tot_elements), where $tot_elements is the number of instances to be created, passed as parameter in the seed file, in the count() method.

But, I got a exception when I use it, the exception is below transcripted. In the Stackoverflow there was explanation about this error sometimes occur with small datasets. So I changed to function to
$faker->randomElement(), but I will search other ways to solve it in the future and then use the variable $tot_elements.

Exception : " FAILED Tests\Feature\RouteTest > available routes wit… OverflowException
Maximum retries of 10000 reached without finding a unique value
at vendor/fakerphp/faker/src/Faker/UniqueGenerator.php:80"


#Organizer name setting
$organizer_name = $this->faker
->randomElement(static::$names_map);

#Calculating create date from start date
$create_at_obj = $this->faker->dateTimeBetween( "-2years",now()) ;
$create_at = $create_at_obj->format("Y-m-d H:i:s");

#Calculating updated date from create date
$update_at_obj = $this->faker->dateTimeBetween($create_at_obj,now()) ;
$update_at = $update_at_obj->format("Y-m-d H:i:s");

return [
'id' => $this->faker ->unique()
-> randomElement([1,2,3,4]),
'name' => $organizer_name,
'description' => sprintf('<p>.This is a description for %s.</p>',$organizer_name),
'website' => sprintf('https://%s.com',$organizer_name),
'logo' => sprintf('organizers/%s_logo.jpg',$organizer_name),
#slug should be unique
'slug' => $organizer_name
.'-'.str_replace(' ','',$this->faker->unique()->streetName()),
'status' => $this->faker->randomElement(static::$status_map),
'created_at' => $create_at,
'updated_at' => $update_at,
];
}
}
65 changes: 65 additions & 0 deletions database/factories/ReportFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Report;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Report>
*/
class ReportFactory extends Factory
{
protected $model = Report::class;

protected static $status_map = [
'PENDING',
'APPROVED',
'REJECTED',
];

public function definition(): array
{
# This number is the same in the count parameter in the seeder file
$tot_elements = $this->count;

#Calculating the first date
$start_date_obj = $this->faker->dateTimeBetween('-2year ',now()) ;
$start_date = $start_date_obj->format('Y-m-d');
$start_time = $start_date_obj->format('H:i:s');

# Calculating the end date from the start date
$end_date_obj = $this->faker->dateTimeBetween( $start_date_obj,now()) ;
$end_date = $end_date_obj ->format('Y-m-d');
$end_time = $end_date_obj->format('H:i:s');

#Calculating create date from start date
$create_at_obj = $this->faker->dateTimeBetween( "-2years",$start_date_obj) ;
$create_at = $create_at_obj->format("Y-m-d H:i:s");

#Calculating updated date from create date
$update_at_obj = $this->faker->dateTimeBetween($create_at_obj,now()) ;
$update_at = $update_at_obj->format("Y-m-d H:i:s");


return [
'id' => $this->faker ->unique()->randomNumber(2,false),
'user_id' => $this->faker->randomElement([1]),
'organizer_ids' =>'1',
'title' => 'Test report '.$this->faker->randomDigit(),
'body' => '<p>'.$this->faker->paragraph().'</p>',
'externe_link' =>'#',
'start_date' => $start_date,
'start_time' => $start_time,
'end_date' => $end_date,
'end_time' => $end_time,
'location' => null,
'location_human' => 'Loc',
'image' => null,
'status' => $this->faker->randomElement(static::$status_map),
'created_at' => $create_at,
'updated_at' => $update_at,

];
}
}
Loading
Loading