From 9b587a87c0b7325bf13b93b2f6e639a980d461ea Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 8 Jan 2022 16:12:25 -0600 Subject: [PATCH 1/2] Tests: add test for connecting to non-existant host --- tests/Functional/Net/SFTPUserStoryTest.php | 8 ++++++++ tests/PhpseclibTestCase.php | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index c155c627..c9235a4b 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -23,6 +23,14 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase self::$exampleDataLength = 10000; } + public function testBadHostname() + { + $this->setExpectedException('PHPUnit_Framework_Error_Notice'); + + $sftp = new Net_SFTP('bad host name'); + $sftp->login('username', 'password'); + } + public function testConstructor() { $sftp = new Net_SFTP($this->getEnv('SSH_HOSTNAME')); diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index ca68596b..f13709fc 100644 --- a/tests/PhpseclibTestCase.php +++ b/tests/PhpseclibTestCase.php @@ -145,4 +145,24 @@ abstract class PhpseclibTestCase extends PHPUnit\Framework\TestCase parent::assertNotContains($needle, $haystack, $message); } + + public function setExpectedException($name, $message = null, $code = null) + { + if (version_compare(PHP_VERSION, '7.0.0') < 0) { + parent::setExpectedException($name, $message, $code); + return; + } + switch ($name) { + case 'PHPUnit_Framework_Error_Notice': + case 'PHPUnit_Framework_Error_Warning': + $name = str_replace('_', '\\', $name); + } + $this->expectException($name); + if (!empty($message)) { + $this->expectExceptionMessage($message); + } + if (!empty($code)) { + $this->expectExceptionCode($code); + } + } } From bc457521f1cdfe880eb5252470b40e6334baac6e Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 8 Jan 2022 16:36:47 -0600 Subject: [PATCH 2/2] Tests: update for 2.0 branch --- tests/Functional/Net/SFTPUserStoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index e7d81f7c..6da4ddcc 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -29,7 +29,7 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase { $this->setExpectedException('PHPUnit_Framework_Error_Notice'); - $sftp = new Net_SFTP('bad host name'); + $sftp = new SFTP('bad host name'); $sftp->login('username', 'password'); }