34ee52a4d0ac5888d2bb991707c447814842fdcf
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ProcessedFileTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Core\Resource\ProcessedFile;
20 use TYPO3\CMS\Core\Resource\ResourceStorage;
21
22 /**
23 * Testcase for the ProcessedFile class of the TYPO3 FAL
24 */
25 class ProcessedFileTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
27 /**
28 * Subject is not notice free, disable E_NOTICES
29 */
30 protected static $suppressNotices = true;
31
32 /**
33 * @var \PHPUnit_Framework_MockObject_MockObject|Folder
34 */
35 protected $folderMock;
36
37 /**
38 * @var \PHPUnit_Framework_MockObject_MockObject|ResourceStorage
39 */
40 protected $storageMock;
41
42 /**
43 * @var array
44 */
45 protected $databaseRow = [];
46
47 /**
48 * @throws \PHPUnit\Framework\Exception
49 */
50 protected function setUp()
51 {
52 $this->storageMock = $this->createMock(ResourceStorage::class);
53 $this->storageMock->expects($this->any())->method('getUid')->will($this->returnValue(5));
54
55 $this->folderMock = $this->createMock(Folder::class);
56 $this->folderMock->expects($this->any())->method('getStorage')->willReturn($this->storageMock);
57
58 $this->storageMock->expects($this->any())->method('getProcessingFolder')->willReturn($this->folderMock);
59
60 $this->databaseRow = [
61 'uid' => '1234567',
62 'identifier' => 'dummy.txt',
63 'name' => $this->getUniqueId('dummy_'),
64 'storage' => $this->storageMock->getUid(),
65 ];
66 }
67
68 /**
69 * @param array $dbRow
70 * @param ResourceStorage $storageMock
71 * @return File
72 */
73 protected function getFileFixture($dbRow = null, $storageMock = null)
74 {
75 return new File($dbRow ?: $this->databaseRow, $storageMock ?: $this->storageMock);
76 }
77
78 /**
79 * @param array $dbRow
80 * @param File $originalFile
81 * @return ProcessedFile
82 */
83 protected function getProcessedFileFixture($dbRow = null, $originalFile = null)
84 {
85 if ($originalFile === null) {
86 $originalFile = $this->getFileFixture();
87 }
88 return new ProcessedFile($originalFile, 'dummy', [], $dbRow ?: $this->databaseRow);
89 }
90
91 /**
92 * @test
93 */
94 public function propertiesOfProcessedFileAreSetFromDatabaseRow()
95 {
96 $processedFileObject = $this->getProcessedFileFixture();
97 $this->assertSame($this->databaseRow, $processedFileObject->getProperties());
98 }
99
100 /**
101 * @test
102 */
103 public function deletingProcessedFileRemovesFile()
104 {
105 $this->storageMock->expects($this->once())->method('deleteFile');
106 $processedDatabaseRow = $this->databaseRow;
107 $processedDatabaseRow['identifier'] = 'processed_dummy.txt';
108 $processedFile = $this->getProcessedFileFixture($processedDatabaseRow);
109 $processedFile->delete(true);
110 }
111
112 /**
113 * @test
114 */
115 public function deletingProcessedFileThatUsesOriginalFileDoesNotRemoveFile()
116 {
117 $this->storageMock->expects($this->never())->method('deleteFile');
118 $processedDatabaseRow = $this->databaseRow;
119 $processedDatabaseRow['identifier'] = null;
120 $processedFile = $this->getProcessedFileFixture($processedDatabaseRow);
121 $processedFile->delete(true);
122 }
123 }