CustomerTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Tests\Unit\Customer;
  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 = factory(Customer::class,3)->create()->toArray();
  17. }
  18. public function create(){
  19. $model = $this->service->create([
  20. "code" => date('Ymd').Str::random(4),
  21. "name" => date('Ymd').Str::random(5),
  22. "company_name" => date('Ymd').Str::random(6),
  23. ]);
  24. $this->assertNotNull($model);
  25. $this->data[] = $model;
  26. $this->assertEquals($model->code,$this->data["model"]["code"]);
  27. $this->assertEquals($model->name,$this->data["model"]["name"]);
  28. $this->assertEquals($model->company_name,$this->data["model"]["company_name"]);
  29. }
  30. public function testGetSelection(){
  31. $models = $this->service->getSelection(["id","name","company_name"]);
  32. $this->assertGreaterThanOrEqual(1,count($models));
  33. $this->assertArrayHasKey("company_name",$models[0]);
  34. }
  35. public function testPaginate(){
  36. $models = $this->service->paginate(5);
  37. $this->assertGreaterThanOrEqual(1,count($models));
  38. $this->assertLessThanOrEqual(5,count($models));
  39. }
  40. public function testFind(){
  41. $model = $this->service->find($this->data["model"]["id"]);
  42. $this->assertNotNull($model);
  43. }
  44. public function testUpdate()
  45. {
  46. $value = date('YmdH').Str::random(4);
  47. $row = $this->service->update(["id"=>$this->data[0]["id"]],[
  48. "name" => $value,
  49. ]);
  50. $this->data["model"]["name"] = $value;
  51. $this->assertEquals($row,1);
  52. }
  53. public function testDestroy()
  54. {
  55. $row = $this->service->destroy($this->data[0]["id"]);
  56. $this->assertEquals($row,1);
  57. $this->assertDeleted("customers", $this->data["model"]);
  58. }
  59. public function tearDown(): void
  60. {
  61. parent::tearDown();
  62. $ids = array_column($this->data,'id');
  63. $row = Customer::destroy($ids);
  64. $this->assertGreaterThan(0,$row);
  65. }
  66. }