forked from eatvio/eatvio-infra
Merge branch 'diets'
This commit is contained in:
@@ -12,9 +12,9 @@ class DietSelected
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $diet;
|
||||
public Diet $diet;
|
||||
|
||||
public $user;
|
||||
public User $user;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
|
||||
@@ -12,14 +12,9 @@ class ProcessDietSelectedNotification implements ShouldQueue
|
||||
{
|
||||
use InteractsWithQueue;
|
||||
|
||||
public $afterCommit = true;
|
||||
public bool $afterCommit = true;
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(DietSelected $event)
|
||||
public function handle(DietSelected $event): void
|
||||
{
|
||||
$user = $event->user;
|
||||
$diet = $event->diet;
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Livewire\Diet\Comment;
|
||||
use App\Models\Diet;
|
||||
use App\Models\DietComment;
|
||||
use App\Models\User;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
|
||||
class Create extends Component
|
||||
@@ -13,23 +14,23 @@ class Create extends Component
|
||||
|
||||
public User $user;
|
||||
|
||||
public $body = '';
|
||||
public string $body = '';
|
||||
|
||||
public $grade = 0;
|
||||
public string|int $grade = 0;
|
||||
|
||||
public $isChecked = true;
|
||||
public bool $isChecked = true;
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.diet.comment.create');
|
||||
}
|
||||
|
||||
public function toggle()
|
||||
public function toggle(): void
|
||||
{
|
||||
$this->isChecked ? false : true;
|
||||
}
|
||||
|
||||
public function save()
|
||||
public function save(): void
|
||||
{
|
||||
$this->toggle();
|
||||
$comment = new DietComment();
|
||||
|
||||
@@ -3,16 +3,20 @@
|
||||
namespace App\Livewire\Diet\Comment;
|
||||
|
||||
use App\Models\Diet;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
|
||||
class Show extends Component
|
||||
{
|
||||
public Diet $diet;
|
||||
|
||||
protected $listeners = ['commentAdded' => '$refresh'];
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.diet.comment.show');
|
||||
}
|
||||
|
||||
protected function getListeners(): array
|
||||
{
|
||||
return ['commentAdded' => '$refresh'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,19 @@
|
||||
namespace App\Livewire\Diet\Comment;
|
||||
|
||||
use App\Models\DietComment;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
|
||||
class Upvote extends Component
|
||||
{
|
||||
public DietComment $comment;
|
||||
|
||||
public function mount(DietComment $comment)
|
||||
public function mount(DietComment $comment): void
|
||||
{
|
||||
$this->comment = $comment;
|
||||
}
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
$upvotes = $this->comment->upvotes()->forDietCommentsUpVote($this->comment)->count();
|
||||
$downvotes = $this->comment->upvotes()->forDietCommentsDownVote($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();
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
namespace App\Livewire\Diet;
|
||||
|
||||
use App\Models\Diet;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
@@ -11,86 +13,92 @@ class Create extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public $name = '';
|
||||
public string $name = '';
|
||||
|
||||
public $image;
|
||||
public UploadedFile|null $image = null;
|
||||
|
||||
public $duration = '7';
|
||||
public int|string $duration = '7';
|
||||
|
||||
public $state = 'draft';
|
||||
public string $state = 'draft';
|
||||
|
||||
public $type = [];
|
||||
public array $type = [];
|
||||
|
||||
public $details = '';
|
||||
public string $details = '';
|
||||
|
||||
public $days = [];
|
||||
public array $days = [];
|
||||
|
||||
public $copyedDay;
|
||||
public string $copyedDay;
|
||||
|
||||
public $copyedDayData;
|
||||
public array $copyedDayData;
|
||||
|
||||
protected $listeners = ['dayUpdated', 'makeCopy', 'makePaste'];
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.diet.create');
|
||||
}
|
||||
|
||||
public function dayUpdated($dayIndex, $data)
|
||||
public function dayUpdated(string $dayIndex, array $data): void
|
||||
{
|
||||
$this->days[$dayIndex] = $data;
|
||||
}
|
||||
|
||||
public function showPreview()
|
||||
public function showPreview(): void
|
||||
{
|
||||
$this->state = 'draft';
|
||||
$this->publish();
|
||||
}
|
||||
|
||||
public function addToDrafts()
|
||||
public function addToDrafts(): void
|
||||
{
|
||||
$this->state = 'draft';
|
||||
$this->publish();
|
||||
}
|
||||
|
||||
public function makeCopy($day)
|
||||
public function makeCopy(string $day): void
|
||||
{
|
||||
$this->copyedDay = $day;
|
||||
}
|
||||
|
||||
public function makePaste($copyToDay)
|
||||
public function makePaste(string $copyToDay): void
|
||||
{
|
||||
$data = $this->days[$this->copyedDay];
|
||||
$this->dispatch('takePaste', day: $copyToDay, data: $data)->to('diet.day-edit');
|
||||
}
|
||||
|
||||
public function publish()
|
||||
public function publish(): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$diet = new Diet();
|
||||
$diet->name = $this->name;
|
||||
$diet->image = $this->image->store('diets', 'public');
|
||||
// $diet->image = $this->image->store('diets', 'public');
|
||||
$diet->image = $this->image->store('diets');
|
||||
$diet->duration = $this->duration;
|
||||
$diet->state = $this->state;
|
||||
$diet->type = json_encode($this->type);
|
||||
$diet->type = $this->type;
|
||||
$diet->details = $this->details;
|
||||
$diet->daily_schedule = json_encode($this->days);
|
||||
$diet->save();
|
||||
$diet->daily_schedule = $this->days;
|
||||
$diet->saveOrFail();
|
||||
|
||||
$user = Auth::user();
|
||||
$user->author_of_diets()->attach($diet->id);
|
||||
|
||||
return redirect()->route('diet.show', $diet->id);
|
||||
$this->redirectRoute('diet.show', $diet);
|
||||
|
||||
// return redirect()->route('diet.show', $diet->id);
|
||||
}
|
||||
|
||||
protected function rules()
|
||||
protected function getListeners(): array
|
||||
{
|
||||
return ['dayUpdated', 'makeCopy', 'makePaste'];
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|min:3',
|
||||
'image' => 'required|image|max:2048',
|
||||
'duration' => 'required|numeric|min:7',
|
||||
'state' => 'required|in:draft,active,inactive',
|
||||
'state' => 'required|in:active,draft,archived',
|
||||
'type' => ['required', 'array:meat,veg,corb,alko', 'min:1'],
|
||||
'details' => 'required|min:3',
|
||||
];
|
||||
|
||||
@@ -2,38 +2,37 @@
|
||||
|
||||
namespace App\Livewire\Diet;
|
||||
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
|
||||
class DayEdit extends Component
|
||||
{
|
||||
public $day;
|
||||
public string $day;
|
||||
|
||||
public $eatingTypes = ['breakfast', 'dinner', 'supper', 'lunch', 'snacks'];
|
||||
public array $eatingTypes = ['breakfast', 'dinner', 'supper', 'lunch', 'snacks'];
|
||||
|
||||
public $activeType = 'breakfast';
|
||||
public string $activeType = 'breakfast';
|
||||
|
||||
public $portions = [];
|
||||
public array $portions = [];
|
||||
|
||||
public $name = '';
|
||||
public string $name = '';
|
||||
|
||||
public $quantity = 0;
|
||||
public int $quantity = 0;
|
||||
|
||||
public $portion = 'gram';
|
||||
public string $portion = 'gram';
|
||||
|
||||
protected $listeners = ['takePaste'];
|
||||
|
||||
protected $rules = [
|
||||
protected array $rules = [
|
||||
'name' => 'required|min:3',
|
||||
'quantity' => 'required|numeric|min:1',
|
||||
'portion' => 'required|min:3',
|
||||
];
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.diet.day-edit');
|
||||
}
|
||||
|
||||
public function addPortion($type)
|
||||
public function addPortion(string $type): void
|
||||
{
|
||||
$this->validate();
|
||||
$newData = [
|
||||
@@ -49,7 +48,7 @@ class DayEdit extends Component
|
||||
$this->dispatch('dayUpdated', dayIndex: $this->day, data: $this->portions);
|
||||
}
|
||||
|
||||
public function delete($uniq, $type)
|
||||
public function delete(string $uniq, string $type): void
|
||||
{
|
||||
$newPortions = collect($this->portions[$type])
|
||||
->filter(
|
||||
@@ -61,7 +60,7 @@ class DayEdit extends Component
|
||||
$this->dispatch('dayUpdated', dayIndex: $this->day, data: $this->portions);
|
||||
}
|
||||
|
||||
public function edit($uniq, $type)
|
||||
public function edit(string $uniq, string $type): void
|
||||
{
|
||||
$elemKey = collect($this->portions[$type])
|
||||
->search(function ($item) use ($uniq) {
|
||||
@@ -80,7 +79,7 @@ class DayEdit extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function takePaste($day, $data)
|
||||
public function takePaste(string $day, array $data): void
|
||||
{
|
||||
if (intval($day) === intval($this->day)) {
|
||||
foreach ($data as $type => $props) {
|
||||
@@ -89,4 +88,9 @@ class DayEdit extends Component
|
||||
$this->dispatch('dayUpdated', dayIndex: $this->day, data: $this->portions);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getListeners(): array
|
||||
{
|
||||
return ['takePaste'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,72 +3,75 @@
|
||||
namespace App\Livewire\Diet;
|
||||
|
||||
use App\Models\Diet;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
class Edit extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public Diet $diet;
|
||||
|
||||
public $name = '';
|
||||
public string $name = '';
|
||||
|
||||
public $image;
|
||||
public UploadedFile|string $image = '';
|
||||
|
||||
public $duration = '7';
|
||||
public int|string $duration = '7';
|
||||
|
||||
public $state = 'draft';
|
||||
public string $state = 'draft';
|
||||
|
||||
public $type = [];
|
||||
public array $type = [];
|
||||
|
||||
public $details = '';
|
||||
public string $details = '';
|
||||
|
||||
public $days = [];
|
||||
public array $days = [];
|
||||
|
||||
public $copyedDay;
|
||||
public string $copyedDay;
|
||||
|
||||
public $copyedDayData;
|
||||
public array $copyedDayData;
|
||||
|
||||
protected $listeners = ['dayUpdated', 'makeCopy', 'makePaste'];
|
||||
|
||||
public function mount(Diet $diet)
|
||||
public function mount(Diet $diet): void
|
||||
{
|
||||
$this->name = $diet->name;
|
||||
$this->image = $diet->image;
|
||||
$this->duration = $diet->duration;
|
||||
$this->state = $diet->state;
|
||||
$this->type = json_decode($diet->type, true);
|
||||
$this->type = $diet->type;
|
||||
$this->details = $diet->details;
|
||||
$this->days = json_decode($diet->daily_schedule, true);
|
||||
$this->days = $diet->daily_schedule;
|
||||
}
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.diet.edit');
|
||||
}
|
||||
|
||||
public function dayUpdated($dayIndex, $data)
|
||||
public function dayUpdated(string $dayIndex, array $data): void
|
||||
{
|
||||
$this->days[$dayIndex] = $data;
|
||||
}
|
||||
|
||||
public function showPreview()
|
||||
public function showPreview(): void
|
||||
{
|
||||
$this->state = 'draft';
|
||||
$this->publish();
|
||||
}
|
||||
|
||||
public function addToDrafts()
|
||||
public function addToDrafts(): void
|
||||
{
|
||||
$this->state = 'draft';
|
||||
$this->publish();
|
||||
}
|
||||
|
||||
public function makeCopy($day)
|
||||
public function makeCopy(string $day): void
|
||||
{
|
||||
/* dd($day); */
|
||||
$this->copyedDay = $day;
|
||||
}
|
||||
|
||||
public function makePaste($copyToDay)
|
||||
public function makePaste(string $copyToDay): void
|
||||
{
|
||||
$data = $this->days[$this->copyedDay];
|
||||
$this->dispatch(
|
||||
@@ -78,7 +81,7 @@ class Edit extends Component
|
||||
)->to('diet.day-edit');
|
||||
}
|
||||
|
||||
public function publish()
|
||||
public function publish(): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
@@ -86,26 +89,32 @@ class Edit extends Component
|
||||
|
||||
$diet->name = $this->name;
|
||||
/* $diet->image = $this->image->store('diets', 'public'); */
|
||||
$diet->image = ! is_string($this->image) ? $this->image->store('diets') : $this->image;
|
||||
$diet->duration = $this->duration;
|
||||
$diet->state = $this->state;
|
||||
$diet->type = json_encode($this->type);
|
||||
$diet->type = $this->type;
|
||||
$diet->details = $this->details;
|
||||
$diet->daily_schedule = json_encode($this->days);
|
||||
$diet->daily_schedule = $this->days;
|
||||
$diet->save();
|
||||
|
||||
$this->redirectRoute('diet.show', $diet);
|
||||
|
||||
/* $user = Auth::user(); */
|
||||
/* $user->author_of_diets()->attach($diet->id); */
|
||||
|
||||
return redirect()->route('diet.show', $diet->id);
|
||||
}
|
||||
|
||||
protected function rules()
|
||||
protected function getListeners(): array
|
||||
{
|
||||
return ['dayUpdated', 'makeCopy', 'makePaste'];
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|min:3',
|
||||
/* 'image' => 'required|image|max:2048', */
|
||||
'duration' => 'required|numeric|min:7',
|
||||
'state' => 'required|in:draft,active,inactive',
|
||||
'state' => 'required|in:active,draft,archived',
|
||||
'type' => ['required', 'array:meat,veg,corb,alko', 'min:1'],
|
||||
'details' => 'required|min:3',
|
||||
];
|
||||
|
||||
@@ -3,20 +3,21 @@
|
||||
namespace App\Livewire\Diet;
|
||||
|
||||
use App\Models\Diet;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
|
||||
class Like extends Component
|
||||
{
|
||||
public Diet $diet;
|
||||
|
||||
public $isIconOnly = false;
|
||||
public bool $isIconOnly = false;
|
||||
|
||||
public function mount(Diet $diet)
|
||||
public function mount(Diet $diet): void
|
||||
{
|
||||
$this->diet = $diet;
|
||||
}
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.diet.like');
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Livewire\Diet;
|
||||
use App\Events\DietSelected;
|
||||
use App\Models\Diet;
|
||||
use App\Models\User;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
|
||||
class StartButton extends Component
|
||||
@@ -13,19 +14,19 @@ class StartButton extends Component
|
||||
|
||||
public User $user;
|
||||
|
||||
public $chosenDietState;
|
||||
public string $chosenDietState;
|
||||
|
||||
public function mount()
|
||||
public function mount(): void
|
||||
{
|
||||
$this->chosenDietState = data_get($this->user->chosen_diet, 'last_state', 'oops');
|
||||
}
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.diet.start-button');
|
||||
}
|
||||
|
||||
public function start()
|
||||
public function start(): void
|
||||
{
|
||||
DietSelected::dispatch(
|
||||
$this->diet,
|
||||
@@ -33,7 +34,7 @@ class StartButton extends Component
|
||||
);
|
||||
}
|
||||
|
||||
public function getLastState()
|
||||
public function getLastState(): void
|
||||
{
|
||||
$this->chosenDietState = data_get($this->user->chosen_diet, 'last_state', 'oops');
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* App\Models\BlogPostLike
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Iben\Statable\Statable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* App\Models\ChosenDiet
|
||||
@@ -46,27 +47,27 @@ class ChosenDiet extends Model
|
||||
'start_date',
|
||||
];
|
||||
|
||||
public function getActorId()
|
||||
public function getActorId(): int
|
||||
{
|
||||
return $this->user_id;
|
||||
}
|
||||
|
||||
public function user()
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function diet()
|
||||
public function diet(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Diet::class);
|
||||
}
|
||||
|
||||
protected function getGraph()
|
||||
protected function getGraph(): string
|
||||
{
|
||||
return 'chosen_diet';
|
||||
}
|
||||
|
||||
protected function saveBeforeTransition()
|
||||
protected function saveBeforeTransition(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
@@ -64,20 +65,30 @@ class Diet extends Model implements Viewable
|
||||
{
|
||||
use HasFactory, InteractsWithViews;
|
||||
|
||||
protected $withCount = [
|
||||
'likes',
|
||||
'comments',
|
||||
'choses',
|
||||
protected $casts = [
|
||||
'type' => 'array',
|
||||
'daily_schedule' => 'array',
|
||||
];
|
||||
|
||||
private $allowedDietTypes = [
|
||||
private array $allowedDietTypes = [
|
||||
'alko',
|
||||
'corb',
|
||||
'meat',
|
||||
'veg',
|
||||
];
|
||||
|
||||
public function author()
|
||||
// protected $withCount = [
|
||||
// 'likes',
|
||||
// 'comments',
|
||||
// 'choses',
|
||||
// ];
|
||||
|
||||
public static function getWithLikesCommentsChosesCount(): Builder
|
||||
{
|
||||
return self::withCount(['likes', 'comments', 'choses']);
|
||||
}
|
||||
|
||||
public function author(): BelongsToMany
|
||||
{
|
||||
return $this
|
||||
->belongsToMany(User::class, 'diet_author', 'diet_id', 'user_id');
|
||||
@@ -90,7 +101,7 @@ class Diet extends Model implements Viewable
|
||||
);*/
|
||||
}
|
||||
|
||||
public function comments()
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(DietComment::class);
|
||||
}
|
||||
@@ -100,7 +111,7 @@ class Diet extends Model implements Viewable
|
||||
return $this->hasMany(DietLike::class);
|
||||
}
|
||||
|
||||
public function isLiked(): bool
|
||||
public function isLiked(): bool|int
|
||||
{
|
||||
if (auth()->user()) {
|
||||
return auth()->user()->diet_likes()->forDiet($this)->count();
|
||||
@@ -109,7 +120,7 @@ class Diet extends Model implements Viewable
|
||||
return false;
|
||||
}
|
||||
|
||||
public function removeLike(): bool
|
||||
public function removeLike(): bool|int
|
||||
{
|
||||
if (auth()->user()) {
|
||||
return auth()->user()->diet_likes()->forDiet($this)->delete();
|
||||
@@ -120,7 +131,7 @@ class Diet extends Model implements Viewable
|
||||
|
||||
public function types(): Attribute
|
||||
{
|
||||
$data = $this->unserializeOptionsArr($this->type);
|
||||
$data = $this->type;
|
||||
|
||||
return new Attribute(
|
||||
get: fn () => array_keys($data),
|
||||
@@ -129,60 +140,51 @@ class Diet extends Model implements Viewable
|
||||
|
||||
public function schedules(): Attribute
|
||||
{
|
||||
$data = $this->unserializeOptionsArr($this->daily_schedule);
|
||||
$data = $this->daily_schedule;
|
||||
|
||||
return new Attribute(
|
||||
get: fn () => $data,
|
||||
);
|
||||
}
|
||||
|
||||
public function choses()
|
||||
public function choses(): HasMany
|
||||
{
|
||||
return $this->hasMany(ChosenDiet::class);
|
||||
}
|
||||
|
||||
public function scopeDurationMin(Builder $query, $days): Builder
|
||||
public function scopeDurationMin(Builder $query, string $days): Builder
|
||||
{
|
||||
return $query->where('duration', '>=', $days);
|
||||
}
|
||||
|
||||
public function scopeDurationMax(Builder $query, $days): Builder
|
||||
public function scopeDurationMax(Builder $query, string $days): Builder
|
||||
{
|
||||
return $query->where('duration', '<=', $days);
|
||||
}
|
||||
|
||||
public function scopeInvolvedMin(Builder $query, $number): Builder
|
||||
public function scopeInvolvedMin(Builder $query, string $number): Builder
|
||||
{
|
||||
return $query->having('choses_count', '>=', $number);
|
||||
}
|
||||
|
||||
public function scopeInvolvedMax(Builder $query, $number): Builder
|
||||
public function scopeInvolvedMax(Builder $query, string $number): Builder
|
||||
{
|
||||
return $query->having('choses_count', '<=', $number);
|
||||
}
|
||||
|
||||
public function scopeContainsType(Builder $query, $types): Builder
|
||||
public function scopeContainsType(Builder $query, array $types): Builder
|
||||
{
|
||||
$i = 0;
|
||||
|
||||
foreach ($types as $type) {
|
||||
if (in_array($type, $this->allowedDietTypes)) {
|
||||
$query = $i === 0
|
||||
? $query->whereJsonContainsKey('type->'.$type)
|
||||
: $query->orWhereJsonContainsKey('type->'.$type);
|
||||
? $query->whereJsonContainsKey('type->' . $type)
|
||||
: $query->orWhereJsonContainsKey('type->' . $type);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function unserializeOptionsArr($str)
|
||||
{
|
||||
if (preg_match('/^".*"$/', $str)) {
|
||||
return json_decode(stripslashes(mb_substr($str, 1, -1)), true);
|
||||
}
|
||||
|
||||
return json_decode(stripslashes($str), true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 DietComment extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public function author()
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function diet()
|
||||
public function diet(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Diet::class, 'diet_id');
|
||||
}
|
||||
@@ -54,7 +55,7 @@ class DietComment extends Model
|
||||
return $this->hasMany(DietCommentUpvote::class, 'comment_id');
|
||||
}
|
||||
|
||||
public function isUpVoted(): bool
|
||||
public function isUpVoted(): bool|int
|
||||
{
|
||||
if (auth()->user()) {
|
||||
return auth()
|
||||
@@ -67,7 +68,7 @@ class DietComment extends Model
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isDownVoted(): bool
|
||||
public function isDownVoted(): bool|int
|
||||
{
|
||||
if (auth()->user()) {
|
||||
return auth()
|
||||
@@ -80,7 +81,7 @@ class DietComment extends Model
|
||||
return false;
|
||||
}
|
||||
|
||||
public function removeUpVote(): bool
|
||||
public function removeUpVote(): bool|int
|
||||
{
|
||||
if (auth()->user()) {
|
||||
return auth()
|
||||
|
||||
@@ -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\DietCommentUpvote
|
||||
@@ -42,22 +44,22 @@ class DietCommentUpvote extends Model
|
||||
'type',
|
||||
];
|
||||
|
||||
public function author()
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function diet_comment()
|
||||
public function diet_comment(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(DietComment::class, 'comment_id');
|
||||
}
|
||||
|
||||
public function scopeForDietCommentsUpVote($query, DietComment $comment)
|
||||
public function scopeForDietCommentsUpVote(Builder $query, DietComment $comment): Builder
|
||||
{
|
||||
return $query->where('comment_id', $comment->id)->where('type', 'upvote');
|
||||
}
|
||||
|
||||
public function scopeForDietCommentsDownVote($query, DietComment $comment)
|
||||
public function scopeForDietCommentsDownVote(Builder $query, DietComment $comment): Builder
|
||||
{
|
||||
return $query->where('comment_id', $comment->id)->where('type', 'downvote');
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -34,7 +35,7 @@ class DietLike extends Model
|
||||
'user_id',
|
||||
];
|
||||
|
||||
public function scopeForDiet($query, Diet $diet)
|
||||
public function scopeForDiet(Builder $query, Diet $diet): Builder
|
||||
{
|
||||
return $query->where('diet_id', $diet->id);
|
||||
}
|
||||
|
||||
@@ -11,39 +11,23 @@ class DietStarted extends Notification implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public $chosenDiet;
|
||||
public object $chosenDiet;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($chosenDiet)
|
||||
public function __construct(object $chosenDiet)
|
||||
{
|
||||
$this->afterCommit();
|
||||
$this->chosenDiet = $chosenDiet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function via($notifiable)
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return ['mail'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return \Illuminate\Notifications\Messages\MailMessage
|
||||
*/
|
||||
public function toMail($notifiable)
|
||||
public function toMail(object $notifiable): MailMessage
|
||||
{
|
||||
return (new MailMessage())
|
||||
->line('you are successful started diet named: '.$this->chosenDiet->diet->name)
|
||||
@@ -51,14 +35,7 @@ class DietStarted extends Notification implements ShouldQueue
|
||||
->line('Thank you for using our application!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($notifiable)
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return [
|
||||
|
||||
|
||||
@@ -2,20 +2,18 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use FakerRestaurant\Provider\en_US\Restaurant;
|
||||
use App\Models\Diet;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Http\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Faker\Generator;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Diet>
|
||||
* @property Generator|Restaurant $faker
|
||||
*/
|
||||
class DietFactory extends Factory
|
||||
{
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
@@ -26,13 +24,13 @@ class DietFactory extends Factory
|
||||
$faker = \Faker\Factory::create();
|
||||
$this->faker->addProvider(new Restaurant($faker));
|
||||
$dietDuration = $this->faker->randomElement([7, 14, 21]);
|
||||
$dietState = $this->faker->randomElement(['active','draft','archived']);
|
||||
$dietState = $this->faker->randomElement(['active', 'draft', 'archived']);
|
||||
$numberOfDefinedDietTypes = $this->faker->numberBetween(1, 4);
|
||||
$expectedDietTypes = ['meat', 'veg', 'corb', 'alko'];
|
||||
$expectedDietTypes = ['meat', 'veg', 'corb', 'alko'];
|
||||
$selectedDietTypes = $this->faker->randomElements($expectedDietTypes, $numberOfDefinedDietTypes);
|
||||
$dietTypes = array_reduce(
|
||||
$selectedDietTypes,
|
||||
function($acc, $el) {
|
||||
function ($acc, $el) {
|
||||
$acc[$el] = true;
|
||||
return $acc;
|
||||
},
|
||||
@@ -47,12 +45,11 @@ class DietFactory extends Factory
|
||||
$file = new File($imageFilePath);
|
||||
$fileLocalPath = Storage::putFile('diets', $file, 'public');
|
||||
|
||||
for($day = 1; $day <= $dietDuration ; $day++) {
|
||||
foreach($dietEatingTypes as $eatingType) {
|
||||
|
||||
for ($day = 1; $day <= $dietDuration; $day++) {
|
||||
foreach ($dietEatingTypes as $eatingType) {
|
||||
$numberOfProductsInCurrentMeel = $this->faker->numberBetween(1, 4);
|
||||
|
||||
for(;$numberOfProductsInCurrentMeel > 0; $numberOfProductsInCurrentMeel--) {
|
||||
for (; $numberOfProductsInCurrentMeel > 0; $numberOfProductsInCurrentMeel--) {
|
||||
$meelName = $this->faker->foodName();
|
||||
$dailySchedule[$day][$eatingType][] = [
|
||||
'name' => $meelName,
|
||||
@@ -77,7 +74,7 @@ class DietFactory extends Factory
|
||||
$dietNamePrefix = $this->faker->randomElement(['Super', 'Keto', 'Fast', 'Detox', 'Power Of', 'Green', 'Healthy', 'Turbo']);
|
||||
|
||||
return [
|
||||
'name' => $dietNamePrefix . " " . $dietName . " Diet",
|
||||
'name' => $dietNamePrefix . ' ' . $dietName . ' Diet',
|
||||
'duration' => $dietDuration,
|
||||
'state' => $dietState,
|
||||
'image' => $fileLocalPath,
|
||||
|
||||
@@ -18,6 +18,7 @@ class DatabaseSeeder extends Seeder
|
||||
$this->call(RecipeAuthor::class);
|
||||
$this->call(RecipeCommentSeeder::class);
|
||||
$this->call(RecipeTagSeeder::class);
|
||||
$this->call(DietsSeeder::class);
|
||||
$this->call(BlogPostSeeder::class);
|
||||
}
|
||||
}
|
||||
|
||||
187
tests/Feature/Diet/CreateTest.php
Normal file
187
tests/Feature/Diet/CreateTest.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Diet;
|
||||
|
||||
use App\Livewire\Diet\Create;
|
||||
use App\Models\Diet;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Testing\File;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CreateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private File $image;
|
||||
|
||||
private Diet $diet;
|
||||
|
||||
protected User $user;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->refreshApplicationWithLocale('en');
|
||||
|
||||
Storage::fake('tmp-for-tests');
|
||||
$this->image = UploadedFile::fake()->image('diet.png');
|
||||
|
||||
$this->user = User::factory()->create(['is_consultant' => true]);
|
||||
$this->diet = Diet::factory()->make();
|
||||
}
|
||||
|
||||
public function test_show_form_create_diet(): void
|
||||
{
|
||||
$response = $this
|
||||
->actingAs($this->user)
|
||||
->get(route('diet.create'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_publish_new_diet_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$dietCountAtStart = Diet::count();
|
||||
|
||||
$testCase = Livewire::test(Create::class)
|
||||
->set('name', $this->diet->name)
|
||||
->assertSet('name', $this->diet->name)
|
||||
->set('image', $this->image)
|
||||
->set('duration', $this->diet->duration)
|
||||
->assertSet('duration', $this->diet->duration)
|
||||
->set('state', $this->diet->state)
|
||||
->assertSet('state', $this->diet->state)
|
||||
->set('type', $this->diet->type)
|
||||
->assertSet('type', $this->diet->type)
|
||||
->set('details', $this->diet->details)
|
||||
->assertSet('details', $this->diet->details)
|
||||
->set('daily_schedule', $this->diet->daily_schedule)
|
||||
->assertSet('daily_schedule', $this->diet->daily_schedule)
|
||||
->call('publish')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['name', 'image', 'duration', 'state', 'details', 'daily_schedule', 'type']);
|
||||
|
||||
$lastCreatedDiet = Diet::orderBy('id', 'desc')->first();
|
||||
|
||||
|
||||
$this->assertEquals($dietCountAtStart + 1, Diet::count());
|
||||
|
||||
$newDietUrl = route('diet.show', $lastCreatedDiet);
|
||||
|
||||
$testCase->assertRedirect($newDietUrl);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($lastCreatedDiet->image);
|
||||
|
||||
|
||||
$response = $this->actingAs($this->user)->get(route('diet.index'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_to_draft_new_diet_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$dietCountAtStart = Diet::count();
|
||||
|
||||
$testCase = Livewire::test(Create::class)
|
||||
->set('name', $this->diet->name)
|
||||
->assertSet('name', $this->diet->name)
|
||||
->set('image', $this->image)
|
||||
->set('duration', $this->diet->duration)
|
||||
->assertSet('duration', $this->diet->duration)
|
||||
->set('state', $this->diet->state)
|
||||
->assertSet('state', $this->diet->state)
|
||||
->set('type', $this->diet->type)
|
||||
->assertSet('type', $this->diet->type)
|
||||
->set('details', $this->diet->details)
|
||||
->assertSet('details', $this->diet->details)
|
||||
->set('daily_schedule', $this->diet->daily_schedule)
|
||||
->assertSet('daily_schedule', $this->diet->daily_schedule)
|
||||
->call('addToDrafts')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['name', 'image', 'duration', 'details', 'daily_schedule']);
|
||||
|
||||
self::assertEquals('draft', $testCase->state);
|
||||
|
||||
$lastCreatedDiet = Diet::orderBy('id', 'desc')->first();
|
||||
|
||||
$newDietUrl = route('diet.show', $lastCreatedDiet);
|
||||
|
||||
$testCase->assertRedirect($newDietUrl);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($lastCreatedDiet->image);
|
||||
|
||||
$this->assertEquals($dietCountAtStart + 1, Diet::count());
|
||||
|
||||
$response = $this->actingAs($this->user)->get(route('diet.index'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_to_preview_new_diet_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$dietCountAtStart = Diet::count();
|
||||
|
||||
$testCase = Livewire::test(Create::class)
|
||||
->set('name', $this->diet->name)
|
||||
->assertSet('name', $this->diet->name)
|
||||
->set('image', $this->image)
|
||||
->set('duration', $this->diet->duration)
|
||||
->assertSet('duration', $this->diet->duration)
|
||||
->set('state', $this->diet->state)
|
||||
->assertSet('state', $this->diet->state)
|
||||
->set('type', $this->diet->type)
|
||||
->assertSet('type', $this->diet->type)
|
||||
->set('details', $this->diet->details)
|
||||
->assertSet('details', $this->diet->details)
|
||||
->set('daily_schedule', $this->diet->daily_schedule)
|
||||
->assertSet('daily_schedule', $this->diet->daily_schedule)
|
||||
->call('showPreview')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['name', 'image', 'duration', 'details', 'daily_schedule']);
|
||||
|
||||
self::assertEquals('draft', $testCase->state);
|
||||
|
||||
$lastCreatedDiet = Diet::orderBy('id', 'desc')->first();
|
||||
|
||||
$newDietUrl = route('diet.show', $lastCreatedDiet);
|
||||
|
||||
$testCase->assertRedirect($newDietUrl);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($lastCreatedDiet->image);
|
||||
|
||||
$this->assertEquals($dietCountAtStart + 1, Diet::count());
|
||||
|
||||
$response = $this->actingAs($this->user)->get(route('diet.index'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_set_empty_data(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$dietCountAtStart = Diet::count();
|
||||
|
||||
Livewire::test(Create::class)
|
||||
->set('name', null)
|
||||
->set('image', null)
|
||||
->set('duration', null)
|
||||
->set('state', null)
|
||||
->set('type', null)
|
||||
->set('details', null)
|
||||
->set('daily_schedule', null)
|
||||
->call('publish')
|
||||
->assertHasErrors(['name', 'image', 'details', 'type']);
|
||||
|
||||
$this->assertEquals($dietCountAtStart, Diet::count());
|
||||
}
|
||||
}
|
||||
204
tests/Feature/Diet/EditTest.php
Normal file
204
tests/Feature/Diet/EditTest.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Diet;
|
||||
|
||||
use App\Models\Diet;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
use App\Livewire\Diet\Edit;
|
||||
use Livewire\Livewire;
|
||||
|
||||
class EditTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private UploadedFile $image;
|
||||
|
||||
private Diet $diet;
|
||||
|
||||
protected User $user;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->refreshApplicationWithLocale('en');
|
||||
|
||||
Storage::fake('tmp-for-tests');
|
||||
$this->image = UploadedFile::fake()->image('diet.png');
|
||||
|
||||
$this->user = User::factory()->create(['is_consultant' => true]);
|
||||
$this->diet = Diet::factory()->create();
|
||||
}
|
||||
|
||||
public function test_show_form_edit_diet(): void
|
||||
{
|
||||
$response = $this
|
||||
->actingAs($this->user)
|
||||
->get(route('diet.edit', $this->diet));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_publish_new_diet_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$updatedDietBeforeToAddDatabase = Diet::factory()->make();
|
||||
|
||||
$testCase = Livewire::test(Edit::class, [
|
||||
'diet' => $this->diet,
|
||||
])
|
||||
->set('name', $updatedDietBeforeToAddDatabase->name)
|
||||
->assertSet('name', $updatedDietBeforeToAddDatabase->name)
|
||||
->set('image', $this->image)
|
||||
->set('duration', $updatedDietBeforeToAddDatabase->duration)
|
||||
->assertSet('duration', $updatedDietBeforeToAddDatabase->duration)
|
||||
->set('state', $updatedDietBeforeToAddDatabase->state)
|
||||
->assertSet('state', $updatedDietBeforeToAddDatabase->state)
|
||||
->set('type', $updatedDietBeforeToAddDatabase->type)
|
||||
->assertSet('type', $updatedDietBeforeToAddDatabase->type)
|
||||
->set('details', $updatedDietBeforeToAddDatabase->details)
|
||||
->assertSet('details', $updatedDietBeforeToAddDatabase->details)
|
||||
->set('daily_schedule', $this->diet->daily_schedule)
|
||||
->assertSet('daily_schedule', $this->diet->daily_schedule)
|
||||
->call('publish')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['name', 'image', 'duration', 'state', 'details', 'daily_schedule', 'type']);
|
||||
|
||||
$updatedDietAfterToAddDatabase = Diet::orderByDesc('id')->first();
|
||||
|
||||
$newDietUrl = route('diet.show', $updatedDietAfterToAddDatabase);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($updatedDietAfterToAddDatabase->image);
|
||||
|
||||
|
||||
// check data after update recipe
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->name, $updatedDietAfterToAddDatabase->name);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->duration, $updatedDietAfterToAddDatabase->duration);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->state, $updatedDietAfterToAddDatabase->state);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->type, $updatedDietAfterToAddDatabase->type);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->details, $updatedDietAfterToAddDatabase->details);
|
||||
// self::assertEquals($updatedDietBeforeToAddDatabase->daily_schedule, $updatedDietAfterToAddDatabase->daily_schedule);
|
||||
|
||||
$testCase->assertRedirect($newDietUrl);
|
||||
|
||||
}
|
||||
|
||||
public function test_to_draft_new_diet_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$updatedDietBeforeToAddDatabase = Diet::factory()->make();
|
||||
|
||||
$testCase = Livewire::test(Edit::class, [
|
||||
'diet' => $this->diet,
|
||||
])
|
||||
->set('name', $updatedDietBeforeToAddDatabase->name)
|
||||
->assertSet('name', $updatedDietBeforeToAddDatabase->name)
|
||||
->set('image', $this->image)
|
||||
->set('duration', $updatedDietBeforeToAddDatabase->duration)
|
||||
->assertSet('duration', $updatedDietBeforeToAddDatabase->duration)
|
||||
->set('state', $updatedDietBeforeToAddDatabase->state)
|
||||
->assertSet('state', $updatedDietBeforeToAddDatabase->state)
|
||||
->set('type', $updatedDietBeforeToAddDatabase->type)
|
||||
->assertSet('type', $updatedDietBeforeToAddDatabase->type)
|
||||
->set('details', $updatedDietBeforeToAddDatabase->details)
|
||||
->assertSet('details', $updatedDietBeforeToAddDatabase->details)
|
||||
->set('daily_schedule', $this->diet->daily_schedule)
|
||||
->assertSet('daily_schedule', $this->diet->daily_schedule)
|
||||
->call('addToDrafts')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['name', 'image', 'duration', 'state', 'details', 'days', 'type']);
|
||||
|
||||
$updatedDietAfterToAddDatabase = Diet::orderByDesc('id')->first();
|
||||
|
||||
$newDietUrl = route('diet.show', $updatedDietAfterToAddDatabase);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($updatedDietAfterToAddDatabase->image);
|
||||
|
||||
|
||||
// check data after update recipe
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->name, $updatedDietAfterToAddDatabase->name);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->duration, $updatedDietAfterToAddDatabase->duration);
|
||||
self::assertEquals('draft', $updatedDietAfterToAddDatabase->state);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->type, $updatedDietAfterToAddDatabase->type);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->details, $updatedDietAfterToAddDatabase->details);
|
||||
// self::assertEquals($updatedDietBeforeToAddDatabase->daily_schedule, $updatedDietAfterToAddDatabase->daily_schedule);
|
||||
|
||||
$testCase->assertRedirect($newDietUrl);
|
||||
}
|
||||
|
||||
public function test_to_preview_new_diet_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$updatedDietBeforeToAddDatabase = Diet::factory()->make();
|
||||
|
||||
$testCase = Livewire::test(Edit::class, [
|
||||
'diet' => $this->diet,
|
||||
])
|
||||
->set('name', $updatedDietBeforeToAddDatabase->name)
|
||||
->assertSet('name', $updatedDietBeforeToAddDatabase->name)
|
||||
->set('image', $this->image)
|
||||
->set('duration', $updatedDietBeforeToAddDatabase->duration)
|
||||
->assertSet('duration', $updatedDietBeforeToAddDatabase->duration)
|
||||
->set('state', $updatedDietBeforeToAddDatabase->state)
|
||||
->assertSet('state', $updatedDietBeforeToAddDatabase->state)
|
||||
->set('type', $updatedDietBeforeToAddDatabase->type)
|
||||
->assertSet('type', $updatedDietBeforeToAddDatabase->type)
|
||||
->set('details', $updatedDietBeforeToAddDatabase->details)
|
||||
->assertSet('details', $updatedDietBeforeToAddDatabase->details)
|
||||
->set('daily_schedule', $this->diet->daily_schedule)
|
||||
->assertSet('daily_schedule', $this->diet->daily_schedule)
|
||||
->call('showPreview')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['name', 'image', 'duration', 'state', 'details', 'days', 'type']);
|
||||
|
||||
$updatedDietAfterToAddDatabase = Diet::orderByDesc('id')->first();
|
||||
|
||||
$newDietUrl = route('diet.show', $updatedDietAfterToAddDatabase);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($updatedDietAfterToAddDatabase->image);
|
||||
|
||||
|
||||
// check data after update recipe
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->name, $updatedDietAfterToAddDatabase->name);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->duration, $updatedDietAfterToAddDatabase->duration);
|
||||
self::assertEquals('draft', $updatedDietAfterToAddDatabase->state);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->type, $updatedDietAfterToAddDatabase->type);
|
||||
self::assertEquals($updatedDietBeforeToAddDatabase->details, $updatedDietAfterToAddDatabase->details);
|
||||
// self::assertEquals($updatedDietBeforeToAddDatabase->daily_schedule, $updatedDietAfterToAddDatabase->daily_schedule);
|
||||
|
||||
$testCase->assertRedirect($newDietUrl);
|
||||
}
|
||||
|
||||
public function testNothingChange(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$dietUpdate = Livewire::test(Edit::class, [
|
||||
'diet' => $this->diet,
|
||||
])
|
||||
->call('publish')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['name', 'image', 'duration', 'state', 'details', 'days', 'type']);
|
||||
|
||||
$newDietUrl = route('diet.show', $this->diet, false);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertMissing($dietUpdate->image);
|
||||
|
||||
// check data after update recipe
|
||||
self::assertEquals($this->diet->name, $dietUpdate->name);
|
||||
self::assertEquals($this->diet->duration, $dietUpdate->duration);
|
||||
self::assertEquals($this->diet->state, $dietUpdate->state);
|
||||
self::assertEquals($this->diet->type, $dietUpdate->type);
|
||||
self::assertEquals($this->diet->details, $dietUpdate->details);
|
||||
self::assertEquals($this->diet->daily_schedule, $dietUpdate->days);
|
||||
|
||||
$dietUpdate->assertRedirect($newDietUrl);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
31
tests/Feature/Diet/IndexTest.php
Normal file
31
tests/Feature/Diet/IndexTest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Diet;
|
||||
|
||||
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('plan.index'));
|
||||
|
||||
$response
|
||||
->assertOk();
|
||||
}
|
||||
}
|
||||
31
tests/Feature/Diet/ShowTest.php
Normal file
31
tests/Feature/Diet/ShowTest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Diet;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User;
|
||||
use App\Models\Diet;
|
||||
|
||||
class ShowTest extends TestCase
|
||||
{
|
||||
protected User $user;
|
||||
private Diet $diet;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->refreshApplicationWithLocale('en');
|
||||
|
||||
$this->user = User::factory()->create(['is_consultant' => true]);
|
||||
$this->diet = Diet::inRandomOrder()->first();
|
||||
}
|
||||
|
||||
public function test_show_page_success(): void
|
||||
{
|
||||
$response = $this
|
||||
->actingAs($this->user)
|
||||
->get(route('diet.show', $this->diet));
|
||||
|
||||
$response
|
||||
->assertOk();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user