Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A Mocking tempnam() with vfsstream

This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use tempnam() with vfsStream, I only just now found out from the vfsStre...

posted 3y ago by summea‭  ·  edited 3y ago by summea‭

Answer
#5: Post edited by user avatar summea‭ · 2021-02-17T16:39:33Z (over 3 years ago)
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • Thus, it does not appear that you can use vfsStream with the `tempnam()` function.
  • I don't know if mocking `tempnam()` using another tool like PHP-Mock would help for your situation. Here is a possible example of what mocking `tempnam()` with PHP-Mock could look like, though.
  • **mock.php**
  • ```
  • <?php
  • namespace example;
  • # ref: https://getcomposer.org/doc/01-basic-usage.md
  • require __DIR__ . '/vendor/autoload.php';
  • # ref: https://github.com/php-mock/php-mock#php-mock-api
  • use phpmock\MockBuilder;
  • $builder = new MockBuilder();
  • $builder->setNamespace(__NAMESPACE__)
  • ->setName("tempnam")
  • ->setFunction(
  • function() {
  • return '/tmp/file_abc';
  • }
  • );
  • $mock = $builder->build();
  • $mock->enable();
  • echo(assert(tempnam() == '/tmp/file_abc'));
  • // result should be 1
  • ```
  • **composer.json**
  • ```
  • {
  • "require-dev": {
  • "php-mock/php-mock": "^2.3"
  • }
  • }
  • ```
  • For reference, the code that I was using earlier when trying to find a method to use vfsStream with `tempnam()` looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • Thus, it does not appear that you can use vfsStream with the `tempnam()` function.
  • I don't know if mocking `tempnam()` using another tool like PHP-Mock would help for your situation. Here is a possible example of what mocking `tempnam()` with PHP-Mock could look like, though.
  • **mock.php**
  • ```
  • <?php
  • namespace example;
  • # ref: https://getcomposer.org/doc/01-basic-usage.md
  • require __DIR__ . '/vendor/autoload.php';
  • # ref: https://github.com/php-mock/php-mock#php-mock-api
  • use phpmock\MockBuilder;
  • $builder = new MockBuilder();
  • $builder->setNamespace(__NAMESPACE__)
  • ->setName("tempnam")
  • ->setFunction(
  • function() {
  • return '/tmp/file_abc';
  • }
  • );
  • $mock = $builder->build();
  • $mock->enable();
  • echo(assert(tempnam() == '/tmp/file_abc'));
  • // result should be 1
  • ```
  • **composer.json**
  • ```
  • {
  • "require-dev": {
  • "php-mock/php-mock": "^2.3"
  • }
  • }
  • ```
  • ---
  • For reference, the code that I was using earlier when trying to find a method to use vfsStream with `tempnam()` looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
#4: Post edited by user avatar summea‭ · 2021-02-17T16:38:14Z (over 3 years ago)
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • Thus, it does not appear that you can use vfsStream with the `tempnam()` function.
  • I don't know if mocking `tempnam()` using another tool like PHP-Mock would help for your situation. Here is a possible example of what mocking `tempnam()` with PHP-Mock could look like, though.
  • **mock.php**
  • ```
  • <?php
  • namespace example;
  • # ref: https://getcomposer.org/doc/01-basic-usage.md
  • require __DIR__ . '/vendor/autoload.php';
  • use phpmock\MockBuilder;
  • $builder = new MockBuilder();
  • $builder->setNamespace(__NAMESPACE__)
  • ->setName("tempnam")
  • ->setFunction(
  • function() {
  • return '/tmp/file_abc';
  • }
  • );
  • $mock = $builder->build();
  • $mock->enable();
  • echo(assert(tempnam() == '/tmp/file_abc'));
  • // result should be 1
  • ```
  • **composer.json**
  • ```
  • {
  • "require-dev": {
  • "php-mock/php-mock": "^2.3"
  • }
  • }
  • ```
  • For reference, the code that I was using earlier when trying to find a method to use vfsStream with `tempnam()` looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • Thus, it does not appear that you can use vfsStream with the `tempnam()` function.
  • I don't know if mocking `tempnam()` using another tool like PHP-Mock would help for your situation. Here is a possible example of what mocking `tempnam()` with PHP-Mock could look like, though.
  • **mock.php**
  • ```
  • <?php
  • namespace example;
  • # ref: https://getcomposer.org/doc/01-basic-usage.md
  • require __DIR__ . '/vendor/autoload.php';
  • # ref: https://github.com/php-mock/php-mock#php-mock-api
  • use phpmock\MockBuilder;
  • $builder = new MockBuilder();
  • $builder->setNamespace(__NAMESPACE__)
  • ->setName("tempnam")
  • ->setFunction(
  • function() {
  • return '/tmp/file_abc';
  • }
  • );
  • $mock = $builder->build();
  • $mock->enable();
  • echo(assert(tempnam() == '/tmp/file_abc'));
  • // result should be 1
  • ```
  • **composer.json**
  • ```
  • {
  • "require-dev": {
  • "php-mock/php-mock": "^2.3"
  • }
  • }
  • ```
  • For reference, the code that I was using earlier when trying to find a method to use vfsStream with `tempnam()` looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
#3: Post edited by user avatar summea‭ · 2021-02-17T16:37:01Z (over 3 years ago)
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • Thus, it does not appear that you can use vfsStream with the `tempnam()` function.
  • For reference, the code that I was trying looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • Thus, it does not appear that you can use vfsStream with the `tempnam()` function.
  • I don't know if mocking `tempnam()` using another tool like PHP-Mock would help for your situation. Here is a possible example of what mocking `tempnam()` with PHP-Mock could look like, though.
  • **mock.php**
  • ```
  • <?php
  • namespace example;
  • # ref: https://getcomposer.org/doc/01-basic-usage.md
  • require __DIR__ . '/vendor/autoload.php';
  • use phpmock\MockBuilder;
  • $builder = new MockBuilder();
  • $builder->setNamespace(__NAMESPACE__)
  • ->setName("tempnam")
  • ->setFunction(
  • function() {
  • return '/tmp/file_abc';
  • }
  • );
  • $mock = $builder->build();
  • $mock->enable();
  • echo(assert(tempnam() == '/tmp/file_abc'));
  • // result should be 1
  • ```
  • **composer.json**
  • ```
  • {
  • "require-dev": {
  • "php-mock/php-mock": "^2.3"
  • }
  • }
  • ```
  • For reference, the code that I was using earlier when trying to find a method to use vfsStream with `tempnam()` looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
#2: Post edited by user avatar summea‭ · 2021-02-17T16:19:34Z (over 3 years ago)
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • For reference, the code that I was trying looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
  • This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).
  • Thus, it does not appear that you can use vfsStream with the `tempnam()` function.
  • For reference, the code that I was trying looked like this:
  • **Example.php**
  • ```
  • <?php
  • #ref: https://github.com/bovigo/vfsStream/wiki/Example
  • #ref: https://www.php.net/manual/en/function.tempnam.php
  • namespace Example;
  • class Example
  • {
  • public function __construct()
  • {
  • }
  • public function addFile($directory, $filename)
  • {
  • $tmpfname = tempnam($directory, $filename);
  • $handle = fopen($tmpfname, "w");
  • fwrite($handle, "Hello, world in a file!");
  • fclose($handle);
  • }
  • }
  • ```
  • **ExampleTest.php**
  • ```
  • <?php
  • # ref: https://github.com/bovigo/vfsStream/wiki/Example
  • namespace Example;
  • require_once 'Example.php';
  • use PHPUnit\Framework\TestCase;
  • use org\bovigo\vfs\vfsStream;
  • use org\bovigo\vfs\vfsStreamDirectory;
  • class ExampleTest extends TestCase
  • {
  • private $testDirectory;
  • public function setUp(): void
  • {
  • $this->testDirectory = vfsStream::setup('test_directory');
  • }
  • public function testFileIsAdded()
  • {
  • $e = new Example();
  • $e->addFile(vfsStream::url('test_directory'), 'test_file');
  • $this->assertTrue($this->testDirectory->hasChild('test_file'));
  • }
  • }
  • ```
  • **composer.json**
  • ```
  • {
  • "require": {
  • "mikey179/vfsstream": "^1.6"
  • },
  • "require-dev": {
  • "phpunit/phpunit": "^9.5"
  • }
  • }
  • ```
  • And when running the ExampleTest.php file, the result was this (with username omitted):
  • ```
  • $ vendor/phpunit/phpunit/phpunit ExampleTest.php
  • PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
  • E 1 / 1 (100%)
  • Time: 00:00.027, Memory: 4.00 MB
  • There was 1 error:
  • 1) Example\ExampleTest::testFileIsAdded
  • tempnam(): file created in the system's temporary directory
  • /home/username/programming/q8/Example.php:16
  • /home/username/programming/q8/ExampleTest.php:24
  • ERRORS!
  • Tests: 1, Assertions: 0, Errors: 1.
  • ```
#1: Initial revision by user avatar summea‭ · 2021-02-17T16:16:06Z (over 3 years ago)
This turned out to be a more difficult question to address than I initially thought! After many attempts at finding a way to use `tempnam()` with vfsStream, I only just now found out from [the vfsStream documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues) that `tempnam()` is one of those types of functions that is among the "thin wrappers around the native C functions". These types of functions evidently do not support "stream wrappers like vfs" ([from the documentation](https://github.com/bovigo/vfsStream/wiki/Known-Issues)).

For reference, the code that I was trying looked like this:

**Example.php**

```
<?php

#ref: https://github.com/bovigo/vfsStream/wiki/Example
#ref: https://www.php.net/manual/en/function.tempnam.php

namespace Example;

class Example
{
    public function __construct()
    {
    }

    public function addFile($directory, $filename)
    {
        $tmpfname = tempnam($directory, $filename);
        $handle = fopen($tmpfname, "w");
        fwrite($handle, "Hello, world in a file!");
        fclose($handle);
    }
}
```

**ExampleTest.php**
```
<?php

# ref: https://github.com/bovigo/vfsStream/wiki/Example

namespace Example;

require_once 'Example.php';
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;

class ExampleTest extends TestCase
{
    private $testDirectory;

    public function setUp(): void
    {
        $this->testDirectory = vfsStream::setup('test_directory');
    }

    public function testFileIsAdded()
    {
        $e = new Example();
        $e->addFile(vfsStream::url('test_directory'), 'test_file');
        $this->assertTrue($this->testDirectory->hasChild('test_file'));
    }
}
```

**composer.json**
```
{
    "require": {
        "mikey179/vfsstream": "^1.6"
    },
    "require-dev": {
        "phpunit/phpunit": "^9.5"
    }
}
```

And when running the ExampleTest.php file, the result was this (with username omitted):
```
$ vendor/phpunit/phpunit/phpunit ExampleTest.php
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:00.027, Memory: 4.00 MB

There was 1 error:

1) Example\ExampleTest::testFileIsAdded
tempnam(): file created in the system's temporary directory

/home/username/programming/q8/Example.php:16
/home/username/programming/q8/ExampleTest.php:24

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
```