Summer Sale! All accounts are 50% off this week.

devhobby's avatar

Laravel ftp testing error

Hi all. I am using Storage for SFTP connection. Now I want to close the connection after connecting it every time. For example, this is my getFiles() method from a class.

$connection = Storage::disk('sftp');
$files = $connection->get('some-directory');
$connection->getDriver()->getAdapter()->disconnect(); 

Everything is working fine.

But my test fails for this behavior. This is my test.

    public function test_get_all_files_from_ftp()
    {
        Storage::fake('sftp');

        // Act
        $files = (new FtpConnector())->getFiles('some-directory');

        // Assert
        $this->assertEquals(x, count($files));
    }

Thanks in advanced.

0 likes
4 replies
devhobby's avatar

Sorry. This is the error:

Error: Call to undefined method League\Flysystem\Adapter\Local::disconnect()

2 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

@devhobby It's because you are faking sftp (Storage::fake('sftp');) which under the hood use the local driver. Therefore you are getting this error.

You can use this:

$adapter = $connection->getDriver()->getAdapter();

if (method_exists($adapter, 'disconnect')) {
    $adapter->disconnect(); 
}
2 likes

Please or to participate in this conversation.