SlideShare ist ein Scribd-Unternehmen logo
1 von 39
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]
 
 

Weitere ähnliche Inhalte

Was ist angesagt?

Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
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 Aboutjoshua.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 mongersbrian 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 regexbrian d foy
 
Zendcon 2007 Api Design
Zendcon 2007 Api DesignZendcon 2007 Api Design
Zendcon 2007 Api Designunodelostrece
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLsAugusto Pascutti
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tiebrian 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
 

Was ist angesagt? (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
 

Ähnlich wie vfsStream - a better approach for file system dependent tests

Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony TechniquesKris Wallsmith
 
JQuery Presentation
JQuery PresentationJQuery Presentation
JQuery PresentationSony 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_ToolGordon 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 coredrumm
 
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
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedBertrand Dunogier
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHPHari K T
 
Extending Zend Framework
Extending Zend FrameworkExtending Zend Framework
Extending Zend FrameworkPHPBelgium
 
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 PresentationBrent Shaffer
 
AWS Hadoop and PIG and overview
AWS Hadoop and PIG and overviewAWS Hadoop and PIG and overview
AWS Hadoop and PIG and overviewDan 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.3Jeremy Coates
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Kürzlich hochgeladen (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

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.