CustomerTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Tests\Unit\Customer\CustomerService;
  3. use App\Customer;
  4. use App\Services\CustomerService;
  5. use Illuminate\Support\Str;
  6. use Tests\TestCase;
  7. class CustomerTest extends TestCase
  8. {
  9. /** @var CustomerService */
  10. public $service;
  11. public $data;
  12. protected function setUp(): void
  13. {
  14. parent::setUp();
  15. $this->service = app(CustomerService::class);
  16. $this->data = [
  17. "models"=>factory(Customer::class,3)->create()->toArray()
  18. ];
  19. }
  20. /**
  21. * @group customer
  22. */
  23. public function testCreate(){
  24. $model = $this->service->create([
  25. "code" => date('Ymd').Str::random(4),
  26. "name" => date('Ymd').Str::random(5),
  27. "company_name" => date('Ymd').Str::random(6),
  28. ]);
  29. $this->assertNotNull($model);
  30. $this->data["models"][] = $model->toArray();
  31. }
  32. /**
  33. * @group customer
  34. */
  35. public function testGetSelection(){
  36. $models = $this->service->getSelection(["id","name","company_name"]);
  37. $this->assertGreaterThanOrEqual(1,count($models));
  38. $this->assertArrayHasKey("company_name",$models[0]);
  39. }
  40. /**
  41. * @group customer
  42. */
  43. public function testPaginate(){
  44. $models = $this->service->paginate(["paginate"=>5]);
  45. $this->assertGreaterThanOrEqual(1,count($models));
  46. $this->assertLessThanOrEqual(5,count($models));
  47. }
  48. /**
  49. * @group customer
  50. */
  51. public function testFind(){
  52. $model = $this->service->find($this->data["models"][0]["id"]);
  53. $this->assertNotNull($model);
  54. }
  55. /**
  56. * @group customer
  57. */
  58. public function testUpdate()
  59. {
  60. $value = date('YmdH').Str::random(4);
  61. $row = $this->service->update(["id"=>$this->data["models"][0]["id"]],[
  62. "name" => $value,
  63. ]);
  64. $this->data["models"][0]["name"] = $value;
  65. $this->assertEquals($row,1);
  66. }
  67. /**
  68. * @group customer
  69. */
  70. public function testDestroy()
  71. {
  72. $row = $this->service->destroy($this->data["models"][0]["id"]);
  73. $this->assertEquals($row,1);
  74. $this->assertDeleted("customers", $this->data["models"][0]);
  75. }
  76. public function tearDown(): void
  77. {
  78. Customer::destroy(array_column($this->data["models"],'id'));
  79. parent::tearDown();
  80. }
  81. }