forked from eatvio/eatvio-infra
added tests for blogPost
This commit is contained in:
@@ -5,6 +5,10 @@ namespace Database\Factories;
|
||||
use App\Models\BlogPost;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Models\User;
|
||||
use Faker\Provider\Youtube;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BlogPost>
|
||||
@@ -20,28 +24,27 @@ class BlogPostFactory extends Factory
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
$imagePath = storage_path('app/public');
|
||||
$curImage = $this->faker->image($imagePath.'/blog_posts', 360, 360, 'animals', true, true, 'cats', true, 'jpg');
|
||||
$curImage = Str::replace($imagePath, '', $curImage);
|
||||
$faker = \Faker\Factory::create();
|
||||
$this->faker->addProvider(new Youtube($faker));
|
||||
|
||||
return [
|
||||
'state' => $this->faker->randomElement(['active', 'archived']),
|
||||
'image' => $curImage,
|
||||
'image' => $this->generateImageFile('blog_post'),
|
||||
'description' => $this->faker->realText($this->faker->numberBetween(100, 200)),
|
||||
'youtube_url' => $this->faker->url(),
|
||||
'created_at' => $this->faker->dateTimeBetween('-1 week', '+1 week'),
|
||||
'updated_at' => $this->faker->dateTimeBetween('-1 week', '+1 week'),
|
||||
'youtube_url' => $this->faker->youtubeRandomUri(),
|
||||
'title' => $this->faker->realText($this->faker->numberBetween(10, 20)),
|
||||
];
|
||||
}
|
||||
|
||||
public function some()
|
||||
private function generateImageFile(string $name): string
|
||||
{
|
||||
User::orderBy('created_at', 'desc')->limit(20)->get()->map(function ($user) {
|
||||
$newBlogPosts = BlogPost::factory()->count(3)->create();
|
||||
$newBlogPosts->map(function ($post) use ($user) {
|
||||
$user->author_of_blog_post()->attach($post->id);
|
||||
});
|
||||
});
|
||||
$images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg'];
|
||||
Storage::fake($name);
|
||||
$imageFileKey = $this->faker->numberBetween(0, 9);
|
||||
$imageFilePath = 'database/assets/images/blogPosts/' . $images[$imageFileKey];
|
||||
$file = new File($imageFilePath);
|
||||
$fileLocalPath = Storage::putFile($name, $file, 'public');
|
||||
|
||||
return $fileLocalPath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user