<?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\WopiBundle\Tests\Service\Wopi;

use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Test\RandomUserTrait;
use Chill\WopiBundle\Service\Wopi\ChillDocumentLockManager;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\RequestInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Security\Core\Security;

/**
 * @internal
 *
 * @coversNothing
 */
final class ChillDocumentLockManagerTest extends KernelTestCase
{
    use ProphecyTrait;

    use RandomUserTrait;

    private MockClock $clock;

    /**
     * @var ObjectProphecy<Security>
     */
    private ObjectProphecy $security;

    private EntityManagerInterface $em;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->em = self::getContainer()->get('doctrine.orm.entity_manager');
        $this->security = $this->prophesize(Security::class);
        $this->clock = new MockClock();
    }

    public function testRelock()
    {
        $user = $this->getRandomUser($this->em);
        $this->security->getUser()->willReturn($user);
        $manager = $this->makeManager(1);

        $document = new StoredObject();
        $request = $this->prophesize(RequestInterface::class);

        $this->assertFalse($manager->hasLock($document, $request->reveal()));

        $this->assertTrue($manager->setLock($document, 'dummy', $request->reveal()));

        $this->assertEquals('dummy', $manager->getLock($document, $request->reveal()));

        $this->assertTrue($manager->setLock($document, 'bar', $request->reveal()));

        $this->assertEquals('bar', $manager->getLock($document, $request->reveal()));

        $this->assertTrue($manager->deleteLock($document, $request->reveal()));

        $this->clock->sleep(10);

        $this->assertFalse($manager->hasLock($document, $request->reveal()));
        $this->em->remove($document);
        $this->em->flush();
    }

    public function testSingleLock()
    {
        $user = $this->getRandomUser($this->em);
        $this->security->getUser()->willReturn($user);
        $manager = $this->makeManager(1);
        $document = new StoredObject();
        $this->em->persist($document);
        $this->em->flush();

        $request = $this->prophesize(RequestInterface::class);

        $this->assertFalse($manager->hasLock($document, $request->reveal()));

        $this->assertTrue($manager->setLock($document, 'dummy', $request->reveal()));

        $this->assertEquals('dummy', $manager->getLock($document, $request->reveal()));

        $this->assertTrue($manager->deleteLock($document, $request->reveal()));

        $this->clock->sleep(10);

        $this->assertFalse($manager->hasLock($document, $request->reveal()));

        $this->em->remove($document);
        $this->em->flush();
    }

    private function makeManager(int $ttlAfterDeleteSeconds = -1): ChillDocumentLockManager
    {
        return new ChillDocumentLockManager($this->security->reveal(), $this->em, $this->clock, $ttlAfterDeleteSeconds);
    }
}
