Skip to content

Commit

Permalink
upgrade examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Kleine committed Jan 14, 2012
1 parent 6007e9e commit fc2886b
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 138 deletions.
17 changes: 8 additions & 9 deletions examples/Example.php
@@ -1,29 +1,28 @@
<?php
/**
* Example class.
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package bovigo_vfs
* @subpackage examples
* @version $Id$
* @package org\bovigo\vfs
*/
namespace org\bovigo\vfs\example;
/**
* Example class.
*
* @package bovigo_vfs
* @subpackage examples
*/
class Example
{
/**
* id of the example
*
* @var string
* @type string
*/
protected $id;
/**
* a directory where we do something..
*
* @var string
* @type string
*/
protected $directory;

Expand Down
34 changes: 16 additions & 18 deletions examples/ExampleTestCaseOldWay.php
@@ -1,28 +1,26 @@
<?php
/**
* Test case for class Example.
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package bovigo_vfs
* @subpackage examples
* @version $Id$
* @package org\bovigo\vfs
*/
require_once 'PHPUnit/Framework.php';
namespace org\bovigo\vfs\example;
require_once 'Example.php';
/**
* Test case for class Example.
*
* @package bovigo_vfs
* @subpackage examples
*/
class ExampleTestCaseOldWay extends PHPUnit_Framework_TestCase
class ExampleTestCaseOldWay extends \PHPUnit_Framework_TestCase
{
/**
* set up test environmemt
*/
public function setUp()
{
if (file_exists(dirname(__FILE__) . '/id') === true) {
rmdir(dirname(__FILE__) . '/id');
if (file_exists(__DIR__ . '/id') === true) {
rmdir(__DIR__ . '/id');
}
}

Expand All @@ -31,20 +29,20 @@ public function setUp()
*/
public function tearDown()
{
if (file_exists(dirname(__FILE__) . '/id') === true) {
rmdir(dirname(__FILE__) . '/id');
if (file_exists(__DIR__ . '/id') === true) {
rmdir(__DIR__ . '/id');
}
}

/**
* test that the directory is created
* @test
*/
public function testDirectoryIsCreated()
public function directoryIsCreated()
{
$example = new Example('id');
$this->assertFalse(file_exists(dirname(__FILE__) . '/id'));
$example->setDirectory(dirname(__FILE__));
$this->assertTrue(file_exists(dirname(__FILE__) . '/id'));
$this->assertFalse(file_exists(__DIR__ . '/id'));
$example->setDirectory(__DIR__);
$this->assertTrue(file_exists(__DIR__ . '/id'));
}
}
?>
32 changes: 20 additions & 12 deletions examples/ExampleTestCaseWithVfsStream.php
@@ -1,39 +1,47 @@
<?php
/**
* Test case for class Example.
* This file is part of vfsStream.
*
* @package bovigo_vfs
* @subpackage examples
* @version $Id$
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package org\bovigo\vfs
*/
require_once 'PHPUnit/Framework.php';
require_once 'vfsStream/vfsStream.php';
namespace org\bovigo\vfs\example;
use org\bovigo\vfs\vfsStream;
require_once 'Example.php';
/**
* Test case for class Example.
*
* @package bovigo_vfs
* @subpackage examples
*/
class ExampleTestCaseWithVfsStream extends PHPUnit_Framework_TestCase
class ExampleTestCaseWithVfsStream extends \PHPUnit_Framework_TestCase
{
/**
* root directory
*
* @type vfsStreamDirectory
*/
protected $root;

/**
* set up test environmemt
*/
public function setUp()
{
vfsStream::setup('exampleDir');
$this->root = vfsStream::setup('exampleDir');
}

/**
* test that the directory is created
* @test
*/
public function testDirectoryIsCreated()
public function directoryIsCreated()
{
$example = new Example('id');
$this->assertFalse(vfsStreamWrapper::getRoot()->hasChild('id'));
$this->assertFalse($this->root->hasChild('id'));
$example->setDirectory(vfsStream::url('exampleDir'));
$this->assertTrue(vfsStreamWrapper::getRoot()->hasChild('id'));
$this->assertTrue($this->root->hasChild('id'));
}
}
?>
17 changes: 8 additions & 9 deletions examples/FailureExample.php
@@ -1,23 +1,22 @@
<?php
/**
* Example class to demonstrate testing of failure behaviour with vfsStream.
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package bovigo_vfs
* @subpackage examples
* @version $Id$
* @package org\bovigo\vfs
*/
namespace org\bovigo\vfs\example;
/**
* Example class to demonstrate testing of failure behaviour with vfsStream.
*
* @package bovigo_vfs
* @subpackage examples
*/
class FailureExample
{
/**
* filename to write data
*
* @var string
* @type string
*/
protected $filename;

Expand All @@ -42,7 +41,7 @@ public function writeData($data)
if (false === $bytes) {
return 'could not write data';
}

return 'ok';
}

Expand Down
56 changes: 27 additions & 29 deletions examples/FailureExampleTestCase.php
@@ -1,60 +1,58 @@
<?php
/**
* Test case for class FailureExample.
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package bovigo_vfs
* @subpackage examples
* @version $Id$
* @package org\bovigo\vfs
*/
require_once 'PHPUnit/Framework.php';
require_once 'vfsStream/vfsStream.php';
namespace org\bovigo\vfs\example;
use org\bovigo\vfs\vfsStream;
require_once 'FailureExample.php';
/**
* Test case for class FailureExample.
*
* @package bovigo_vfs
* @subpackage examples
*/
class FailureExampleTestCase extends PHPUnit_Framework_TestCase
class FailureExampleTestCase extends \PHPUnit_Framework_TestCase
{
/**
* root directory
*
* @type vfsStreamDirectory
*/
protected $root;

/**
* set up test environmemt
*/
public function setUp()
{
vfsStreamWrapper::register();
$this->root = vfsStream::setup('exampleDir');
}

/**
* no failure case
* @test
*/
public function testNoFailure()
public function returnsOkOnNoFailure()
{
$root = new vfsStreamDirectory('exampleDir');
vfsStreamWrapper::setRoot($root);
$example = new FailureExample(vfsStream::url('exampleDir/test.txt'));
$this->assertSame('ok', $example->writeData('testdata'));
$this->assertTrue($root->hasChild('test.txt'));
$this->assertSame('testdata', $root->getChild('test.txt')->getContent());
$this->assertTrue($this->root->hasChild('test.txt'));
$this->assertSame('testdata', $this->root->getChild('test.txt')->getContent());
}

/**
* can't write to file
* @test
*/
public function testNoWrite()
public function returnsErrorMessageIfWritingToFileFails()
{
$root = new vfsStreamDirectory('exampleDir');
$file = $this->getMock('vfsStreamFile', array('write'), array('test.txt'));
$file->setContent('notoverwritten');
$file->expects($this->once())
->method('write')
->will($this->returnValue(false));
$root->addChild($file);
vfsStreamWrapper::setRoot($root);
$file = vfsStream::newFile('test.txt', 0000)
->withContent('notoverwritten')
->at($this->root);
$example = new FailureExample(vfsStream::url('exampleDir/test.txt'));
$this->assertSame('could not write data', $example->writeData('testdata'));
$this->assertTrue($root->hasChild('test.txt'));
$this->assertSame('notoverwritten', $root->getChild('test.txt')->getContent());
$this->assertTrue($this->root->hasChild('test.txt'));
$this->assertSame('notoverwritten', $this->root->getChild('test.txt')->getContent());
}
}
?>
36 changes: 17 additions & 19 deletions examples/FileModeExampleTestCaseOldWay.php
@@ -1,28 +1,26 @@
<?php
/**
* Test case for class FilemodeExample.
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package stubbles_vfs
* @subpackage examples
* @version $Id$
* @package org\bovigo\vfs
*/
require_once 'PHPUnit/Framework.php';
namespace org\bovigo\vfs\example;
require_once 'FilemodeExample.php';
/**
* Test case for class FilemodeExample.
*
* @package stubbles_vfs
* @subpackage examples
*/
class FileModeExampleTestCaseOldWay extends PHPUnit_Framework_TestCase
class FilemodeExampleTestCaseOldWay extends \PHPUnit_Framework_TestCase
{
/**
* set up test environmemt
*/
public function setUp()
{
if (file_exists(dirname(__FILE__) . '/id') === true) {
rmdir(dirname(__FILE__) . '/id');
if (file_exists(__DIR__ . '/id') === true) {
rmdir(__DIR__ . '/id');
}
}

Expand All @@ -31,8 +29,8 @@ public function setUp()
*/
public function tearDown()
{
if (file_exists(dirname(__FILE__) . '/id') === true) {
rmdir(dirname(__FILE__) . '/id');
if (file_exists(__DIR__ . '/id') === true) {
rmdir(__DIR__ . '/id');
}
}

Expand All @@ -42,12 +40,12 @@ public function tearDown()
public function testDirectoryHasCorrectDefaultFilePermissions()
{
$example = new FilemodeExample('id');
$example->setDirectory(dirname(__FILE__));
$example->setDirectory(__DIR__);
if (DIRECTORY_SEPARATOR === '\\') {
// can not really test on windows, filemode from mkdir() is ignored
$this->assertEquals(40777, decoct(fileperms(dirname(__FILE__) . '/id')));
$this->assertEquals(40777, decoct(fileperms(__DIR__ . '/id')));
} else {
$this->assertEquals(40700, decoct(fileperms(dirname(__FILE__) . '/id')));
$this->assertEquals(40700, decoct(fileperms(__DIR__ . '/id')));
}
}

Expand All @@ -57,12 +55,12 @@ public function testDirectoryHasCorrectDefaultFilePermissions()
public function testDirectoryHasCorrectDifferentFilePermissions()
{
$example = new FilemodeExample('id', 0755);
$example->setDirectory(dirname(__FILE__));
$example->setDirectory(__DIR__);
if (DIRECTORY_SEPARATOR === '\\') {
// can not really test on windows, filemode from mkdir() is ignored
$this->assertEquals(40777, decoct(fileperms(dirname(__FILE__) . '/id')));
$this->assertEquals(40777, decoct(fileperms(__DIR__ . '/id')));
} else {
$this->assertEquals(40755, decoct(fileperms(dirname(__FILE__) . '/id')));
$this->assertEquals(40755, decoct(fileperms(__DIR__ . '/id')));
}
}
}
Expand Down

0 comments on commit fc2886b

Please sign in to comment.