Add an importer for motives

This commit is contained in:
2025-09-02 10:12:47 +02:00
parent 10b73e06e1
commit 25561cdf63
6 changed files with 308 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\TicketBundle\Tests\Service\Import;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Entity\StoredObjectVersion;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\TicketBundle\Repository\MotiveRepository;
use Chill\TicketBundle\Service\Import\ImportMotivesFromDirectory;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
/**
* @internal
*
* @coversNothing
*/
class ImportMotivesFromDirectoryTest extends TestCase
{
use ProphecyTrait;
public function testImportSmoke(): void
{
// 1) Prepare temporary directory with a minimal motives.yaml and a small file
$tmpBase = sys_get_temp_dir().DIRECTORY_SEPARATOR.'chill_ticket_import_'.bin2hex(random_bytes(6));
$this->assertTrue(mkdir($tmpBase, 0777, true));
$filePath = $tmpBase.DIRECTORY_SEPARATOR.'file.txt';
file_put_contents($filePath, 'hello world');
$yaml = <<<'YAML'
- label:
fr: "Test Motive"
urgent: true
supplementary_informations:
- label:
fr: "Some note"
stored_objects:
- label:
fr: "Doc title"
filename: "file.txt"
YAML;
file_put_contents($tmpBase.DIRECTORY_SEPARATOR.'motives.yaml', $yaml);
// 2) Create mocks (Prophecy) for dependencies
$emProphecy = $this->prophesize(EntityManagerInterface::class);
// persist should be called for StoredObject and Motive at least once
$emProphecy->persist(Argument::type(StoredObject::class))->shouldBeCalled();
$emProphecy->persist(Argument::that(fn($arg) =>
// Motive class is in another namespace; just check it's an object with setLabel method
\is_object($arg) && method_exists($arg, 'setLabel')))->shouldBeCalled();
$emProphecy->flush()->shouldBeCalled();
$somMock = $this->createMock(StoredObjectManagerInterface::class);
$somMock
->expects($this->once())
->method('write')
->willReturnCallback(function (StoredObject $so, string $content, ?string $contentType) {
$this->assertSame('text/plain', $contentType);
return new StoredObjectVersion($so, 1, [], [], $contentType ?? 'application/octet-stream', 'file.txt');
});
$repoProphecy = $this->prophesize(MotiveRepository::class);
$repoProphecy->findByLabel('Test Motive', 'fr')->willReturn([])->shouldBeCalled();
// 3) Run the importer
$importer = new ImportMotivesFromDirectory(
$emProphecy->reveal(),
$somMock,
$repoProphecy->reveal(),
);
$importer->import($tmpBase, 'fr');
// 4) Cleanup
@unlink($filePath);
@unlink($tmpBase.DIRECTORY_SEPARATOR.'motives.yaml');
@rmdir($tmpBase);
// If we reached here, it's a successful smoke test
$this->assertTrue(true);
}
}