AttachmentNormalizerTest.php in Replication 8
File
tests/src/Kernel/Normalizer/AttachmentNormalizerTest.php
View source
<?php
namespace Drupal\Tests\replication\Kernel\Normalizer;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\file\Entity\File;
class AttachmentNormalizerTest extends NormalizerTestBase {
protected $serializer;
protected $fileContents;
protected $fileHandle;
protected $entityClass = 'Drupal\\file\\Entity\\File';
protected $fileEntity;
protected function setUp() {
parent::setUp();
$this->fileContents = $this
->randomString();
$this->fileHandle = fopen('temporary://' . $this
->randomMachineName(), 'w+b');
fwrite($this->fileHandle, $this->fileContents);
rewind($this->fileHandle);
$meta = stream_get_meta_data($this->fileHandle);
$this->fileEntity = File::create([
'uri' => $meta['uri'],
]);
}
public function testNormalizer() {
$normalized = $this->serializer
->normalize($this->fileEntity);
$this
->assertTrue(!is_resource($normalized), 'File entity was normalized to a file entity.');
$normalized = $this->serializer
->normalize($this->fileEntity, 'json');
$this
->assertTrue(!is_resource($normalized), 'File entity was normalized to a file entity.');
$normalized = $this->serializer
->normalize($this->fileEntity, 'stream');
$this
->assertTrue(is_resource($normalized), 'File entity was normalized to a file resource.');
$normalized = $this->serializer
->normalize($this->fileEntity, 'base64_stream');
$this
->assertTrue(is_resource($normalized), 'File entity was normalized to a file resource.');
$serialized = $this->serializer
->serialize($this->fileEntity, 'stream');
$this
->assertEquals($serialized, $this->fileContents, 'File entity was serialized to file contents.');
$denormalized = $this->serializer
->denormalize($this->fileHandle, $this->entityClass, 'stream');
$this
->assertTrue($denormalized instanceof $this->entityClass, new FormattableMarkup('Denormalized entity is an instance of @class', [
'@class' => $this->entityClass,
]));
$this
->assertSame($this->fileEntity
->getEntityTypeId(), $denormalized
->getEntityTypeId(), 'Expected entity type found.');
}
}