‪TYPO3CMS  10.4
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;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪StreamFactoryTest extends UnitTestCase
30 {
35  {
36  $factory = new ‪StreamFactory();
37  self::assertInstanceOf(StreamFactoryInterface::class, $factory);
38  }
39 
44  {
45  $factory = new ‪StreamFactory();
46  $stream = $factory->createStream();
47  self::assertSame('', $stream->__toString());
48  }
49 
54  {
55  $factory = new ‪StreamFactory();
56  $stream = $factory->createStream('');
57  self::assertSame('', $stream->__toString());
58  }
59 
64  {
65  $factory = new ‪StreamFactory();
66  $stream = $factory->createStream('Foo');
67  self::assertSame('Foo', $stream->__toString());
68  }
69 
74  {
75  $factory = new ‪StreamFactory();
76  $stream = $factory->createStream();
77  $stream->write('Foo');
78  self::assertSame('Foo', $stream->__toString());
79  }
80 
85  {
86  $factory = new ‪StreamFactory();
87  $stream = $factory->createStream('Foo');
88  $stream->write('Bar');
89  self::assertSame('FooBar', $stream->__toString());
90  }
91 
95  public function ‪testCreateStreamFromFile()
96  {
97  $fileName = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('test_');
98  file_put_contents($fileName, 'Foo');
99 
100  $factory = new ‪StreamFactory();
101  $stream = $factory->createStreamFromFile($fileName);
102  self::assertSame('Foo', $stream->__toString());
103  }
104 
109  {
110  $fileName = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('test_');
111 
112  $factory = new ‪StreamFactory();
113  $stream = $factory->createStreamFromFile($fileName, 'w');
114  $stream->write('Foo');
115 
116  $contents = file_get_contents($fileName);
117  self::assertSame('Foo', $contents);
118  }
119 
124  {
125  $fileName = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('test_');
126  touch($fileName);
127 
128  $this->expectException(\InvalidArgumentException::class);
129  $this->expectExceptionCode(1566823434);
130  $factory = new ‪StreamFactory();
131  $factory->createStreamFromFile($fileName, 'z');
132  }
133 
138  {
139  $unavailableFileName = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('test_');
140  $this->expectException(\RuntimeException::class);
141  $this->expectExceptionCode(1566823435);
142  $factory = new ‪StreamFactory();
143  $factory->createStreamFromFile($unavailableFileName, 'r');
144  }
145 
150  {
151  $fileName = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('test_');
152  touch($fileName);
153  file_put_contents($fileName, 'Foo');
154 
155  $resource = fopen($fileName, 'r');
156 
157  $factory = new ‪StreamFactory();
158  $stream = $factory->createStreamFromResource($resource);
159  self::assertSame('Foo', $stream->__toString());
160  }
161 
166  {
167  $this->expectException(\InvalidArgumentException::class);
168  $this->expectExceptionCode(1566853697);
169  $resource = xml_parser_create();
170 
171  $factory = new ‪StreamFactory();
172  $factory->createStreamFromResource($resource);
173  }
174 }
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamFromEmptyString
‪testCreateStreamFromEmptyString()
Definition: StreamFactoryTest.php:53
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamFromNonEmptyString
‪testCreateStreamFromNonEmptyString()
Definition: StreamFactoryTest.php:63
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamReturnsAppendableStream
‪testCreateStreamReturnsAppendableStream()
Definition: StreamFactoryTest.php:84
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamFromResource
‪testCreateStreamFromResource()
Definition: StreamFactoryTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamFromFileWithMode
‪testCreateStreamFromFileWithMode()
Definition: StreamFactoryTest.php:108
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamReturnsEmptyStreamByDefault
‪testCreateStreamReturnsEmptyStreamByDefault()
Definition: StreamFactoryTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamResourceFromInvalidResource
‪testCreateStreamResourceFromInvalidResource()
Definition: StreamFactoryTest.php:165
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamFromFile
‪testCreateStreamFromFile()
Definition: StreamFactoryTest.php:95
‪TYPO3\CMS\Core\Http\StreamFactory
Definition: StreamFactory.php:27
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamReturnsWritableStream
‪testCreateStreamReturnsWritableStream()
Definition: StreamFactoryTest.php:73
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest
Definition: StreamFactoryTest.php:30
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\implementsPsr17FactoryInterface
‪implementsPsr17FactoryInterface()
Definition: StreamFactoryTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamFromFileWithMissingFile
‪testCreateStreamFromFileWithMissingFile()
Definition: StreamFactoryTest.php:137
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\testCreateStreamFromFileWithInvalidMode
‪testCreateStreamFromFileWithInvalidMode()
Definition: StreamFactoryTest.php:123
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192