AttributeFamilyTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. use Webkul\Attribute\Models\AttributeFamily as AttributeFamilyModel;
  3. use function Pest\Laravel\deleteJson;
  4. use function Pest\Laravel\get;
  5. use function Pest\Laravel\getJson;
  6. use function Pest\Laravel\postJson;
  7. use function Pest\Laravel\putJson;
  8. it('should return attribute family listing page', function () {
  9. // Act and Assert.
  10. $this->loginAsAdmin();
  11. get(route('admin.catalog.families.index'))
  12. ->assertOk()
  13. ->assertSeeText(trans('admin::app.catalog.families.index.title'))
  14. ->assertSeeText(trans('admin::app.catalog.families.index.add'));
  15. });
  16. it('should return listing items of attribute family', function () {
  17. // Arrange.
  18. $attributeFamily = AttributeFamilyModel::factory()->create();
  19. // Act and Assert.
  20. $this->loginAsAdmin();
  21. getJson(route('admin.catalog.families.index'), [
  22. 'X-Requested-With' => 'XMLHttpRequest',
  23. ])
  24. ->assertOk()
  25. ->assertJsonPath('records.0.id', $attributeFamily->id)
  26. ->assertJsonPath('records.0.code', $attributeFamily->code)
  27. ->assertJsonPath('meta.total', 2);
  28. });
  29. it('should return attribute family create page', function () {
  30. // Act and Assert.
  31. $this->loginAsAdmin();
  32. get(route('admin.catalog.families.create'))
  33. ->assertOk()
  34. ->assertSeeText(trans('admin::app.catalog.families.create.title'))
  35. ->assertSeeText(trans('admin::app.catalog.families.create.back-btn'));
  36. });
  37. it('should store newly created attribute family', function () {
  38. // Act and Assert.
  39. $this->loginAsAdmin();
  40. postJson(route('admin.catalog.families.store'), [
  41. 'code' => $code = fake()->numerify('code########'),
  42. 'name' => $name = fake()->name(),
  43. 'attribute_groups' => [
  44. [
  45. 'code' => $code,
  46. 'name' => $name,
  47. 'column' => 1,
  48. ],
  49. ],
  50. ])
  51. ->assertRedirectToRoute('admin.catalog.families.index')
  52. ->isRedirection();
  53. $this->assertModelWise([
  54. AttributeFamilyModel::class => [
  55. [
  56. 'code' => $code,
  57. 'name' => $name,
  58. ],
  59. ],
  60. ]);
  61. });
  62. it('should fail the validation with errors when certain inputs are not provided when store in attribute family', function () {
  63. // Act and Assert.
  64. $this->loginAsAdmin();
  65. postJson(route('admin.catalog.families.store'))
  66. ->assertJsonValidationErrorFor('name')
  67. ->assertJsonValidationErrorFor('code')
  68. ->assertUnprocessable();
  69. });
  70. it('should return edit page of attribute families', function () {
  71. // Arrange.
  72. $attributeFamily = AttributeFamilyModel::factory()->create();
  73. // Act and Assert.
  74. $this->loginAsAdmin();
  75. get(route('admin.catalog.families.edit', $attributeFamily->id))
  76. ->assertOk()
  77. ->assertSeeText(trans('admin::app.catalog.families.edit.title'))
  78. ->assertSeeText(trans('admin::app.catalog.families.edit.back-btn'));
  79. });
  80. it('should fail the validation with errors when certain inputs are not provided when update in attribute family', function () {
  81. // Arrange.
  82. $attributeFamily = AttributeFamilyModel::factory()->create();
  83. // Act and Assert.
  84. $this->loginAsAdmin();
  85. putJson(route('admin.catalog.families.update', $attributeFamily->id))
  86. ->assertJsonValidationErrorFor('name')
  87. ->assertJsonValidationErrorFor('code')
  88. ->assertUnprocessable();
  89. });
  90. it('should update the existing attribute families', function () {
  91. // Arrange.
  92. $attributeFamily = AttributeFamilyModel::factory()->create();
  93. // Act and Assert.
  94. $this->loginAsAdmin();
  95. putJson(route('admin.catalog.families.update', $attributeFamily->id), $data = [
  96. 'code' => fake()->numerify('code#######'),
  97. 'name' => $attributeFamily->name,
  98. ])
  99. ->assertRedirectToRoute('admin.catalog.families.index')
  100. ->isRedirection();
  101. $this->assertModelWise([
  102. AttributeFamilyModel::class => [
  103. $data,
  104. ],
  105. ]);
  106. });
  107. it('should delete the existing attribute family', function () {
  108. // Arrange.
  109. $attributeFamily = AttributeFamilyModel::factory()->create();
  110. // Act and Assert.
  111. $this->loginAsAdmin();
  112. deleteJson(route('admin.catalog.families.delete', $attributeFamily->id))
  113. ->assertOk()
  114. ->assertSeeText(trans('admin::app.catalog.families.delete-success'));
  115. $this->assertDatabaseMissing('attribute_families', [
  116. 'id' => $attributeFamily->id,
  117. ]);
  118. });
  119. it('should not be able to delete the attribute family if the attribute family is the only one present', function () {
  120. // Act and Assert.
  121. $this->loginAsAdmin();
  122. deleteJson(route('admin.catalog.families.delete', $attributeFamilyId = 1))
  123. ->assertBadRequest()
  124. ->assertSeeText(trans('admin::app.catalog.families.last-delete-error'));
  125. $this->assertModelWise([
  126. AttributeFamilyModel::class => [
  127. [
  128. 'id' => $attributeFamilyId,
  129. ],
  130. ],
  131. ]);
  132. });