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

StoreTest.php 4.3KB

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