. */ declare(strict_types=1); namespace SP\Tests\Infrastructure\File; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\MockObject; use SP\Domain\File\Ports\FileHandlerInterface; use SP\Infrastructure\File\FileException; use SP\Infrastructure\File\YamlFileStorage; use SP\Tests\UnitaryTestCase; /** * Class YamlFileStorageTest */ #[Group('unitary')] class YamlFileStorageTest extends UnitaryTestCase { private const YAML = 'list:' . PHP_EOL . ' -' . PHP_EOL . ' id: 1' . PHP_EOL . ' name: a_name' . PHP_EOL; private FileHandlerInterface|MockObject $fileHandler; private YamlFileStorage $yamlFileStorage; /** * @throws FileException */ public function testLoad() { $file = TMP_PATH . DIRECTORY_SEPARATOR . 'test.yaml'; $this->fileHandler ->method('getFile') ->willReturn($file); file_put_contents($file, self::YAML); $out = $this->yamlFileStorage->load(); $expected = ['list' => [['id' => 1, 'name' => 'a_name']]]; $this->assertEquals($expected, $out); } /** * @throws FileException */ public function testSave() { $this->fileHandler ->expects($this->once()) ->method('save') ->with(self::YAML); $this->yamlFileStorage->save(['list' => [(object)['id' => 1, 'name' => 'a_name']]]); } /** * @throws FileException */ public function testGetFileTime() { $time = self::$faker->unixTime(); $this->fileHandler->expects($this->once()) ->method('getFileTime') ->willReturn($time); $this->assertEquals($time, $this->yamlFileStorage->getFileTime()); } protected function setUp(): void { parent::setUp(); $this->fileHandler = $this->createMock(FileHandlerInterface::class); $this->yamlFileStorage = new YamlFileStorage($this->fileHandler); } }