RecordByYearTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Tests\Services\NewOrderCountingRecordService;
  3. use App\Services\NewOrderCountingRecordService;
  4. use Carbon\Carbon;
  5. use Tests\TestCase;
  6. use App\OrderCountingRecord;
  7. use App\Traits\TestMockSubServices;
  8. class RecordByYearTest extends TestCase
  9. {
  10. use TestMockSubServices;
  11. /** @var NewOrderCountingRecordService $service */
  12. public $service;
  13. private $data;
  14. private $amount = 1;
  15. function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->service = app('NewOrderCountingRecordService');
  19. // $this->data['newOrderCountingRecords']
  20. // = factory(OrderCountingRecord::class, $this->amount)
  21. // ->create();
  22. }
  23. public function testReturned()
  24. {
  25. $this->assertTrue(true);
  26. }
  27. function tearDown(): void
  28. {
  29. OrderCountingRecord::query()
  30. ->whereIn('id', data_get($this->data['newOrderCountingRecords'], '*.id') ?? [])
  31. ->delete();
  32. parent::tearDown();
  33. }
  34. /**
  35. * @test
  36. */
  37. public function get_test()
  38. {
  39. $startYear = Carbon::parse(now()->subYear())->year;
  40. if (is_null(null)) {
  41. $end = now()->subYear()->toDateString();
  42. }
  43. $endYear = Carbon::parse($end)->year;
  44. $aa = OrderCountingRecord::query()
  45. ->whereBetween('year', [$startYear, $endYear])
  46. ->where('counting_unit', '月')
  47. ->sum('amount');
  48. $this->data['newOrderCountingRecords']
  49. = factory(OrderCountingRecord::class, $this->amount)
  50. ->create([
  51. 'date_target' => now()->subYear()->startOfMonth()->toDateString(),
  52. 'counting_unit' => '月',
  53. 'amount' => 10,
  54. 'year' => now()->subYear()->year,
  55. ]);
  56. $this->service->recordByYear('2020-01-01');
  57. $bb = OrderCountingRecord::query()
  58. ->whereBetween('year', [$startYear, $endYear])
  59. ->where('counting_unit', '月')
  60. ->sum('amount');
  61. $this->assertEquals($aa+10, $bb);
  62. }
  63. }