AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Store;
  4. use App\Models\User;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Inertia\Testing\AssertableInertia as Assert;
  7. use Tests\TestCase;
  8. class StoreTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. /**
  12. * @return array<string, string>
  13. */
  14. private function validData(): array
  15. {
  16. return [
  17. 'name' => 'Sambal Nenek',
  18. 'category' => 'food',
  19. 'description' => 'Homemade sambal from a family recipe, premium and warm.',
  20. 'language' => 'ms',
  21. ];
  22. }
  23. public function test_guests_cannot_reach_store_pages()
  24. {
  25. $this->get(route('stores.create'))->assertRedirect(route('login'));
  26. $this->post(route('stores.store'), $this->validData())->assertRedirect(route('login'));
  27. $this->assertDatabaseCount('stores', 0);
  28. }
  29. public function test_a_user_can_create_a_store()
  30. {
  31. $user = User::factory()->create();
  32. $response = $this->actingAs($user)->post(route('stores.store'), $this->validData());
  33. $response->assertRedirect(route('stores.show', 'sambal-nenek'));
  34. $this->assertDatabaseHas('stores', [
  35. 'user_id' => $user->id,
  36. 'name' => 'Sambal Nenek',
  37. 'slug' => 'sambal-nenek',
  38. 'category' => 'food',
  39. 'language' => 'ms',
  40. 'status' => 'draft',
  41. ]);
  42. }
  43. public function test_store_details_are_validated()
  44. {
  45. $user = User::factory()->create();
  46. $response = $this->actingAs($user)->post(route('stores.store'), [
  47. 'name' => '',
  48. 'category' => 'not-a-category',
  49. 'description' => 'too short',
  50. 'language' => 'fr',
  51. ]);
  52. $response->assertSessionHasErrors(['name', 'category', 'description', 'language']);
  53. $this->assertDatabaseCount('stores', 0);
  54. }
  55. public function test_a_user_cannot_set_the_owner_or_status_of_a_store()
  56. {
  57. $user = User::factory()->create();
  58. $other = User::factory()->create();
  59. $this->actingAs($user)->post(route('stores.store'), [
  60. ...$this->validData(),
  61. 'user_id' => $other->id,
  62. 'status' => 'published',
  63. 'slug' => 'admin',
  64. ]);
  65. $store = Store::query()->sole();
  66. $this->assertSame($user->id, $store->user_id);
  67. $this->assertSame('draft', $store->status->value);
  68. $this->assertSame('sambal-nenek', $store->slug);
  69. }
  70. public function test_stores_with_the_same_name_get_different_slugs()
  71. {
  72. $user = User::factory()->create();
  73. $this->actingAs($user);
  74. $this->post(route('stores.store'), $this->validData());
  75. $this->post(route('stores.store'), $this->validData());
  76. $this->assertSame(['sambal-nenek', 'sambal-nenek-2'], Store::query()->orderBy('id')->pluck('slug')->all());
  77. }
  78. public function test_reserved_words_are_not_used_as_slugs()
  79. {
  80. $this->assertSame('admin-2', Store::uniqueSlug('Admin'));
  81. }
  82. public function test_the_dashboard_lists_only_the_users_own_stores()
  83. {
  84. $user = User::factory()->create();
  85. $mine = Store::factory()->for($user)->create();
  86. Store::factory()->create();
  87. $this->actingAs($user)
  88. ->get(route('dashboard'))
  89. ->assertOk()
  90. ->assertInertia(fn (Assert $page) => $page
  91. ->component('dashboard')
  92. ->has('stores', 1)
  93. ->where('stores.0.slug', $mine->slug));
  94. }
  95. public function test_a_user_can_view_their_own_store()
  96. {
  97. $user = User::factory()->create();
  98. $store = Store::factory()->for($user)->create();
  99. $this->actingAs($user)
  100. ->get(route('stores.show', $store->slug))
  101. ->assertOk()
  102. ->assertInertia(fn (Assert $page) => $page
  103. ->component('stores/show')
  104. ->where('store.name', $store->name));
  105. }
  106. public function test_a_user_cannot_view_someone_elses_store()
  107. {
  108. $user = User::factory()->create();
  109. $store = Store::factory()->create();
  110. $this->actingAs($user)->get(route('stores.show', $store->slug))->assertNotFound();
  111. }
  112. }