CustomerTest.php 2.0 KB

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