SlideShare a Scribd company logo
vfsStream A better approach for file system dependent tests Frank Kleine, 1&1 Internet AG
 
 
 
 
 
 
T-Shirt available at zazzle.de (no, I'm not paid for this)
 
(Obligatory Zen-Style image)
 
AND NOW FOR SOMETHING COMPLETELY DIFFERENT
Unit tests ,[object Object],[object Object],[object Object]
Basic example: a method to test class Example {   public function __construct($id) {    $this->id = $id;   }  public function setDirectory($dir) {   $this->dir = $dir . '/' . $this->id;     if (file_exists($this->dir) === false) {         mkdir($this->dir, 0700, true);     }   }   … }
Basic example: traditional test $DIR = dirname(__FILE__); public function setUp() {   if (file_exists($DIR . '/id')) {       rmdir($DIR . '/id');   } } public function tearDown() {   if (file_exists($DIR . '/id')) {       rmdir($DIR . '/id');   } } public function testDirectoryIsCreated() {   $example = new Example('id');   $this->assertFalse(file_exists($DIR . '/id'));   $example->setDirectory($DIR);   $this->assertTrue(file_exists($DIR . '/id')); }
Basic example: vfsStream test public function setUp() {   vfsStreamWrapper::register();   $root = new vfsStreamDirectory('aDir');   vfsStreamWrapper::setRoot($root); } public function testDirectoryIsCreated() {   $url = vfsStream::url('aDir/id');   $example = new Example('id');   $this->assertFalse(file_exists($url));   $example->setDirectory(vfsStream::url('aDir'));   $this->assertTrue(file_exists($url)); }
Advantages ,[object Object],[object Object],[object Object]
How does this work? ,[object Object],[object Object],[object Object],[object Object]
What does not work? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example with file mode class Example {   public function __construct($id,  $mode = 0700) {    $this->id   = $id;    $this->mode = $mode;   }  public function setDirectory($dir) {   $this->dir = $dir . '/' . $this->id;     if (file_exists($this->dir) === false) {         mkdir($this->directory, $this->mode, true);     }   }   … }
Example with file mode, cont. $DIR = dirname(__FILE__);   public function testDirDefaultFilePermissions() {   $example = new Example('id');   $example->setDirectory($DIR);   if (DIRECTORY_SEPARATOR === '') {     $this->assertEquals(40777, decoct(fileperms($DIR . '/id')));   } else {     $this->assertEquals(40700, decoct(fileperms($DIR . '/id')));   } } public function testDirDifferentFilePermissions() {   $example = new Example('id', 0755);   $example->setDirectory($DIR);   if (DIRECTORY_SEPARATOR === '') {     $this->assertEquals(40777, decoct(fileperms($DIR . '/id')));   } else {     $this->assertEquals(40755, decoct(fileperms($DIR . '/id')));   } }
Example with file mode, cont. public function setUp() {   vfsStreamWrapper::register();   $this->root = new vfsStreamDirectory('aDir');   vfsStreamWrapper::setRoot($this->root); } public function testDirDefaultFilePermissions() {   $example = new Example('id');   $example->setDirectory(vfsStream::url('aDir'));   $this->assertEquals(0700,   $this->root->getChild('id')->getPermissions());  } public function testDirDifferentFilePermissions() {   $example = new Example('id', 0755);   $example->setDirectory(vfsStream::url('aDir'));   $this->assertEquals(0755,   $this->root->getChild('id')->getPermissions()); }
Advantages ,[object Object],[object Object],[object Object]
Different config files class RssFeedController { public function __construct($configPath) { $feeds = Properties::fromFile($configPath . '/rss-feeds.ini') ->getSection('feeds', array()); if (count($feeds) === 0) { throw new ConfigurationException(); } $this->routeName = valueFromRequest(); if (null === $this->routeName) { // no special feed requested, use first configured one reset($feeds); $this->routeName = key($feeds); } } … }
Different config files, cont. public function setUp() {   vfsStreamWrapper::register();   $root = new vfsStreamDirectory('config');   vfsStreamWrapper::setRoot($root); $this->configFile = vfsStream::newFile('rss-feeds.ini')   ->at($root); } /** * @test * @expectedException  FileNotFoundException **/ public function loadFeedsFailsIfFeedConfigFileDoesNotExist() { $example = new RssFeedController(vfsStream::url('doesNotExist')); }
Different config files, cont. 2 /** * @test * @expectedException  ConfigurationException **/ public function noFeedsSectionConfiguredThrowsException() { $this->configFile->setContent(''); $example = new RssFeedController(vfsStream::url('config')); }
Different config files, cont. 3 /** * @test * @expectedException  ConfigurationException **/ public function noFeedsConfiguredThrowsException() { $this->configFile->setContent('[feeds]'); $example = new RssFeedController(vfsStream::url('config')); }
Different config files, cont. 4 /** * @test **/ public function selectsFirstFeedIfNoneGivenWithRequestValue() { $this->configFile->setContent('[feeds] default = "org::stubbles::test::xml::rss::DefaultFeed"'); $example = new RssFeedController(vfsStream::url('config')); // assertions that the default feed was selected … }
Different config files, cont. 5 /** * @test **/ public function selectsOtherFeedBasedOnRequestValue() { $this->configFile->setContent("[feeds] default = amp;quot;org::stubbles::test::xml::rss::DefaultFeedamp;quot; other = amp;quot;org::stubbles::test::xml::rss::OtherFeedamp;quot;"); $example = new RssFeedController(vfsStream::url('config')); // assertions that the other feed was selected … }
Advantages ,[object Object],[object Object],[object Object],[object Object]
vfsStream 0.7.0 ,[object Object],[object Object],[object Object],[object Object]
File permissions class Example {   public function writeConfig($config, $configFile) {   file_put_contents($configFile, serialize($config)); } … }
File permissions, the tests /** * @test */ public function normalTest() { vfsStreamWrapper::setRoot(vfsStream::newDirectory('exampleDir')); $example = new FilePermissionsExample(); $example->writeConfig(array('foo' => 'bar'), vfsStream::url('exampleDir/writable.ini') ); // assertions here }
File permissions, another test /** * @test */ public function directoryNotWritable() { vfsStreamWrapper::setRoot( vfsStream::newDirectory('exampleDir', 0444) ); $example = new FilePermissionsExample(); $example->writeConfig(array('foo' => 'bar'),    vfsStream::url('exampleDir/config.ini') ); }
Advantages ,[object Object],[object Object],[object Object]
 
Ressources ,[object Object],[object Object],[object Object],[object Object],[object Object]
 
 

More Related Content

What's hot

Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
Bastian Feder
 
Bioinformatics p5-bioperlv2014
Bioinformatics p5-bioperlv2014Bioinformatics p5-bioperlv2014
Bioinformatics p5-bioperlv2014
Prof. Wim Van Criekinge
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
Andrew Shitov
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of SmartmatchAndrew Shitov
 
Utility Modules That You Should Know About
Utility Modules That You Should Know AboutUtility Modules That You Should Know About
Utility Modules That You Should Know About
joshua.mcadams
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regex
brian d foy
 
Zendcon 2007 Api Design
Zendcon 2007 Api DesignZendcon 2007 Api Design
Zendcon 2007 Api Designunodelostrece
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
brian d foy
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
Augusto Pascutti
 
Workshop unittesting
Workshop unittestingWorkshop unittesting
Workshop unittesting
Joshua Thijssen
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
brian d foy
 
Legacy applications - 4Developes konferencja, Piotr Pasich
Legacy applications  - 4Developes konferencja, Piotr PasichLegacy applications  - 4Developes konferencja, Piotr Pasich
Legacy applications - 4Developes konferencja, Piotr PasichPiotr Pasich
 
PHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look AheadPHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look Aheadthinkphp
 

What's hot (20)

Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Five
FiveFive
Five
 
Bioinformatics p5-bioperlv2014
Bioinformatics p5-bioperlv2014Bioinformatics p5-bioperlv2014
Bioinformatics p5-bioperlv2014
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
 
Utility Modules That You Should Know About
Utility Modules That You Should Know AboutUtility Modules That You Should Know About
Utility Modules That You Should Know About
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regex
 
Zendcon 2007 Api Design
Zendcon 2007 Api DesignZendcon 2007 Api Design
Zendcon 2007 Api Design
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
 
Perl5i
Perl5iPerl5i
Perl5i
 
Workshop unittesting
Workshop unittestingWorkshop unittesting
Workshop unittesting
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
 
Legacy applications - 4Developes konferencja, Piotr Pasich
Legacy applications  - 4Developes konferencja, Piotr PasichLegacy applications  - 4Developes konferencja, Piotr Pasich
Legacy applications - 4Developes konferencja, Piotr Pasich
 
PHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look AheadPHP 5.3 And PHP 6 A Look Ahead
PHP 5.3 And PHP 6 A Look Ahead
 
Shell.php
Shell.phpShell.php
Shell.php
 

Similar to vfsStream - a better approach for file system dependent tests

Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
Kris Wallsmith
 
JQuery Presentation
JQuery PresentationJQuery Presentation
JQuery Presentation
Sony Jain
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
Gordon Forsythe
 
Maintaining your own branch of Drupal core
Maintaining your own branch of Drupal coreMaintaining your own branch of Drupal core
Maintaining your own branch of Drupal core
drumm
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappersPositive Hack Days
 
Php frameworks
Php frameworksPhp frameworks
Php frameworks
Anil Kumar Panigrahi
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster Unleashed
Bertrand Dunogier
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHPHari K T
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
Tagged Social
 
Extending Zend Framework
Extending Zend FrameworkExtending Zend Framework
Extending Zend Framework
PHPBelgium
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
Valentine Dianov
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
Lars Jankowfsky
 
Building a horizontally scalable API in php
Building a horizontally scalable API in phpBuilding a horizontally scalable API in php
Building a horizontally scalable API in phpWade Womersley
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitPeter Wilcsinszky
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes Presentation
Brent Shaffer
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
Michelangelo van Dam
 
AWS Hadoop and PIG and overview
AWS Hadoop and PIG and overviewAWS Hadoop and PIG and overview
AWS Hadoop and PIG and overview
Dan Morrill
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
andrewnacin
 
What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3
Jeremy Coates
 

Similar to vfsStream - a better approach for file system dependent tests (20)

Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
JQuery Presentation
JQuery PresentationJQuery Presentation
JQuery Presentation
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 
Framework
FrameworkFramework
Framework
 
Maintaining your own branch of Drupal core
Maintaining your own branch of Drupal coreMaintaining your own branch of Drupal core
Maintaining your own branch of Drupal core
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Php frameworks
Php frameworksPhp frameworks
Php frameworks
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster Unleashed
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
Extending Zend Framework
Extending Zend FrameworkExtending Zend Framework
Extending Zend Framework
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Building a horizontally scalable API in php
Building a horizontally scalable API in phpBuilding a horizontally scalable API in php
Building a horizontally scalable API in php
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnit
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes Presentation
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 
AWS Hadoop and PIG and overview
AWS Hadoop and PIG and overviewAWS Hadoop and PIG and overview
AWS Hadoop and PIG and overview
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
 
What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3
 

Recently uploaded

Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 

Recently uploaded (20)

Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 

vfsStream - a better approach for file system dependent tests

  • 1. vfsStream A better approach for file system dependent tests Frank Kleine, 1&1 Internet AG
  • 2.  
  • 3.  
  • 4.  
  • 5.  
  • 6.  
  • 7.  
  • 8. T-Shirt available at zazzle.de (no, I'm not paid for this)
  • 9.  
  • 11.  
  • 12. AND NOW FOR SOMETHING COMPLETELY DIFFERENT
  • 13.
  • 14. Basic example: a method to test class Example { public function __construct($id) {   $this->id = $id; } public function setDirectory($dir) { $this->dir = $dir . '/' . $this->id;     if (file_exists($this->dir) === false) {         mkdir($this->dir, 0700, true);     } } … }
  • 15. Basic example: traditional test $DIR = dirname(__FILE__); public function setUp() { if (file_exists($DIR . '/id')) {       rmdir($DIR . '/id');   } } public function tearDown() { if (file_exists($DIR . '/id')) {       rmdir($DIR . '/id');   } } public function testDirectoryIsCreated() { $example = new Example('id');   $this->assertFalse(file_exists($DIR . '/id'));   $example->setDirectory($DIR);   $this->assertTrue(file_exists($DIR . '/id')); }
  • 16. Basic example: vfsStream test public function setUp() { vfsStreamWrapper::register(); $root = new vfsStreamDirectory('aDir');   vfsStreamWrapper::setRoot($root); } public function testDirectoryIsCreated() { $url = vfsStream::url('aDir/id');   $example = new Example('id');   $this->assertFalse(file_exists($url));   $example->setDirectory(vfsStream::url('aDir'));   $this->assertTrue(file_exists($url)); }
  • 17.
  • 18.
  • 19.
  • 20. Example with file mode class Example { public function __construct($id,  $mode = 0700) {   $this->id   = $id;   $this->mode = $mode; } public function setDirectory($dir) { $this->dir = $dir . '/' . $this->id;     if (file_exists($this->dir) === false) {         mkdir($this->directory, $this->mode, true);     } } … }
  • 21. Example with file mode, cont. $DIR = dirname(__FILE__); public function testDirDefaultFilePermissions() { $example = new Example('id');   $example->setDirectory($DIR);   if (DIRECTORY_SEPARATOR === '') {     $this->assertEquals(40777, decoct(fileperms($DIR . '/id')));   } else {     $this->assertEquals(40700, decoct(fileperms($DIR . '/id')));   } } public function testDirDifferentFilePermissions() {   $example = new Example('id', 0755);   $example->setDirectory($DIR);   if (DIRECTORY_SEPARATOR === '') {     $this->assertEquals(40777, decoct(fileperms($DIR . '/id')));   } else {     $this->assertEquals(40755, decoct(fileperms($DIR . '/id')));   } }
  • 22. Example with file mode, cont. public function setUp() { vfsStreamWrapper::register(); $this->root = new vfsStreamDirectory('aDir');   vfsStreamWrapper::setRoot($this->root); } public function testDirDefaultFilePermissions() { $example = new Example('id');   $example->setDirectory(vfsStream::url('aDir'));   $this->assertEquals(0700, $this->root->getChild('id')->getPermissions()); } public function testDirDifferentFilePermissions() {   $example = new Example('id', 0755);   $example->setDirectory(vfsStream::url('aDir'));   $this->assertEquals(0755, $this->root->getChild('id')->getPermissions()); }
  • 23.
  • 24. Different config files class RssFeedController { public function __construct($configPath) { $feeds = Properties::fromFile($configPath . '/rss-feeds.ini') ->getSection('feeds', array()); if (count($feeds) === 0) { throw new ConfigurationException(); } $this->routeName = valueFromRequest(); if (null === $this->routeName) { // no special feed requested, use first configured one reset($feeds); $this->routeName = key($feeds); } } … }
  • 25. Different config files, cont. public function setUp() { vfsStreamWrapper::register(); $root = new vfsStreamDirectory('config');   vfsStreamWrapper::setRoot($root); $this->configFile = vfsStream::newFile('rss-feeds.ini') ->at($root); } /** * @test * @expectedException FileNotFoundException **/ public function loadFeedsFailsIfFeedConfigFileDoesNotExist() { $example = new RssFeedController(vfsStream::url('doesNotExist')); }
  • 26. Different config files, cont. 2 /** * @test * @expectedException ConfigurationException **/ public function noFeedsSectionConfiguredThrowsException() { $this->configFile->setContent(''); $example = new RssFeedController(vfsStream::url('config')); }
  • 27. Different config files, cont. 3 /** * @test * @expectedException ConfigurationException **/ public function noFeedsConfiguredThrowsException() { $this->configFile->setContent('[feeds]'); $example = new RssFeedController(vfsStream::url('config')); }
  • 28. Different config files, cont. 4 /** * @test **/ public function selectsFirstFeedIfNoneGivenWithRequestValue() { $this->configFile->setContent('[feeds] default = "org::stubbles::test::xml::rss::DefaultFeed"'); $example = new RssFeedController(vfsStream::url('config')); // assertions that the default feed was selected … }
  • 29. Different config files, cont. 5 /** * @test **/ public function selectsOtherFeedBasedOnRequestValue() { $this->configFile->setContent("[feeds] default = amp;quot;org::stubbles::test::xml::rss::DefaultFeedamp;quot; other = amp;quot;org::stubbles::test::xml::rss::OtherFeedamp;quot;"); $example = new RssFeedController(vfsStream::url('config')); // assertions that the other feed was selected … }
  • 30.
  • 31.
  • 32. File permissions class Example { public function writeConfig($config, $configFile) { file_put_contents($configFile, serialize($config)); } … }
  • 33. File permissions, the tests /** * @test */ public function normalTest() { vfsStreamWrapper::setRoot(vfsStream::newDirectory('exampleDir')); $example = new FilePermissionsExample(); $example->writeConfig(array('foo' => 'bar'), vfsStream::url('exampleDir/writable.ini') ); // assertions here }
  • 34. File permissions, another test /** * @test */ public function directoryNotWritable() { vfsStreamWrapper::setRoot( vfsStream::newDirectory('exampleDir', 0444) ); $example = new FilePermissionsExample(); $example->writeConfig(array('foo' => 'bar'), vfsStream::url('exampleDir/config.ini') ); }
  • 35.
  • 36.  
  • 37.
  • 38.  
  • 39.