DolibarrSyncTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Tests\Service\Cron\Job;
  3. use App\Service\Cron\Job\DolibarrSync;
  4. use App\Service\Cron\UI\CronUIInterface;
  5. use App\Service\Dolibarr\DolibarrSyncService;
  6. use App\Service\Rest\Operation\UpdateOperation;
  7. use PHPUnit\Framework\MockObject\MockObject;
  8. use PHPUnit\Framework\TestCase;
  9. use Psr\Log\LoggerInterface;
  10. class DolibarrSyncTest extends TestCase
  11. {
  12. private CronUIInterface|MockObject $ui;
  13. private MockObject|LoggerInterface $logger;
  14. private DolibarrSyncService|MockObject $dolibarrSyncService;
  15. public function setUp(): void {
  16. $this->ui = $this->getMockBuilder(CronUIInterface::class)->disableOriginalConstructor()->getMock();
  17. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  18. $this->dolibarrSyncService = $this->getMockBuilder(DolibarrSyncService::class)->disableOriginalConstructor()->getMock();
  19. }
  20. private function getMockFor(string $method) {
  21. $dolibarrSync = $this->getMockBuilder(DolibarrSync::class)
  22. ->setConstructorArgs([$this->dolibarrSyncService])
  23. ->setMethodsExcept([$method, 'setUI', 'setLoggerInterface'])
  24. ->getMock();
  25. $dolibarrSync->setUI($this->ui);
  26. $dolibarrSync->setLoggerInterface($this->logger);
  27. return $dolibarrSync;
  28. }
  29. public function testPreview(): void
  30. {
  31. $dolibarrSync = $this->getMockFor('preview');
  32. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  33. $operation1->method('getLabel')->willReturn('Op 1');
  34. $operation1->method('getChangeLog')->willReturn(['foo', 'bar']);
  35. $operation2 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  36. $operation2->method('getLabel')->willReturn('Op 2');
  37. $operation2->method('getChangeLog')->willReturn([]);
  38. $operation3 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  39. $operation3->method('getLabel')->willReturn('Op 3');
  40. $operation3->method('getChangeLog')->willReturn(['hello', 'world']);
  41. $this->dolibarrSyncService
  42. ->expects(self::once())
  43. ->method('scan')
  44. ->willReturn([1 => $operation1, 2 => $operation2, 3 => $operation3]);
  45. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  46. ["1. Op 1"],
  47. [" foo"],
  48. [" bar"],
  49. ["2. Op 2"],
  50. ["3. Op 3"],
  51. [" hello"],
  52. [" world"],
  53. );
  54. $dolibarrSync->preview();
  55. }
  56. public function testExecute(): void {
  57. $dolibarrSync = $this->getMockFor('execute');
  58. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  59. $operation1->method('getStatus')->willReturn(UpdateOperation::STATUS_DONE);
  60. $operation2 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  61. $operation2->method('getStatus')->willReturn(UpdateOperation::STATUS_DONE);
  62. $operation3 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  63. $operation3->method('getStatus')->willReturn(UpdateOperation::STATUS_ERROR);
  64. $operations = [1 => $operation1, 2 => $operation2, 3 => $operation3];
  65. $this->dolibarrSyncService
  66. ->expects(self::once())
  67. ->method('scan')
  68. ->willReturn($operations);
  69. $this->dolibarrSyncService
  70. ->expects(self::once())
  71. ->method('execute')
  72. ->with($operations, self::isInstanceOf(\Closure::class))
  73. ->willReturn($operations);
  74. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  75. ["Executing..."],
  76. ["2 operations successfully executed"],
  77. ["1 errors"],
  78. );
  79. $dolibarrSync->execute();
  80. }
  81. }