CustomerTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. public static function tearDownAfterClass(): void
  13. {
  14. parent::tearDownAfterClass();
  15. Customer::query()->truncate();
  16. }
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->service = app(CustomerService::class);
  21. $this->data = [
  22. "model" => [
  23. "code" => date('Ymd').Str::random(4),
  24. "name" => date('Ymd').Str::random(5),
  25. "company_name" => date('Ymd').Str::random(6),
  26. ]
  27. ];
  28. }
  29. public function create(){
  30. $model = $this->service->create($this->data["model"]);
  31. $this->assertNotNull($model);
  32. $this->data["model"]["id"] = $model->id;
  33. $this->assertEquals($model->code,$this->data["model"]["code"]);
  34. $this->assertEquals($model->name,$this->data["model"]["name"]);
  35. $this->assertEquals($model->company_name,$this->data["model"]["company_name"]);
  36. }
  37. public function testGetSelection(){
  38. $this->create();
  39. $models = $this->service->getSelection(["id","name","company_name"]);
  40. $this->assertGreaterThanOrEqual(1,count($models));
  41. $this->assertArrayHasKey("company_name",$models[0]);
  42. }
  43. public function testPaginate(){
  44. $this->create();
  45. $models = $this->service->paginate(5);
  46. $this->assertGreaterThanOrEqual(1,count($models));
  47. $this->assertLessThanOrEqual(5,count($models));
  48. }
  49. public function testFind(){
  50. $this->create();
  51. $model = $this->service->find($this->data["model"]["id"]);
  52. $this->assertNotNull($model);
  53. }
  54. public function testUpdate()
  55. {
  56. $this->create();
  57. $value = date('YmdH').Str::random(4);
  58. $row = $this->service->update(["id"=>$this->data["model"]["id"]],[
  59. "name" => $value,
  60. ]);
  61. $this->data["model"]["name"] = $value;
  62. $this->assertEquals($row,1);
  63. }
  64. public function testDestroy()
  65. {
  66. $this->create();
  67. $row = $this->service->destroy($this->data["model"]["id"]);
  68. $this->assertEquals($row,1);
  69. $this->assertDeleted("customers", $this->data["model"]);
  70. }
  71. }