Tests: introduce dedicated tests for the `PHPMailer::fileIsAccessible()` method
So far, this method did not have dedicated tests. The test file this commit introduces, tests nearly all aspects of the method as well as documents the current behaviour of the method. There is one particular test case missing. This is annotated in the class docblock. While this method is quite simple, testing it separately means that the tests for methods _using_ this method don't have to _also_ test the functioning of this method, which means they can be more focussed on their own logic.
This commit is contained in:
parent
e93f027b1f
commit
49bcd4a035
|
|
@ -0,0 +1 @@
|
|||
Testing 1-2-3
|
||||
|
|
@ -0,0 +1 @@
|
|||
Testing 1-2-3
|
||||
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPMailer - PHP email transport unit tests.
|
||||
* PHP version 5.5.
|
||||
*
|
||||
* @author Marcus Bointon <phpmailer@synchromedia.co.uk>
|
||||
* @author Andy Prevost
|
||||
* @copyright 2012 - 2020 Marcus Bointon
|
||||
* @copyright 2004 - 2009 Andy Prevost
|
||||
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
|
||||
*/
|
||||
|
||||
namespace PHPMailer\Test\PHPMailer;
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use ReflectionMethod;
|
||||
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
|
||||
|
||||
/**
|
||||
* Test file accessibility verification functionality.
|
||||
*
|
||||
* {@internal There is one test case known to be missing, which is a test
|
||||
* with a valid UNC path. If someone can figure out a way to add a test for
|
||||
* that case, that would be awesome!}
|
||||
*
|
||||
* @covers \PHPMailer\PHPMailer\PHPMailer::fileIsAccessible
|
||||
*/
|
||||
final class FileIsAccessibleTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Verify whether the "is a file accessible" check works correctly.
|
||||
*
|
||||
* @dataProvider dataFileIsAccessible
|
||||
*
|
||||
* @param string $input A relative or absolute path to a file.
|
||||
* @param bool $expected The expected function return value.
|
||||
*/
|
||||
public function testFileIsAccessible($input, $expected)
|
||||
{
|
||||
$reflMethod = new ReflectionMethod(PHPMailer::class, 'fileIsAccessible');
|
||||
$reflMethod->setAccessible(true);
|
||||
$result = $reflMethod->invoke(null, $input);
|
||||
$reflMethod->setAccessible(false);
|
||||
|
||||
self::assertSame($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function dataFileIsAccessible()
|
||||
{
|
||||
$fixturesPath = dirname(__DIR__) . '/Fixtures/FileIsAccessibleTest/';
|
||||
|
||||
return [
|
||||
'Valid: accessible file' => [
|
||||
'input' => $fixturesPath . 'accessible.txt',
|
||||
'expected' => true,
|
||||
],
|
||||
'Invalid: path not permitted' => [
|
||||
'input' => 'https://github.com/PHPMailer/PHPMailer/',
|
||||
'expected' => false,
|
||||
],
|
||||
'Invalid: file does not exist' => [
|
||||
'input' => $fixturesPath . 'thisfiledoesnotexist.txt',
|
||||
'expected' => false,
|
||||
],
|
||||
'Invalid: file in UNC path does not exist' => [
|
||||
'input' => '\\\\nowhere\nothing',
|
||||
'expected' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the "is a file accessible" check correctly fails when the file permissions make
|
||||
* the file unreadable.
|
||||
*/
|
||||
public function testFileIsAccessibleFailsOnUnreadableFile()
|
||||
{
|
||||
if (\DIRECTORY_SEPARATOR === '\\') {
|
||||
// Windows does not respect chmod permissions.
|
||||
$this->markTestSkipped('This test requires a non-Windows OS.');
|
||||
}
|
||||
|
||||
$path = dirname(__DIR__) . '/Fixtures/FileIsAccessibleTest/';
|
||||
$file = $path . 'inaccessible.txt';
|
||||
chmod($file, octdec('0'));
|
||||
|
||||
$reflMethod = new ReflectionMethod(PHPMailer::class, 'fileIsAccessible');
|
||||
$reflMethod->setAccessible(true);
|
||||
$result = $reflMethod->invoke(null, $file);
|
||||
$reflMethod->setAccessible(false);
|
||||
|
||||
// Reset to the default for git files before running assertions.
|
||||
chmod($file, octdec('644'));
|
||||
|
||||
self::assertFalse($result);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue