‪TYPO3CMS  ‪main
StreamFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\StreamFactoryInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 final class ‪StreamFactoryTest extends UnitTestCase
31 {
36  protected function ‪getTestDirectory(string $prefix = 'root_'): string
37  {
38  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId($prefix);
40  $this->testFilesToDelete[] = $path;
41  return $path;
42  }
43 
47  public function ‪implementsPsr17FactoryInterface(): void
48  {
49  $factory = new ‪StreamFactory();
50  self::assertInstanceOf(StreamFactoryInterface::class, $factory);
51  }
52 
57  {
58  $factory = new ‪StreamFactory();
59  $stream = $factory->createStream();
60  self::assertSame('', $stream->__toString());
61  }
62 
66  public function ‪createStreamFromEmptyString(): void
67  {
68  $factory = new ‪StreamFactory();
69  $stream = $factory->createStream('');
70  self::assertSame('', $stream->__toString());
71  }
72 
76  public function ‪createStreamFromNonEmptyString(): void
77  {
78  $factory = new ‪StreamFactory();
79  $stream = $factory->createStream('Foo');
80  self::assertSame('Foo', $stream->__toString());
81  }
82 
86  public function ‪createStreamReturnsWritableStream(): void
87  {
88  $factory = new ‪StreamFactory();
89  $stream = $factory->createStream();
90  $stream->write('Foo');
91  self::assertSame('Foo', $stream->__toString());
92  }
93 
98  {
99  $factory = new ‪StreamFactory();
100  $stream = $factory->createStream('Foo');
101  $stream->write('Bar');
102  self::assertSame('FooBar', $stream->__toString());
103  }
104 
108  public function ‪createStreamFromFile(): void
109  {
110  $fileName = $this->‪getTestDirectory() . '/' . ‪StringUtility::getUniqueId('test_');
111  file_put_contents($fileName, 'Foo');
112 
113  $factory = new ‪StreamFactory();
114  $stream = $factory->createStreamFromFile($fileName);
115  self::assertSame('Foo', $stream->__toString());
116  }
117 
121  public function ‪createStreamFromFileWithMode(): void
122  {
123  $fileName = $this->‪getTestDirectory() . '/' . ‪StringUtility::getUniqueId('test_');
124 
125  $factory = new ‪StreamFactory();
126  $stream = $factory->createStreamFromFile($fileName, 'w');
127  $stream->write('Foo');
128 
129  $contents = file_get_contents($fileName);
130  self::assertSame('Foo', $contents);
131  }
132 
137  {
138  $fileName = $this->‪getTestDirectory() . '/' . ‪StringUtility::getUniqueId('test_');
139  touch($fileName);
140 
141  $this->expectException(\InvalidArgumentException::class);
142  $this->expectExceptionCode(1566823434);
143  $factory = new ‪StreamFactory();
144  $factory->createStreamFromFile($fileName, 'z');
145  }
146 
151  {
152  $unavailableFileName = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('test_');
153  $this->expectException(\RuntimeException::class);
154  $this->expectExceptionCode(1566823435);
155  $factory = new ‪StreamFactory();
156  $factory->createStreamFromFile($unavailableFileName, 'r');
157  }
158 
162  public function ‪createStreamFromResource(): void
163  {
164  $fileName = $this->‪getTestDirectory() . '/' . ‪StringUtility::getUniqueId('test_');
165  touch($fileName);
166  file_put_contents($fileName, 'Foo');
167 
168  $resource = fopen($fileName, 'r');
169 
170  $factory = new ‪StreamFactory();
171  $stream = $factory->createStreamFromResource($resource);
172  self::assertSame('Foo', $stream->__toString());
173  }
174 
179  {
180  $this->expectException(\InvalidArgumentException::class);
181  $this->expectExceptionCode(1566853697);
182  $resource = xml_parser_create();
183 
184  $factory = new ‪StreamFactory();
185  $factory->createStreamFromResource($resource);
186  }
187 }
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromResource
‪createStreamFromResource()
Definition: StreamFactoryTest.php:162
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamReturnsWritableStream
‪createStreamReturnsWritableStream()
Definition: StreamFactoryTest.php:86
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFileWithMissingFile
‪createStreamFromFileWithMissingFile()
Definition: StreamFactoryTest.php:150
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamResourceFromInvalidResource
‪createStreamResourceFromInvalidResource()
Definition: StreamFactoryTest.php:178
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromNonEmptyString
‪createStreamFromNonEmptyString()
Definition: StreamFactoryTest.php:76
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFileWithMode
‪createStreamFromFileWithMode()
Definition: StreamFactoryTest.php:121
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromEmptyString
‪createStreamFromEmptyString()
Definition: StreamFactoryTest.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:1638
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFileWithInvalidMode
‪createStreamFromFileWithInvalidMode()
Definition: StreamFactoryTest.php:136
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamReturnsAppendableStream
‪createStreamReturnsAppendableStream()
Definition: StreamFactoryTest.php:97
‪TYPO3\CMS\Core\Http\StreamFactory
Definition: StreamFactory.php:27
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\getTestDirectory
‪getTestDirectory(string $prefix='root_')
Definition: StreamFactoryTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest
Definition: StreamFactoryTest.php:31
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\implementsPsr17FactoryInterface
‪implementsPsr17FactoryInterface()
Definition: StreamFactoryTest.php:47
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamReturnsEmptyStreamByDefault
‪createStreamReturnsEmptyStreamByDefault()
Definition: StreamFactoryTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFile
‪createStreamFromFile()
Definition: StreamFactoryTest.php:108
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57