2

First of all, I'm new in PHPUnit testing and PHP, sorry if I'm missing something too obvious.

Ok, now, for my question: I'm using a Virtual File System called VfsStream to test the function unlink( ). For some reason in my testing this error occurred:

[bianca@cr-22ncg22 tests-phpunit]$ phpunit
PHPUnit 4.6.10 by Sebastian Bergmann and contributors.

Configuration read from /var/www/html/tests-phpunit/phpunit.xml

E

Time: 27 ms, Memory: 4.50Mb

There was 1 error:

1) test\UnlinkTest\UnlinkTest::testUnlink
Object of class App\Libraries\Unlink could not be converted to string

/var/www/html/tests-phpunit/test/UnlinkTest.php:21
/home/bianca/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:153
/home/bianca/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:105

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

I know is something with my Unlink class and what it's returning, but I don't know what is.

The class I'm testing:

class Unlink {

    public function unlinkFile($file) {

        if (!unlink($file)) {
            echo ("Error deleting $file");
        }
        else {
            echo ("Deleted $file");
        }

        return unlink($file);
    }

}

?>

The class where my tests are:

use org\bovigo\vfs\vfsStream;
use App\Libraries\Unlink;

class UnlinkTest extends \PHPUnit_Framework_TestCase {

    public function setUp() {
        $root = vfsStream::setup('home');
        $removeFile = new Unlink();
    }

    public function tearDown() {
        $root = null;
    }

    public function testUnlink() {
        $root = vfsStream::setup('home');
        $removeFile = new Unlink();
        vfsStream::newFile('test.txt', 0744)->at($root)->setContent("The new contents of the file");
        $this->$removeFile->unlinkFile(vfsStream::url('home/test.txt'));
        $this->assertFalse(var_dump(file_exists(vfsStream::url('home/test.txt'))));
    }

}

?>

Anybody can help me to fix it?

B. Pereira
  • 75
  • 1
  • 13

1 Answers1

2

The error you're getting is because initially you create this local variable:

$removeFile = new Unlink();

But then you refer to it as $this->$removeFile when you do:

$this->$removeFile->unlinkFile(vfsStream::url('home/test.txt'));

That's not correct; you might use that where you have a class variable and you want to refer to it dynamically. e.g.

class YourClass {
    public $foo;
    public $bar;

    public function __construct() {
        $this->foo = 'hello';
        $this->bar = 'world';
    }

    public function doStuff() {
        $someVariable = 'foo';

        echo $this->$someVariable;  // outputs 'hello'
    }
}

All you need to do is get rid of the $this, and change it to:

$removeFile->unlinkFile(vfsStream::url('home/test.txt'));
duncan
  • 31,401
  • 13
  • 78
  • 99
  • I knew it was a simple error like this! haha Thank you, @duncan. Now I need to fix other problems with vfsStream lol – B. Pereira Mar 31 '16 at 20:08
  • @B.Pereira if this answer solved your issue please accept it. If you're not familiar with stackoverflow please take a tour here: http://stackoverflow.com/tour – Jakub Zalas Apr 01 '16 at 07:35