StreamFactoryTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 5.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. use Cake\Http\StreamFactory;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Test case for the stream factory.
  21. */
  22. class StreamFactoryTest extends TestCase
  23. {
  24. protected StreamFactory $factory;
  25. protected string $filename = TMP . 'stream-factory-file-test.txt';
  26. protected function setUp(): void
  27. {
  28. parent::setUp();
  29. $this->factory = new StreamFactory();
  30. }
  31. protected function tearDown(): void
  32. {
  33. parent::tearDown();
  34. // phpcs:disable
  35. @unlink($this->filename);
  36. // phpcs:enable
  37. }
  38. public function testCreateStream(): void
  39. {
  40. $stream = $this->factory->createStream('test');
  41. $this->assertSame('test', $stream->getContents());
  42. }
  43. public function testCreateStreamFile(): void
  44. {
  45. file_put_contents($this->filename, 'it works');
  46. $stream = $this->factory->createStreamFromFile($this->filename);
  47. $this->assertSame('it works', $stream->getContents());
  48. }
  49. public function testCreateStreamResource(): void
  50. {
  51. file_put_contents($this->filename, 'it works');
  52. $resource = fopen($this->filename, 'r');
  53. $stream = $this->factory->createStreamFromResource($resource);
  54. $this->assertSame('it works', $stream->getContents());
  55. fclose($resource);
  56. }
  57. }