‪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 PHPUnit\Framework\Attributes\Test;
21 use Psr\Http\Message\StreamFactoryInterface;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 final class ‪StreamFactoryTest extends UnitTestCase
32 {
37  protected function ‪getTestDirectory(string $prefix = 'root_'): string
38  {
39  $path = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId($prefix);
41  $this->testFilesToDelete[] = $path;
42  return $path;
43  }
44 
45  #[Test]
46  public function ‪implementsPsr17FactoryInterface(): void
47  {
48  $factory = new ‪StreamFactory();
49  self::assertInstanceOf(StreamFactoryInterface::class, $factory);
50  }
51 
52  #[Test]
54  {
55  $factory = new ‪StreamFactory();
56  $stream = $factory->createStream();
57  self::assertSame('', $stream->__toString());
58  }
59 
60  #[Test]
61  public function ‪createStreamFromEmptyString(): void
62  {
63  $factory = new ‪StreamFactory();
64  $stream = $factory->createStream('');
65  self::assertSame('', $stream->__toString());
66  }
67 
68  #[Test]
69  public function ‪createStreamFromNonEmptyString(): void
70  {
71  $factory = new ‪StreamFactory();
72  $stream = $factory->createStream('Foo');
73  self::assertSame('Foo', $stream->__toString());
74  }
75 
76  #[Test]
77  public function ‪createStreamReturnsWritableStream(): void
78  {
79  $factory = new ‪StreamFactory();
80  $stream = $factory->createStream();
81  $stream->write('Foo');
82  self::assertSame('Foo', $stream->__toString());
83  }
84 
85  #[Test]
87  {
88  $factory = new ‪StreamFactory();
89  $stream = $factory->createStream('Foo');
90  $stream->write('Bar');
91  self::assertSame('FooBar', $stream->__toString());
92  }
93 
94  #[Test]
95  public function ‪createStreamFromFile(): void
96  {
97  $fileName = $this->‪getTestDirectory() . '/' . ‪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 
105  #[Test]
106  public function ‪createStreamFromFileWithMode(): void
107  {
108  $fileName = $this->‪getTestDirectory() . '/' . ‪StringUtility::getUniqueId('test_');
109 
110  $factory = new ‪StreamFactory();
111  $stream = $factory->createStreamFromFile($fileName, 'w');
112  $stream->write('Foo');
113 
114  $contents = file_get_contents($fileName);
115  self::assertSame('Foo', $contents);
116  }
117 
118  #[Test]
120  {
121  $fileName = $this->‪getTestDirectory() . '/' . ‪StringUtility::getUniqueId('test_');
122  touch($fileName);
123 
124  $this->expectException(\InvalidArgumentException::class);
125  $this->expectExceptionCode(1566823434);
126  $factory = new ‪StreamFactory();
127  $factory->createStreamFromFile($fileName, 'z');
128  }
129 
130  #[Test]
132  {
133  $unavailableFileName = ‪Environment::getVarPath() . '/tests/' . ‪StringUtility::getUniqueId('test_');
134  $this->expectException(\RuntimeException::class);
135  $this->expectExceptionCode(1566823435);
136  $factory = new ‪StreamFactory();
137  $factory->createStreamFromFile($unavailableFileName, 'r');
138  }
139 
140  #[Test]
141  public function ‪createStreamFromResource(): void
142  {
143  $fileName = $this->‪getTestDirectory() . '/' . ‪StringUtility::getUniqueId('test_');
144  touch($fileName);
145  file_put_contents($fileName, 'Foo');
146 
147  $resource = fopen($fileName, 'r');
148 
149  $factory = new ‪StreamFactory();
150  $stream = $factory->createStreamFromResource($resource);
151  self::assertSame('Foo', $stream->__toString());
152  }
153 
154  #[Test]
156  {
157  $this->expectException(\InvalidArgumentException::class);
158  $this->expectExceptionCode(1566853697);
159  $resource = xml_parser_create();
160 
161  $factory = new ‪StreamFactory();
162  $factory->createStreamFromResource($resource);
163  }
164 }
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromResource
‪createStreamFromResource()
Definition: StreamFactoryTest.php:141
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamReturnsWritableStream
‪createStreamReturnsWritableStream()
Definition: StreamFactoryTest.php:77
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFileWithMissingFile
‪createStreamFromFileWithMissingFile()
Definition: StreamFactoryTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamResourceFromInvalidResource
‪createStreamResourceFromInvalidResource()
Definition: StreamFactoryTest.php:155
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromNonEmptyString
‪createStreamFromNonEmptyString()
Definition: StreamFactoryTest.php:69
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep(string $directory)
Definition: GeneralUtility.php:1654
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFileWithMode
‪createStreamFromFileWithMode()
Definition: StreamFactoryTest.php:106
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromEmptyString
‪createStreamFromEmptyString()
Definition: StreamFactoryTest.php:61
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFileWithInvalidMode
‪createStreamFromFileWithInvalidMode()
Definition: StreamFactoryTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamReturnsAppendableStream
‪createStreamReturnsAppendableStream()
Definition: StreamFactoryTest.php:86
‪TYPO3\CMS\Core\Http\StreamFactory
Definition: StreamFactory.php:27
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\getTestDirectory
‪getTestDirectory(string $prefix='root_')
Definition: StreamFactoryTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest
Definition: StreamFactoryTest.php:32
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\implementsPsr17FactoryInterface
‪implementsPsr17FactoryInterface()
Definition: StreamFactoryTest.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamReturnsEmptyStreamByDefault
‪createStreamReturnsEmptyStreamByDefault()
Definition: StreamFactoryTest.php:53
‪TYPO3\CMS\Core\Tests\Unit\Http\StreamFactoryTest\createStreamFromFile
‪createStreamFromFile()
Definition: StreamFactoryTest.php:95
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57