CustomerTest.php 2.0 KB

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