diff --git a/app/Http/Controllers/BlogPostController.php b/app/Http/Controllers/BlogPostController.php index b1fe70d..d3e0799 100644 --- a/app/Http/Controllers/BlogPostController.php +++ b/app/Http/Controllers/BlogPostController.php @@ -44,7 +44,7 @@ class BlogPostController extends Controller ->cooldown($expiresAt) ->record(); - echo view('blog_post/show', $data); + return view('blog_post/show', $data); } public function edit(BlogPost $blogPost): View diff --git a/app/Livewire/BlogPost/Comment/Create.php b/app/Livewire/BlogPost/Comment/Create.php index ef84721..27325ad 100644 --- a/app/Livewire/BlogPost/Comment/Create.php +++ b/app/Livewire/BlogPost/Comment/Create.php @@ -5,6 +5,7 @@ namespace App\Livewire\BlogPost\Comment; use App\Models\BlogPost; use App\Models\BlogPostComment; use App\Models\User; +use Illuminate\View\View; use Livewire\Component; class Create extends Component @@ -13,16 +14,16 @@ class Create extends Component public User $user; - public $body = ''; + public string $body = ''; - public $grade = 0; + public string|int $grade = 0; - public function render() + public function render(): View { return view('livewire.blog-post.comment.create'); } - public function save() + public function save(): void { $comment = new BlogPostComment(); $comment->grade = $this->grade; diff --git a/app/Livewire/BlogPost/Comment/Show.php b/app/Livewire/BlogPost/Comment/Show.php index 1efdb24..0cf4bb4 100644 --- a/app/Livewire/BlogPost/Comment/Show.php +++ b/app/Livewire/BlogPost/Comment/Show.php @@ -3,16 +3,20 @@ namespace App\Livewire\BlogPost\Comment; use App\Models\BlogPost; +use Illuminate\View\View; use Livewire\Component; class Show extends Component { public BlogPost $blogPost; - protected $listeners = ['commentAdded' => '$refresh']; - - public function render() + public function render(): View { return view('livewire.blog-post.comment.show'); } + + protected function getListeners(): array + { + return ['commentAdded' => '$refresh']; + } } diff --git a/app/Livewire/BlogPost/Comment/Upvote.php b/app/Livewire/BlogPost/Comment/Upvote.php index 600776e..0590029 100644 --- a/app/Livewire/BlogPost/Comment/Upvote.php +++ b/app/Livewire/BlogPost/Comment/Upvote.php @@ -3,18 +3,19 @@ namespace App\Livewire\BlogPost\Comment; use App\Models\BlogPostComment; +use Illuminate\View\View; use Livewire\Component; class Upvote extends Component { public BlogPostComment $comment; - public function mount(BlogPostComment $comment) + public function mount(BlogPostComment $comment): void { $this->comment = $comment; } - public function render() + public function render(): View { $upvotes = $this->comment->upvotes()->forBlogPostCommentsUpVote($this->comment)->count(); $downvotes = $this->comment->upvotes()->forBlogPostCommentsDownVote($this->comment)->count(); @@ -30,7 +31,7 @@ class Upvote extends Component ); } - public function upvote() + public function upvote(): void { if ($this->comment->isUpVoted()) { $this->comment->removeUpVote(); @@ -49,7 +50,7 @@ class Upvote extends Component ]); } - public function downvote() + public function downvote(): void { if ($this->comment->isDownVoted()) { $this->comment->removeDownVote(); diff --git a/app/Livewire/BlogPost/Create.php b/app/Livewire/BlogPost/Create.php index 6a87956..7fa86f1 100644 --- a/app/Livewire/BlogPost/Create.php +++ b/app/Livewire/BlogPost/Create.php @@ -4,7 +4,10 @@ namespace App\Livewire\BlogPost; use App\Models\BlogPost; use App\Models\Tag; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Auth; +use Illuminate\View\View; use Livewire\Component; use Livewire\WithFileUploads; @@ -12,56 +15,55 @@ class Create extends Component { use WithFileUploads; - public $image; + public UploadedFile|null $image = null; - public $state = 'archived'; + public string $state = 'archived'; - public $title = ''; + public string $title = ''; - public $description = ''; + public string $description = ''; - public $tags = []; + public Collection|array $tags = []; - public $videoUrl = ''; + public string $videoUrl = ''; - protected $listeners = ['parentComponentTagAdded', 'parentComponentTagRemoved']; - - public function render() + public function render(): View { return view('livewire.blog-post.create'); } - public function parentComponentTagAdded(Tag $tag) + public function parentComponentTagAdded(Tag $tag): void { $tagIndex = $tag->id; $this->tags[$tagIndex] = $tag; } - public function parentComponentTagRemoved($tag) + public function parentComponentTagRemoved(Tag $tag): void { $this->tags = collect($this->tags) ->filter(fn ($curTag) => $curTag['name'] !== $tag) ->all(); } - public function draft() + public function draft(): void { $this->state = 'archived'; $this->publish(); } - public function preview() + public function preview(): void { $this->state = 'archived'; $this->publish(); } - public function publish() + public function publish(): void { $this->validate(); $post = new BlogPost(); - $post->image = $this->image->store('blog_posts', 'public'); + // $post->image = $this->image->store('blog_posts', 'public'); + $post->image = $this->image->store('blog_posts'); $post->state = $this->state; $post->title = $this->title; $post->description = $this->description; @@ -74,10 +76,18 @@ class Create extends Component $user = Auth::user(); $user->author_of_blog_post()->attach($post->id); - return redirect()->route('blog_post.show', $post->id); + $this->redirectRoute('blog_post.show', $post); + // return redirect()->route('blog_post.show', $post->id); } - protected function rules() + // protected $listeners = ['parentComponentTagAdded', 'parentComponentTagRemoved']; + + protected function getListeners(): array + { + return ['parentComponentTagAdded', 'parentComponentTagRemoved']; + } + + protected function rules(): array { return [ 'image' => 'required|image|max:2048', diff --git a/app/Livewire/BlogPost/Edit.php b/app/Livewire/BlogPost/Edit.php index a95081c..cc2b3cf 100644 --- a/app/Livewire/BlogPost/Edit.php +++ b/app/Livewire/BlogPost/Edit.php @@ -4,7 +4,10 @@ namespace App\Livewire\BlogPost; use App\Models\BlogPost; use App\Models\Tag; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Auth; +use Illuminate\View\View; use Livewire\Component; use Livewire\WithFileUploads; @@ -14,23 +17,21 @@ class Edit extends Component public BlogPost $blogPost; - public $image; + public string|UploadedFile $image; - public $state = 'archived'; + public string $state = 'archived'; - public $title = ''; + public string $title = ''; - public $description = ''; + public string $description = ''; - public $tags = []; + public Collection|array $tags = []; - public $videoUrl = ''; + public string $videoUrl = ''; - protected $listeners = ['blogPostTagAdded', 'blogPostTagRemoved']; - - public function mount(BlogPost $blogPost) + public function mount(BlogPost $blogPost): void { - $this->name = $blogPost->name; + // $this->name = $blogPost->name; $this->image = $blogPost->image; $this->state = $blogPost->state; $this->title = $blogPost->title; @@ -39,13 +40,13 @@ class Edit extends Component $this->videoUrl = $blogPost->youtube_url; } - public function parentComponentTagAdded(Tag $tag) + public function parentComponentTagAdded(Tag $tag): void { $tagIndex = $tag->id; $this->tags[$tagIndex] = $tag; } - public function parentComponentTagRemoved($tag) + public function parentComponentTagRemoved(Tag $tag): void { $tagToDetach = Tag::where('name', $tag)->first(); $this->tags = collect($this->tags) @@ -55,24 +56,24 @@ class Edit extends Component $this->blogPost->tags()->detach($tagToDetach->id); } - public function draft() + public function draft(): void { $this->state = 'archived'; $this->publish(); } - public function preview() + public function preview(): void { $this->state = 'archived'; $this->publish(); } - public function publish() + public function publish(): void { $this->validate(); $post = $this->blogPost; - /* $post->image = $this->image->store('blog_posts', 'public'); */ + $post->image = ! is_string($this->image) ? $this->image->store('blog_posts') : $this->image; $post->state = $this->state; $post->title = $this->title; $post->description = $this->description; @@ -88,15 +89,20 @@ class Edit extends Component $user = Auth::user(); $user->author_of_blog_post()->attach($post->id); - return redirect()->route('blog_post.show', $post->id); + $this->redirectRoute('blog_post.show', $post); } - public function render() + public function render(): View { return view('livewire.blog-post.edit'); } - protected function rules() + protected function getListeners(): array + { + return ['blogPostTagAdded', 'blogPostTagRemoved']; + } + + protected function rules(): array { return [ /* 'image' => 'required|image|max:2048', */ diff --git a/app/Livewire/BlogPost/Like.php b/app/Livewire/BlogPost/Like.php index be27bab..e8b84a9 100644 --- a/app/Livewire/BlogPost/Like.php +++ b/app/Livewire/BlogPost/Like.php @@ -3,20 +3,21 @@ namespace App\Livewire\BlogPost; use App\Models\BlogPost; +use Illuminate\View\View; use Livewire\Component; class Like extends Component { public BlogPost $blogPost; - public $isIconOnly = false; + public bool $isIconOnly = false; - public function mount(BlogPost $blogPost) + public function mount(BlogPost $blogPost): void { $this->blogPost = $blogPost; } - public function render() + public function render(): View { return view('livewire.blog-post.like'); } diff --git a/app/Livewire/BlogPost/LikeCount.php b/app/Livewire/BlogPost/LikeCount.php index 7ab9525..c0bbf43 100644 --- a/app/Livewire/BlogPost/LikeCount.php +++ b/app/Livewire/BlogPost/LikeCount.php @@ -3,24 +3,28 @@ namespace App\Livewire\BlogPost; use App\Models\BlogPost; +use Illuminate\View\View; use Livewire\Component; class LikeCount extends Component { public BlogPost $blogPost; - protected $listeners = ['liked' => '$refresh']; - - public function mount(BlogPost $blogPost) + public function mount(BlogPost $blogPost): void { $this->blogPost = $blogPost; } - public function render() + public function render(): View { return view( 'livewire.blog-post.like-count', ['count' => $this->blogPost->likes_count] ); } + + protected function getListeners(): array + { + return ['liked' => '$refresh']; + } } diff --git a/app/Livewire/BlogPost/ListView.php b/app/Livewire/BlogPost/ListView.php index d412e58..bde3fa9 100644 --- a/app/Livewire/BlogPost/ListView.php +++ b/app/Livewire/BlogPost/ListView.php @@ -6,6 +6,7 @@ use App\Models\BlogPost; use App\Models\Tag; use App\Models\User; use Illuminate\Support\Str; +use Illuminate\View\View; use Livewire\Component; use Livewire\WithPagination; @@ -13,22 +14,22 @@ class ListView extends Component { use WithPagination; - public $search = ''; + public string $search = ''; - public $typeFilter = 'all'; + public string $typeFilter = 'all'; - public $tag = ''; + public string $tag = ''; - public $author; + public User|null $author = null; - protected $queryString = [ + protected array $queryString = [ 'search' => ['except' => ''], 'typeFilter' => ['except' => 'all'], 'tag' => ['except' => ''], 'author' => ['except' => ''], ]; - public function render() + public function render(): View { if ($this->author) { $author = $this->author; diff --git a/app/Models/BlogPost.php b/app/Models/BlogPost.php index df96189..0957e9a 100644 --- a/app/Models/BlogPost.php +++ b/app/Models/BlogPost.php @@ -4,11 +4,13 @@ namespace App\Models; use CyrildeWit\EloquentViewable\Contracts\Viewable; use CyrildeWit\EloquentViewable\InteractsWithViews; -/* use Illuminate\Database\Eloquent\Builder; */ +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; -/* use Illuminate\Database\Eloquent\Casts\Attribute; */ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOneThrough; +use Illuminate\Database\Eloquent\Relations\MorphToMany; +use Illuminate\Support\Str; /** * App\Models\BlogPost @@ -56,17 +58,22 @@ class BlogPost extends Model implements Viewable { use HasFactory, InteractsWithViews; - protected $withCount = [ - 'likes', - 'comments', - ]; + // protected array $withCount = [ + // 'likes', + // 'comments', + // ]; - public function tags() + public static function getWithLikesAndCommentsCount(): Builder + { + return self::withCount(['likes', 'comments']); + } + + public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'taggable'); } - public function author() + public function author(): HasOneThrough { return $this ->hasOneThrough(User::class, BlogPostAuthor::class, 'blog_post_id', 'id', 'id', 'user_id') @@ -79,7 +86,7 @@ class BlogPost extends Model implements Viewable ); } - public function comments() + public function comments(): HasMany { return $this->hasMany(BlogPostComment::class); } @@ -89,7 +96,7 @@ class BlogPost extends Model implements Viewable return $this->hasMany(BlogPostLike::class); } - public function isLiked(): bool + public function isLiked(): bool|int { $curUser = auth()->user(); if ($curUser) { @@ -109,12 +116,12 @@ class BlogPost extends Model implements Viewable return false; } - public function scopeNoEagerLoads($query) + public function scopeNoEagerLoads(Builder $query): Builder { return $query->setEagerLoads([]); } - public function scopeContainsTag($query, $tagString): Builder + public function scopeContainsTag(Builder $query, string $tagString): Builder { $tagId = Str::of($tagString)->split('/-/')->first(); diff --git a/app/Models/BlogPostComment.php b/app/Models/BlogPostComment.php index ed7fbab..d14a6ef 100644 --- a/app/Models/BlogPostComment.php +++ b/app/Models/BlogPostComment.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; /** @@ -39,12 +40,12 @@ class BlogPostComment extends Model { use HasFactory; - public function author() + public function author(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } - public function blog_post() + public function blog_post(): BelongsTo { return $this->belongsTo(BlogPost::class, 'blog_post_id'); } @@ -54,7 +55,7 @@ class BlogPostComment extends Model return $this->hasMany(BlogPostCommentUpvote::class, 'blog_post_comment_id'); } - public function isUpVoted(): bool + public function isUpVoted(): bool|int { $curUser = auth()->user(); if ($curUser) { @@ -67,7 +68,7 @@ class BlogPostComment extends Model return false; } - public function isDownVoted(): bool + public function isDownVoted(): bool|int { $curUser = auth()->user(); if ($curUser) { diff --git a/app/Models/BlogPostCommentUpvote.php b/app/Models/BlogPostCommentUpvote.php index e87ae70..07cf998 100644 --- a/app/Models/BlogPostCommentUpvote.php +++ b/app/Models/BlogPostCommentUpvote.php @@ -2,8 +2,10 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; /** * App\Models\BlogPostCommentUpvote @@ -36,28 +38,31 @@ class BlogPostCommentUpvote extends Model { use HasFactory; + /** + * @var array + */ protected $fillable = [ 'user_id', 'blog_post_comment_id', 'type', ]; - public function author() + public function author(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } - public function blog_post_comment() + public function blog_post_comment(): BelongsTo { return $this->belongsTo(BlogPostComment::class, 'blog_post_comment_id'); } - public function scopeForBlogPostCommentsUpVote($query, BlogPostComment $comment) + public function scopeForBlogPostCommentsUpVote(Builder $query, BlogPostComment $comment): Builder { return $query->where('blog_post_comment_id', $comment->id)->where('type', 'upvote'); } - public function scopeForBlogPostCommentsDownVote($query, BlogPostComment $comment) + public function scopeForBlogPostCommentsDownVote(Builder $query, BlogPostComment $comment): Builder { return $query->where('blog_post_comment_id', $comment->id)->where('type', 'downvote'); } diff --git a/app/Models/BlogPostLike.php b/app/Models/BlogPostLike.php index 1c524e2..829d3d0 100644 --- a/app/Models/BlogPostLike.php +++ b/app/Models/BlogPostLike.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; /** * App\Models\BlogPostLike @@ -30,11 +31,14 @@ class BlogPostLike extends Model { use HasFactory; + /** + * @var array + */ protected $fillable = [ 'user_id', ]; - public function scopeForBlogPost($query, BlogPost $blogPost) + public function scopeForBlogPost(Builder $query, BlogPost $blogPost): Builder { return $query->where('blog_post_id', $blogPost->id); } diff --git a/database/assets/images/blogPosts/1.jpg b/database/assets/images/blogPosts/1.jpg new file mode 100644 index 0000000..109d54b Binary files /dev/null and b/database/assets/images/blogPosts/1.jpg differ diff --git a/database/assets/images/blogPosts/10.jpg b/database/assets/images/blogPosts/10.jpg new file mode 100644 index 0000000..f741713 Binary files /dev/null and b/database/assets/images/blogPosts/10.jpg differ diff --git a/database/assets/images/blogPosts/2.jpg b/database/assets/images/blogPosts/2.jpg new file mode 100644 index 0000000..d9678ed Binary files /dev/null and b/database/assets/images/blogPosts/2.jpg differ diff --git a/database/assets/images/blogPosts/3.jpg b/database/assets/images/blogPosts/3.jpg new file mode 100644 index 0000000..486d99a Binary files /dev/null and b/database/assets/images/blogPosts/3.jpg differ diff --git a/database/assets/images/blogPosts/4.jpg b/database/assets/images/blogPosts/4.jpg new file mode 100644 index 0000000..a733364 Binary files /dev/null and b/database/assets/images/blogPosts/4.jpg differ diff --git a/database/assets/images/blogPosts/5.jpg b/database/assets/images/blogPosts/5.jpg new file mode 100644 index 0000000..60afe23 Binary files /dev/null and b/database/assets/images/blogPosts/5.jpg differ diff --git a/database/assets/images/blogPosts/6.jpg b/database/assets/images/blogPosts/6.jpg new file mode 100644 index 0000000..dea5e63 Binary files /dev/null and b/database/assets/images/blogPosts/6.jpg differ diff --git a/database/assets/images/blogPosts/7.jpg b/database/assets/images/blogPosts/7.jpg new file mode 100644 index 0000000..296e4d2 Binary files /dev/null and b/database/assets/images/blogPosts/7.jpg differ diff --git a/database/assets/images/blogPosts/8.jpg b/database/assets/images/blogPosts/8.jpg new file mode 100644 index 0000000..1c4af7f Binary files /dev/null and b/database/assets/images/blogPosts/8.jpg differ diff --git a/database/assets/images/blogPosts/9.jpg b/database/assets/images/blogPosts/9.jpg new file mode 100644 index 0000000..ed5d6c8 Binary files /dev/null and b/database/assets/images/blogPosts/9.jpg differ diff --git a/database/factories/BlogPostCommentFactory.php b/database/factories/BlogPostCommentFactory.php new file mode 100644 index 0000000..99d1795 --- /dev/null +++ b/database/factories/BlogPostCommentFactory.php @@ -0,0 +1,25 @@ + + */ +class BlogPostCommentFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'grade' => $this->faker->randomDigit(), + 'body' => $this->faker->text(), + ]; + + } +} diff --git a/database/factories/BlogPostFactory.php b/database/factories/BlogPostFactory.php index 1780ea6..a7988a5 100644 --- a/database/factories/BlogPostFactory.php +++ b/database/factories/BlogPostFactory.php @@ -5,9 +5,16 @@ 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; +use Faker\Generator; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BlogPost> + * + * @property Generator|Youtube $faker */ class BlogPostFactory extends Factory { @@ -20,28 +27,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; } } diff --git a/database/seeders/BlogPostSeeder.php b/database/seeders/BlogPostSeeder.php index a5e4c02..9e8ce08 100644 --- a/database/seeders/BlogPostSeeder.php +++ b/database/seeders/BlogPostSeeder.php @@ -3,16 +3,62 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; +use App\Models\BlogPost; +use App\Models\User; +use App\Models\BlogPostComment; +use App\Models\Tag; class BlogPostSeeder extends Seeder { /** * Run the database seeds. - * - * @return void */ - public function run() + public function run(): void { - // + $blogPosts = BlogPost::factory()->count(10)->create(); + + if (User::count() > 0 && BlogPost::count() > 0) { + + $blogPosts->each( + function ($blogPost) { + User::inRandomOrder() + ->first() + ->author_of_blog_post() + ->attach($blogPost->id); + } + ); + + $comments = BlogPostComment::factory()->count(5)->make(); + + $comments->each( + static function ($comment): void { + $comment->author()->associate( + User::inRandomOrder() + ->first() + ->id + ); + + $comment->blog_post()->associate( + BlogPost::inRandomOrder() + ->first() + ->id + ); + + $comment->saveOrFail(); + } + ); + + $blogPosts = BlogPost::all(); + + $blogPosts->each( + static function ($blogPost): void { + + $tags = Tag::factory()->count(5)->create(); + + $tagsIds = collect($tags)->map(fn($curTag) => $curTag['id']); + $blogPost->tags()->attach($tagsIds); + } + ); + } } } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 0be680a..e99e991 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -18,5 +18,6 @@ class DatabaseSeeder extends Seeder $this->call(RecipeAuthor::class); $this->call(RecipeCommentSeeder::class); $this->call(RecipeTagSeeder::class); + $this->call(BlogPostSeeder::class); } } diff --git a/tests/Feature/BlogPost/CreateTest.php b/tests/Feature/BlogPost/CreateTest.php new file mode 100644 index 0000000..da7e846 --- /dev/null +++ b/tests/Feature/BlogPost/CreateTest.php @@ -0,0 +1,180 @@ +refreshApplicationWithLocale('en'); + + Storage::fake('tmp-for-tests'); + $this->image = UploadedFile::fake()->image('blogPost.png'); + + $this->user = User::factory()->create(['is_consultant' => true]); + $this->blogPost = BlogPost::factory()->make(); + + } + + public function test_show_form_create_blogPost(): void + { + $response = $this + ->actingAs($this->user) + ->get(route('blog_post.create')); + + $response->assertSuccessful(); + } + + public function test_publish_new_blogPost_success(): void + { + $this->actingAs($this->user); + + $blogPostCountAtStart = BlogPost::count(); + + $testCase = Livewire::test(Create::class) + ->set('title', $this->blogPost->title) + ->assertSet('title', $this->blogPost->title) + ->set('state', $this->blogPost->state) + ->assertSet('state', $this->blogPost->state) + ->set('image', $this->image) + ->set('description', $this->blogPost->description) + ->assertSet('description', $this->blogPost->description) + ->set('youtube_url', $this->blogPost->youtube_url) + ->assertSet('youtube_url', $this->blogPost->youtube_url) + ->call('publish') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'description', 'youtube_url']); + + + $lastCreatedBlogPost = BlogPost::orderBy('id', 'desc')->first(); + + $newBlogPostUrl = route('blog_post.show', $lastCreatedBlogPost); + + $testCase->assertRedirect($newBlogPostUrl); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedBlogPost->image); + + $this->assertEquals($blogPostCountAtStart + 1, BlogPost::count()); + + $response = $this->actingAs($this->user)->get(route('blog_post.index')); + + $response->assertSuccessful(); + } + + public function test_to_draft_new_blogPost_success(): void + { + $this->actingAs($this->user); + + $blogPostCountAtStart = BlogPost::count(); + + $testCase = Livewire::test(Create::class) + ->set('title', $this->blogPost->title) + ->assertSet('title', $this->blogPost->title) + ->set('state', $this->blogPost->state) + ->assertSet('state', $this->blogPost->state) + ->set('image', $this->image) + ->set('description', $this->blogPost->description) + ->assertSet('description', $this->blogPost->description) + ->set('youtube_url', $this->blogPost->youtube_url) + ->assertSet('youtube_url', $this->blogPost->youtube_url) + ->call('draft') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'description', 'youtube_url']); + + + self::assertEquals('archived', $testCase->state); + + $lastCreatedBlogPost = BlogPost::orderBy('id', 'desc')->first(); + + $newBlogPostUrl = route('blog_post.show', $lastCreatedBlogPost); + + $testCase->assertRedirect($newBlogPostUrl); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedBlogPost->image); + + $this->assertEquals($blogPostCountAtStart + 1, BlogPost::count()); + + $response = $this->actingAs($this->user)->get(route('blog_post.index')); + + $response->assertSuccessful(); + } + + public function test_to_preview_new_blogPost_success(): void + { + $this->actingAs($this->user); + + $blogPostCountAtStart = BlogPost::count(); + + $testCase = Livewire::test(Create::class) + ->set('title', $this->blogPost->title) + ->assertSet('title', $this->blogPost->title) + ->set('state', $this->blogPost->state) + ->assertSet('state', $this->blogPost->state) + ->set('image', $this->image) + ->set('description', $this->blogPost->description) + ->assertSet('description', $this->blogPost->description) + ->set('youtube_url', $this->blogPost->youtube_url) + ->assertSet('youtube_url', $this->blogPost->youtube_url) + ->call('preview') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'description', 'youtube_url']); + + + self::assertEquals('archived', $testCase->state); + + $lastCreatedBlogPost = BlogPost::orderBy('id', 'desc')->first(); + + $newBlogPostUrl = route('blog_post.show', $lastCreatedBlogPost); + + $testCase->assertRedirect($newBlogPostUrl); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedBlogPost->image); + + $this->assertEquals($blogPostCountAtStart + 1, BlogPost::count()); + + $response = $this->actingAs($this->user)->get(route('blog_post.index')); + + $response->assertSuccessful(); + } + + public function test_set_empty_data(): void + { + $this->actingAs($this->user); + + $blogPostCountAtStart = BlogPost::count(); + + Livewire::test(Create::class) + ->set('title', null) + ->assertSet('title', null) + ->set('state', null) + ->assertSet('state', null) + ->set('image', null) + ->set('description', null) + ->assertSet('description', null) + ->set('youtube_url', null) + ->assertSet('youtube_url', null) + ->call('publish') + ->assertSuccessful() + ->assertHasErrors(['image', 'description']); + + $this->assertEquals($blogPostCountAtStart, BlogPost::count()); + } +} diff --git a/tests/Feature/BlogPost/EditTest.php b/tests/Feature/BlogPost/EditTest.php new file mode 100644 index 0000000..ec8520c --- /dev/null +++ b/tests/Feature/BlogPost/EditTest.php @@ -0,0 +1,195 @@ +refreshApplicationWithLocale('en'); + + Storage::fake('tmp-for-tests'); + $this->image = UploadedFile::fake()->image('blogPost.png'); + + $this->user = User::factory()->create(['is_consultant' => true]); + $this->blogPost = BlogPost::factory()->create(); + } + + public function test_show_form_edit_blogPost(): void + { + $response = $this + ->actingAs($this->user) + ->get(route('blog_post.edit', $this->blogPost)); + + $response->assertSuccessful(); + } + + public function test_publish_new_blogPost_success(): void + { + $this->actingAs($this->user); + + $updatedBlogPostBeforeToAddDatabase = BlogPost::factory()->make(); + + $testCase = Livewire::test( + Edit::class, [ + 'blogPost' => $this->blogPost, + ] + ) + ->set('title', $updatedBlogPostBeforeToAddDatabase->title) + ->assertSet('title', $updatedBlogPostBeforeToAddDatabase->title) + ->set('state', $updatedBlogPostBeforeToAddDatabase->state) + ->assertSet('state', $updatedBlogPostBeforeToAddDatabase->state) + ->set('image', $this->image) + ->set('description', $updatedBlogPostBeforeToAddDatabase->description) + ->assertSet('description', $updatedBlogPostBeforeToAddDatabase->description) + ->set('youtube_url', $updatedBlogPostBeforeToAddDatabase->youtube_url) + ->assertSet('youtube_url', $updatedBlogPostBeforeToAddDatabase->youtube_url) + ->call('publish') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'state', 'description', 'youtube_url']); + + + $updatedBlogPostAfterToAddDatabase = BlogPost::orderByDesc('id')->first(); + + $newBlogPostUrl = route('blog_post.show', $updatedBlogPostAfterToAddDatabase); + + Storage::disk('tmp-for-tests')->assertExists($updatedBlogPostAfterToAddDatabase->image); + + + // check data after update recipe + self::assertEquals($updatedBlogPostBeforeToAddDatabase->title, $updatedBlogPostAfterToAddDatabase->title); + self::assertEquals($updatedBlogPostBeforeToAddDatabase->state, $updatedBlogPostAfterToAddDatabase->state); + self::assertEquals($updatedBlogPostBeforeToAddDatabase->description, $updatedBlogPostAfterToAddDatabase->description); + // self::assertEquals($updatedBlogPostBeforeToAddDatabase->youtube_url, $updatedBlogPostAfterToAddDatabase->youtube_url); + + $testCase->assertRedirect($newBlogPostUrl); + } + + public function test_to_draft_new_blogPost_success(): void + { + $this->actingAs($this->user); + + $updatedBlogPostBeforeToAddDatabase = BlogPost::factory()->make(); + + $testCase = Livewire::test( + Edit::class, [ + 'blogPost' => $this->blogPost, + ] + ) + ->set('title', $updatedBlogPostBeforeToAddDatabase->title) + ->assertSet('title', $updatedBlogPostBeforeToAddDatabase->title) + ->set('state', $updatedBlogPostBeforeToAddDatabase->state) + ->assertSet('state', $updatedBlogPostBeforeToAddDatabase->state) + ->set('image', $this->image) + ->set('description', $updatedBlogPostBeforeToAddDatabase->description) + ->assertSet('description', $updatedBlogPostBeforeToAddDatabase->description) + ->set('youtube_url', $updatedBlogPostBeforeToAddDatabase->youtube_url) + ->assertSet('youtube_url', $updatedBlogPostBeforeToAddDatabase->youtube_url) + ->call('draft') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'state', 'description', 'youtube_url']); + + + $updatedBlogPostAfterToAddDatabase = BlogPost::orderByDesc('id')->first(); + + $newBlogPostUrl = route('blog_post.show', $updatedBlogPostAfterToAddDatabase); + + Storage::disk('tmp-for-tests')->assertExists($updatedBlogPostAfterToAddDatabase->image); + + + // check data after update recipe + self::assertEquals($updatedBlogPostBeforeToAddDatabase->title, $updatedBlogPostAfterToAddDatabase->title); + self::assertEquals('archived', $updatedBlogPostAfterToAddDatabase->state); + self::assertEquals($updatedBlogPostBeforeToAddDatabase->description, $updatedBlogPostAfterToAddDatabase->description); + // self::assertEquals($updatedBlogPostBeforeToAddDatabase->youtube_url, $updatedBlogPostAfterToAddDatabase->youtube_url); + + $testCase->assertRedirect($newBlogPostUrl); + } + + public function test_to_preview_new_blogPost_success(): void + { + $this->actingAs($this->user); + + $updatedBlogPostBeforeToAddDatabase = BlogPost::factory()->make(); + + $testCase = Livewire::test( + Edit::class, [ + 'blogPost' => $this->blogPost, + ] + ) + ->set('title', $updatedBlogPostBeforeToAddDatabase->title) + ->assertSet('title', $updatedBlogPostBeforeToAddDatabase->title) + ->set('state', $updatedBlogPostBeforeToAddDatabase->state) + ->assertSet('state', $updatedBlogPostBeforeToAddDatabase->state) + ->set('image', $this->image) + ->set('description', $updatedBlogPostBeforeToAddDatabase->description) + ->assertSet('description', $updatedBlogPostBeforeToAddDatabase->description) + ->set('youtube_url', $updatedBlogPostBeforeToAddDatabase->youtube_url) + ->assertSet('youtube_url', $updatedBlogPostBeforeToAddDatabase->youtube_url) + ->call('preview') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'state', 'description', 'youtube_url']); + + + $updatedBlogPostAfterToAddDatabase = BlogPost::orderByDesc('id')->first(); + + $newBlogPostUrl = route('blog_post.show', $updatedBlogPostAfterToAddDatabase); + + Storage::disk('tmp-for-tests')->assertExists($updatedBlogPostAfterToAddDatabase->image); + + + // check data after update recipe + self::assertEquals($updatedBlogPostBeforeToAddDatabase->title, $updatedBlogPostAfterToAddDatabase->title); + self::assertEquals('archived', $updatedBlogPostAfterToAddDatabase->state); + self::assertEquals($updatedBlogPostBeforeToAddDatabase->description, $updatedBlogPostAfterToAddDatabase->description); + // self::assertEquals($updatedBlogPostBeforeToAddDatabase->youtube_url, $updatedBlogPostAfterToAddDatabase->youtube_url); + + $testCase->assertRedirect($newBlogPostUrl); + + } + + public function testNothingChange(): void + { + $this->actingAs($this->user); + + $blogPostUpdate = Livewire::test( + Edit::class, [ + 'blogPost' => $this->blogPost, + ] + ) + ->call('publish') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'state', 'description', 'youtube_url']); + + $newBlogPostUrl = route('blog_post.show', $this->blogPost, false); + + Storage::disk('tmp-for-tests')->assertMissing($blogPostUpdate->image); + + // check data after update recipe + self::assertEquals($this->blogPost->title, $blogPostUpdate->title); + self::assertEquals($this->blogPost->state, $blogPostUpdate->state); + self::assertEquals($this->blogPost->description, $blogPostUpdate->description); + // self::assertEquals($this->blogPost->youtube_url, $blogPostUpdate->youtube_url); + + $blogPostUpdate->assertRedirect($newBlogPostUrl); + } + +} + diff --git a/tests/Feature/BlogPost/IndexTest.php b/tests/Feature/BlogPost/IndexTest.php new file mode 100644 index 0000000..c12d432 --- /dev/null +++ b/tests/Feature/BlogPost/IndexTest.php @@ -0,0 +1,31 @@ +refreshApplicationWithLocale('en'); + + $this->user = User::factory()->create(['is_consultant' => true]); + } + + public function test_index_page_success(): void + { + $response = $this + ->actingAs($this->user) + ->get(route('blog_post.index')); + + $response + ->assertOk(); + } +} diff --git a/tests/Feature/BlogPost/ShowTest.php b/tests/Feature/BlogPost/ShowTest.php new file mode 100644 index 0000000..67a16be --- /dev/null +++ b/tests/Feature/BlogPost/ShowTest.php @@ -0,0 +1,35 @@ +refreshApplicationWithLocale('en'); + + $this->user = User::inRandomOrder()->first(); + $this->blogPost = BlogPost::inRandomOrder()->first(); + } + + public function test_show_page_success(): void + { + $response = $this->actingAs($this->user) + ->get(route('blog_post.show', $this->blogPost)); + + $response + ->assertOk(); + } + +}