ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ResourceBuilderTest.php
Go to the documentation of this file.
1<?php
2
4
19use PHPUnit\Framework\TestCase;
20use Psr\Http\Message\UploadedFileInterface;
21
23{
24 private $id = 'dummy';
25
26
32 public function __construct(string $id)
33 {
34 $this->id = $id;
35 }
36
37
42 {
43 return new ResourceIdentification($this->id);
44 }
45}
46
52class ResourceBuilderTest extends TestCase
53{
54
58 private $revision;
62 private $information;
87
88
92 protected function setUp() : void
93 {
94 parent::setUp();
95
96 $this->storage_handler = $this->createMock(StorageHandler::class);
97 $this->revision_repository = $this->createMock(RevisionRepository::class);
98 $this->resource_repository = $this->createMock(ResourceRepository::class);
99 $this->information_repository = $this->createMock(InformationRepository::class);
100 $this->resource_builder = new ResourceBuilder(
101 $this->storage_handler,
102 $this->revision_repository,
103 $this->resource_repository,
104 $this->information_repository
105 );
106 $this->information = $this->createMock(Information::class);
107 $this->revision = $this->createMock(Revision::class);
108 }
109
110
111 public function testNewResource() : void
112 {
113 $file_id = 'my_file_id';
114 $file_name = 'testfile.txt';
115 $file_mime_type = 'application/base64';
116 $file_size = 256;
117
118 $r = new DummyIDGenerator($file_id);
119 $identification = $r->getUniqueResourceIdentification();
120 $result = $this->getUploadResult($file_name, $file_mime_type, $file_size);
121
122 $this->storage_handler->expects($this->once())
123 ->method('getIdentificationGenerator')
124 ->willReturn($r);
125
126 $this->resource_repository->expects($this->once())
127 ->method('blank')
128 ->with($identification)
129 ->willReturn(new StorableFileResource($identification));
130
131 $this->revision_repository->expects($this->once())
132 ->method('blank')
133 ->willReturn(new UploadedFileRevision($identification, $result));
134
135 $resource = $this->resource_builder->new($result);
136 $revision = $resource->getCurrentRevision();
137
138 $this->assertEquals($identification->serialize(), $resource->getIdentification()->serialize());
139 $this->assertEquals($file_id, $resource->getIdentification()->serialize());
140 $this->assertEquals($file_name, $revision->getInformation()->getTitle());
141 $this->assertEquals($file_mime_type, $revision->getInformation()->getMimeType());
142 $this->assertEquals($file_size, $revision->getInformation()->getSize());
143
144 // Store it
145 $this->resource_repository->expects($this->once())->method('store')->with($resource);
146 $this->storage_handler->expects($this->once())->method('storeUpload')->with($revision);
147 $this->revision_repository->expects($this->once())->method('store')->with($revision);
148 $this->information_repository->expects($this->once())->method('store')->with($revision->getInformation(), $revision);
149 $this->resource_builder->store($resource);
150 }
151
152
156 private function getUploadResult(string $file_name, string $mime_type, int $size) : UploadResult
157 {
158 return new UploadResult(
159 $file_name,
160 $size,
161 $mime_type,
163 new ProcessingStatus(ProcessingStatus::OK, 'No processors were registered.'),
164 'dummy/path'
165 );
166 }
167}
$result
$size
Definition: RandomTest.php:84
An exception for terminatinating execution or to throw for unit testing.
__construct(string $id)
DummyIDGenerator constructor.
getUploadResult(string $file_name, string $mime_type, int $size)