DolibarrSyncTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Tests\Unit\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. {
  17. $this->ui = $this->getMockBuilder(CronUIInterface::class)->disableOriginalConstructor()->getMock();
  18. $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  19. $this->dolibarrSyncService = $this->getMockBuilder(DolibarrSyncService::class)->disableOriginalConstructor()->getMock();
  20. }
  21. private function getMockFor(string $method)
  22. {
  23. $dolibarrSync = $this->getMockBuilder(DolibarrSync::class)
  24. ->setConstructorArgs([$this->dolibarrSyncService])
  25. ->setMethodsExcept([$method, 'setUI', 'setLoggerInterface'])
  26. ->getMock();
  27. $dolibarrSync->setUI($this->ui);
  28. $dolibarrSync->setLoggerInterface($this->logger);
  29. return $dolibarrSync;
  30. }
  31. public function testPreview(): void
  32. {
  33. $dolibarrSync = $this->getMockFor('preview');
  34. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  35. $operation1->method('getLabel')->willReturn('Op 1');
  36. $operation1->method('getChangeLog')->willReturn(['foo', 'bar']);
  37. $operation2 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  38. $operation2->method('getLabel')->willReturn('Op 2');
  39. $operation2->method('getChangeLog')->willReturn([]);
  40. $operation3 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  41. $operation3->method('getLabel')->willReturn('Op 3');
  42. $operation3->method('getChangeLog')->willReturn(['hello', 'world']);
  43. $this->dolibarrSyncService
  44. ->expects(self::once())
  45. ->method('scan')
  46. ->willReturn([1 => $operation1, 2 => $operation2, 3 => $operation3]);
  47. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  48. ['1. Op 1'],
  49. [' foo'],
  50. [' bar'],
  51. ['2. Op 2'],
  52. ['3. Op 3'],
  53. [' hello'],
  54. [' world'],
  55. );
  56. $dolibarrSync->preview();
  57. }
  58. public function testExecute(): void
  59. {
  60. $dolibarrSync = $this->getMockFor('execute');
  61. $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  62. $operation1->method('getStatus')->willReturn(UpdateOperation::STATUS_DONE);
  63. $operation2 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  64. $operation2->method('getStatus')->willReturn(UpdateOperation::STATUS_DONE);
  65. $operation3 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
  66. $operation3->method('getStatus')->willReturn(UpdateOperation::STATUS_ERROR);
  67. $operations = [1 => $operation1, 2 => $operation2, 3 => $operation3];
  68. $this->dolibarrSyncService
  69. ->expects(self::once())
  70. ->method('scan')
  71. ->willReturn($operations);
  72. $this->dolibarrSyncService
  73. ->expects(self::once())
  74. ->method('execute')
  75. ->with($operations, self::isInstanceOf(\Closure::class))
  76. ->willReturn($operations);
  77. $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
  78. ['Executing...'],
  79. ['2 operations successfully executed'],
  80. ['1 errors'],
  81. );
  82. $dolibarrSync->execute();
  83. }
  84. }