Skip to content

Commit

Permalink
[Filesystem] dontSeeFileFound searches in path (#3882)
Browse files Browse the repository at this point in the history
* Updated FTP and SFTP tests

Didn't run them

* [Filesystem] Created tests for (dont)SeeFileFound

To verify #3877

* [Filesystem] dontSeeFileFound searches in path

Fixes #3877

* [Filesystem] Set parameter types in docblocks

* Reduced code duplication
  • Loading branch information
Naktibalda authored and DavertMik committed Jan 10, 2017
1 parent 8e55204 commit 2aeb8cd
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 40 deletions.
101 changes: 67 additions & 34 deletions src/Codeception/Module/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ public function _before(TestInterface $test)
* Enters a directory In local filesystem.
* Project root directory is used by default
*
* @param $path
* @param string $path
*/
public function amInPath($path)
{
chdir($this->path = $this->absolutizePath($path) . DIRECTORY_SEPARATOR);
$this->debug('Moved to ' . getcwd());
}

/**
* @param string $path
* @return string
*/
protected function absolutizePath($path)
{
// *nix way
Expand All @@ -69,11 +73,12 @@ protected function absolutizePath($path)
* ?>
* ```
*
* @param $filename
* @param string $filename
*/
public function openFile($filename)
{
$this->file = file_get_contents($this->absolutizePath($filename));
$this->filepath = $filename;
}

/**
Expand All @@ -85,7 +90,7 @@ public function openFile($filename)
* ?>
* ```
*
* @param $filename
* @param string $filename
*/
public function deleteFile($filename)
{
Expand All @@ -104,7 +109,7 @@ public function deleteFile($filename)
* ?>
* ```
*
* @param $dirname
* @param string $dirname
*/
public function deleteDir($dirname)
{
Expand All @@ -121,8 +126,8 @@ public function deleteDir($dirname)
* ?>
* ```
*
* @param $src
* @param $dst
* @param string $src
* @param string $dst
*/
public function copyDir($src, $dst)
{
Expand All @@ -141,7 +146,7 @@ public function copyDir($src, $dst)
* ?>
* ```
*
* @param $text
* @param string $text
*/
public function seeInThisFile($text)
{
Expand Down Expand Up @@ -174,7 +179,7 @@ public function seeNumberNewLines($number)
/**
* Checks that contents of currently opened file matches $regex
*
* @param $regex
* @param string $regex
*/
public function seeThisFileMatches($regex)
{
Expand All @@ -194,7 +199,7 @@ public function seeThisFileMatches($regex)
* ?>
* ```
*
* @param $text
* @param string $text
*/
public function seeFileContentsEqual($text)
{
Expand All @@ -212,7 +217,7 @@ public function seeFileContentsEqual($text)
* ?>
* ```
*
* @param $text
* @param string $text
*/
public function dontSeeInThisFile($text)
{
Expand All @@ -237,46 +242,74 @@ public function deleteThisFile()
* ?>
* ```
*
* @param $filename
* @param string $filename
* @param string $path
*/
public function seeFileFound($filename, $path = '')
{
if (file_exists($filename) and !$path) {
if ($path === '' && file_exists($filename)) {
$this->openFile($filename);
$this->filepath = $filename;
$this->debug($filename);
\PHPUnit_Framework_Assert::assertFileExists($path . $filename);
\PHPUnit_Framework_Assert::assertFileExists($filename);
return;
}

$path = $this->absolutizePath($path);
$this->debug($path);
if (!file_exists($path)) {
$this->fail("Directory does not exist: $path");
}
$found = $this->findFileInPath($filename, $path);

$files = Finder::create()->files()->name($filename)->in($path);
foreach ($files as $file) {
$file = $file->getRealPath();
$this->openFile($file);
$this->filepath = $file;
$this->debug($file);
\PHPUnit_Framework_Assert::assertFileExists($file);
return;
if ($found === false) {
$this->fail("File \"$filename\" not found at \"$path\"");
}
$this->fail("File \"$filename\" not found at \"$path\"");

$this->openFile($found);
\PHPUnit_Framework_Assert::assertFileExists($found);
}

/**
* Checks if file does not exist in path
*
* @param $filename
* @param string $filename
* @param string $path
*/
public function dontSeeFileFound($filename, $path = '')
{
\PHPUnit_Framework_Assert::assertFileNotExists($path . $filename);
if ($path === '') {
\PHPUnit_Framework_Assert::assertFileNotExists($filename);
return;
}

$found = $this->findFileInPath($filename, $path);

if ($found === false) {
//this line keeps a count of assertions correct
\PHPUnit_Framework_Assert::assertTrue(true);
return;
}

\PHPUnit_Framework_Assert::assertFileNotExists($found);
}

/**
* Finds the first matching file
*
* @param string $filename
* @param string $path
* @throws \PHPUnit_Framework_AssertionFailedError When path does not exist
* @return string|false Path to the first matching file
*/
private function findFileInPath($filename, $path)
{
$path = $this->absolutizePath($path);
if (!file_exists($path)) {
$this->fail("Directory does not exist: $path");
}

$files = Finder::create()->files()->name($filename)->in($path);
if ($files->count() === 0) {
return false;
}

foreach ($files as $file) {
return $file->getRealPath();
}
}


Expand All @@ -289,7 +322,7 @@ public function dontSeeFileFound($filename, $path = '')
* ?>
* ```
*
* @param $dirname
* @param string $dirname
*/
public function cleanDir($dirname)
{
Expand All @@ -300,8 +333,8 @@ public function cleanDir($dirname)
/**
* Saves contents to file
*
* @param $filename
* @param $contents
* @param string $filename
* @param string $contents
*/
public function writeToFile($filename, $contents)
{
Expand Down
1 change: 0 additions & 1 deletion tests/cli/BootstrapCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function bootstrapEmpty(\CliGuy $I)
{
$I->executeCommand('bootstrap --empty');
$I->dontSeeFileFound('tests/acceptance');
$I->dontSeeFileFound('AcceptanceTester.php', 'tests/acceptance');
$I->seeFileFound('codeception.yml');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cli/RunCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function runTestWithFailFast(\CliGuy $I)
$I->dontSeeInShellOutput("PassingTest: Me");
}

public function runWithCustomOuptutPath(\CliGuy $I)
public function runWithCustomOutputPath(\CliGuy $I)
{
$I->executeCommand('run dummy --xml myverycustom.xml --html myownhtmlreport.html');
$I->seeFileFound('myverycustom.xml', 'tests/_output');
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Codeception/Module/FTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function setUp()
$this->module = new \Codeception\Module\FTP(make_container());
$this->module->_setConfig($this->config);

$this->module->_before(Stub::make('\Codeception\TestCase'));
$this->module->_before(Stub::makeEmpty('\Codeception\Test\Test'));
}

/**
Expand Down Expand Up @@ -119,6 +119,6 @@ public function flow()

public function tearDown()
{
$this->module->_after();
$this->module->_after(Stub::makeEmpty('\Codeception\Test\Test'));
}
}
90 changes: 90 additions & 0 deletions tests/unit/Codeception/Module/FilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

use Codeception\Module\Filesystem;
use Codeception\Util\Stub;

class FilesystemTest extends \PHPUnit_Framework_TestCase
{

/**
* @var \Codeception\Module\Filesystem
*/
protected $module;

public function setUp()
{
$this->module = new Filesystem(make_container());
$this->module->_before(Stub::makeEmpty('\Codeception\Test\Test'));
}


public function tearDown()
{
$this->module->_after(Stub::makeEmpty('\Codeception\Test\Test'));
}

public function testSeeFileFoundPassesWhenFileExists()
{
$this->module->seeFileFound('tests/data/dumps/mysql.sql');
}

public function testSeeFileFoundPassesWhenFileExistsInSubdirectoryOfPath()
{
$this->module->seeFileFound('mysql.sql', 'tests/data/');
}

/**
* @expectedException PHPUnit_Framework_AssertionFailedError
* @expectedExceptionMessage File "does-not-exist" not found at
*/
public function testSeeFileFoundFailsWhenFileDoesNotExist()
{
$this->module->seeFileFound('does-not-exist');
}

/**
* @expectedException PHPUnit_Framework_AssertionFailedError
* @expectedExceptionMessageRegExp /Directory does not exist: .*does-not-exist/
*/
public function testSeeFileFoundFailsWhenPathDoesNotExist()
{
$this->module->seeFileFound('mysql.sql', 'does-not-exist');
}

public function testDontSeeFileFoundPassesWhenFileDoesNotExists()
{
$this->module->dontSeeFileFound('does-not-exist');
}

public function testDontSeeFileFoundPassesWhenFileDoesNotExistsInPath()
{
$this->module->dontSeeFileFound('does-not-exist', 'tests/data/');
}

/**
* @expectedException PHPUnit_Framework_AssertionFailedError
* @expectedExceptionMessage Failed asserting that file "tests/data/dumps/mysql.sql" does not exist.
*/
public function testDontSeeFileFoundFailsWhenFileExists()
{
$this->module->dontSeeFileFound('tests/data/dumps/mysql.sql');
}

/**
* @expectedException PHPUnit_Framework_AssertionFailedError
* @expectedExceptionMessageRegExp /Directory does not exist: .*does-not-exist/
*/
public function testDontSeeFileFoundFailsWhenPathDoesNotExist()
{
$this->module->dontSeeFileFound('mysql.sql', 'does-not-exist');
}

/**
* @expectedException PHPUnit_Framework_AssertionFailedError
* @expectedExceptionMessageRegExp /Failed asserting that file ".*tests\/data\/dumps\/mysql.sql" does not exist/
*/
public function testDontSeeFileFoundFailsWhenFileExistsInSubdirectoryOfPath()
{
$this->module->dontSeeFileFound('mysql.sql', 'tests/data/');
}
}
4 changes: 2 additions & 2 deletions tests/unit/Codeception/Module/SFTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function setUp()
$this->module = new \Codeception\Module\FTP(make_container());
$this->module->_setConfig($this->config);

$this->module->_before(Stub::make('\Codeception\TestCase'));
$this->module->_before(Stub::makeEmpty('\Codeception\Test\Test'));
}

/**
Expand Down Expand Up @@ -108,6 +108,6 @@ public function flow()

public function tearDown()
{
$this->module->_after();
$this->module->_after(Stub::makeEmpty('\Codeception\Test\Test'));
}
}

0 comments on commit 2aeb8cd

Please sign in to comment.