DigiComp.AttachmentViewUtility/Tests/Functional/AttachmentViewTraitTest.php

83 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2023-10-17 18:49:19 +02:00
<?php
declare(strict_types=1);
namespace DigiComp\AttachmentViewUtility\Tests\Functional;
use DigiComp\AttachmentViewUtility\Tests\Functional\Fixtures\SimpleAttachmentTemplateView;
use Neos\Eel\Context;
use Neos\Flow\SignalSlot\Dispatcher;
use Neos\Flow\Tests\FunctionalTestCase;
use Psr\Http\Message\ResponseInterface;
class AttachmentViewTraitTest extends FunctionalTestCase
{
/**
* @test
*/
public function itCreatesAttachmentResponses(): void
{
$view = new SimpleAttachmentTemplateView([
'templateSource' => 'TollesTemplate! {testVar}',
'filenameEelExpression' => 'testVar + ".txt"',
'attachmentCharset' => 'iso-8859-1'
]);
$view->assign('testVar', '£ and € rates');
$result = $view->render();
static::assertInstanceOf(ResponseInterface::class, $result);
static::assertEquals(
'attachment; filename="£ and ? rates.txt"; filename*=UTF-8\'\'%C2%A3%20and%20%E2%82%AC%20rates.txt',
$result->getHeaderLine('Content-Disposition')
);
static::assertEquals('text/plain; charset=iso-8859-1', $result->getHeaderLine('Content-Type'));
static::assertEquals('TollesTemplate! £ and € rates', (string)$result->getBody());
}
/**
* @test
*/
public function itIsPossibleToSuppressCharset(): void
{
$view = new SimpleAttachmentTemplateView([
'templateSource' => 'TollesTemplate! {testVar}',
'filenameEelExpression' => 'testVar + ".txt"',
'attachmentCharset' => false
]);
$view->assign('testVar', 'WORLD');
$result = $view->render();
static::assertEquals('text/plain', $result->getHeaderLine('Content-Type'));
}
/**
* @test
*/
public function itIsPossibleToExtendTheContextBySignalSlot(): void
{
$view = new SimpleAttachmentTemplateView([
'templateSource' => 'TollesTemplate! {testVar}',
'filenameEelExpression' => 'greet(testVar) + ".txt"',
'attachmentCharset' => false
]);
$view->assign('testVar', 'WORLD');
$dispatcher = $this->objectManager->get(Dispatcher::class);
$dispatcher->connect(
SimpleAttachmentTemplateView::class,
'filenameEelExpressionContext',
function (Context $eelContext) {
$eelContext->push(
function ($name) {
return 'Hello ' . $name;
},
'greet'
);
}
);
$result = $view->render();
static::assertEquals(
'attachment; filename="Hello WORLD.txt"',
$result->getHeaderLine('Content-Disposition')
);
}
}