106 lines
3.4 KiB
PHP
106 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service\Upload;
|
|
|
|
use App\Entity\Upload;
|
|
use App\Service\Pdf\Sanitizer;
|
|
use App\Service\Upload\UploadHandler;
|
|
use Flagception\Manager\FeatureManagerInterface;
|
|
use Oneup\UploaderBundle\Uploader\Orphanage\OrphanageManager;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
/**
|
|
* The stand-in document is gated by the upload_replacement feature and by nothing else - not by
|
|
* the environment, not by whether a path happens to be configured. That, and the fact that a
|
|
* missing stand-in is an error rather than a silent fall back to the genuine file, is what is
|
|
* pinned here.
|
|
*/
|
|
class UploadHandlerTest extends TestCase
|
|
{
|
|
private const PROJECT_DIR = '/srv/app';
|
|
private const REPLACEMENT_FILE = 'assets/pdf/chicken.pdf';
|
|
|
|
private FeatureManagerInterface&MockObject $featureManager;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->featureManager = $this->createMock(FeatureManagerInterface::class);
|
|
}
|
|
|
|
public function testTheRealDocumentIsServedWhileTheFeatureIsOff(): void
|
|
{
|
|
$this->featureManager->method('isActive')->with('upload_replacement')->willReturn(false);
|
|
|
|
$this->assertSame(
|
|
'/srv/app/uploads/invoice/i/invoice-1.pdf',
|
|
$this->handler()->getResolvedUploadFilepath($this->upload())
|
|
);
|
|
}
|
|
|
|
public function testTheStandInIsServedWhileTheFeatureIsOn(): void
|
|
{
|
|
$this->featureManager->method('isActive')->with('upload_replacement')->willReturn(true);
|
|
|
|
// the real file of the repository, so the path resolves to something that exists
|
|
$handler = $this->handler(\dirname(__DIR__, 3), self::REPLACEMENT_FILE);
|
|
|
|
$this->assertSame(
|
|
\dirname(__DIR__, 3).'/'.self::REPLACEMENT_FILE,
|
|
$handler->getResolvedUploadFilepath($this->upload())
|
|
);
|
|
}
|
|
|
|
public function testAMissingStandInFailsInsteadOfServingTheRealDocument(): void
|
|
{
|
|
$this->featureManager->method('isActive')->with('upload_replacement')->willReturn(true);
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
$this->expectExceptionMessage('/srv/app/assets/pdf/nope.pdf');
|
|
|
|
$this->handler(self::PROJECT_DIR, 'assets/pdf/nope.pdf')
|
|
->getResolvedUploadFilepath($this->upload())
|
|
;
|
|
}
|
|
|
|
/**
|
|
* The real location is never substituted: it is what writes target.
|
|
*/
|
|
public function testTheUploadFilepathIgnoresTheFeature(): void
|
|
{
|
|
$this->featureManager->method('isActive')->with('upload_replacement')->willReturn(true);
|
|
|
|
$this->assertSame(
|
|
'/srv/app/uploads/invoice/i/invoice-1.pdf',
|
|
$this->handler()->getUploadFilepath($this->upload())
|
|
);
|
|
}
|
|
|
|
private function handler(
|
|
string $projectDir = self::PROJECT_DIR,
|
|
string $replacementFile = self::REPLACEMENT_FILE,
|
|
): UploadHandler {
|
|
return new UploadHandler(
|
|
$this->createMock(RequestStack::class),
|
|
$this->createMock(OrphanageManager::class),
|
|
$this->createMock(Sanitizer::class),
|
|
new NullLogger(),
|
|
$this->featureManager,
|
|
$projectDir,
|
|
$replacementFile,
|
|
);
|
|
}
|
|
|
|
private function upload(): Upload
|
|
{
|
|
return (new Upload())
|
|
->setType(Upload::TYPE_INVOICE)
|
|
->setFilename('invoice-1.pdf')
|
|
;
|
|
}
|
|
}
|