Skip to content

Commit

Permalink
Product Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarm committed Apr 24, 2024
1 parent 91f101a commit 1037284
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Product extends Model
{
use SoftDeletes, HasFactory;
use HasFactory, SoftDeletes;

protected $fillable = [
'name',
Expand Down
41 changes: 0 additions & 41 deletions app/Policies/ProductPolicy.php

This file was deleted.

5 changes: 4 additions & 1 deletion database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
*/
class ProductFactory extends Factory
{
protected $model = Product::class;
Expand All @@ -19,7 +22,7 @@ public function definition(): array
'slug' => $this->faker->slug(),
'description' => $this->faker->text(),
'price' => $this->faker->numberBetween(1500, 6000),
'image' => $this->faker->imageUrl(640, 480, 'animals')
'image' => $this->faker->imageUrl(640, 480, 'animals'),
];
}
}
1 change: 0 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Database\Factories;

use Database\Factories\Concerns\RefreshOnCreate;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
Expand Down
1 change: 0 additions & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Product;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Database\Factories\ProductFactory;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
Expand Down
3 changes: 3 additions & 0 deletions resources/views/product/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-app-layout>
<h1>Products</h1>
</x-app-layout>
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
->middleware(['auth'])
->name('profile');

Route::view('products', 'product.index')
->middleware(['auth'])
->name('products.index');

require __DIR__.'/auth.php';
21 changes: 21 additions & 0 deletions tests/Feature/Product/FeedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use App\Models\User;

test('guest', function () {
$response = $this->get(route('products.index'));

$response->assertRedirect(route('login'));
});

test('auth', function () {
$user = User::factory()->create();

$response = $this->actingAs($user)->get(route('products.index'));

$response->assertStatus(200)
->assertViewIs('product.index')
->assertSee('Products');
});
2 changes: 1 addition & 1 deletion tests/Unit/Models/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Models\Product;

test('to product array', function () {
test('to array', function () {
$product = Product::factory()->create()->fresh();

expect(array_keys($product->toArray()))->toBe([
Expand Down

0 comments on commit 1037284

Please sign in to comment.