Merge branch 'eatvio-main'

This commit is contained in:
2024-01-11 22:31:32 +03:00
31 changed files with 664 additions and 100 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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'];
}
}

View File

@@ -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();

View File

@@ -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',

View File

@@ -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', */

View File

@@ -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');
}

View File

@@ -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'];
}
}

View File

@@ -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;

View File

@@ -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();

View File

@@ -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) {

View File

@@ -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<string>
*/
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');
}

View File

@@ -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<string>
*/
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);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 854 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 746 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

View File

@@ -0,0 +1,25 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BlogPostComment>
*/
class BlogPostCommentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'grade' => $this->faker->randomDigit(),
'body' => $this->faker->text(),
];
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
);
}
}
}

View File

@@ -18,5 +18,6 @@ class DatabaseSeeder extends Seeder
$this->call(RecipeAuthor::class);
$this->call(RecipeCommentSeeder::class);
$this->call(RecipeTagSeeder::class);
$this->call(BlogPostSeeder::class);
}
}

View File

@@ -0,0 +1,180 @@
<?php
namespace Tests\Feature\BlogPost;
use App\Livewire\BlogPost\Create;
use App\Models\BlogPost;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
use Illuminate\Http\Testing\File;
class CreateTest extends TestCase
{
use RefreshDatabase;
private File $image;
private BlogPost $blogPost;
protected User $user;
public function setUp(): void
{
$this->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());
}
}

View File

@@ -0,0 +1,195 @@
<?php
namespace Tests\Feature\BlogPost;
use App\Models\BlogPost;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
use App\Livewire\BlogPost\Edit;
class EditTest extends TestCase
{
use RefreshDatabase;
private UploadedFile $image;
private BlogPost $blogPost;
protected User $user;
public function setUp(): void
{
$this->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);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Tests\Feature\BlogPost;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class IndexTest extends TestCase
{
use RefreshDatabase;
protected User $user;
public function setUp(): void
{
$this->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();
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Tests\Feature\BlogPost;
use Tests\TestCase;
use App\Models\User;
use App\Models\BlogPost;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ShowTest extends TestCase
{
use RefreshDatabase;
private BlogPost $blogPost;
protected User $user;
public function setUp(): void
{
$this->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();
}
}