Skip to content

Commit a0bc060

Browse files
committed
Fix ftp testing
1 parent bfcc7b8 commit a0bc060

4 files changed

Lines changed: 35 additions & 24 deletions

File tree

phpunit.dist.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
<env name="FTP_PASSWORD" value="password"/>
1414
<env name="FTP_USERNAME" value="username"/>
1515
<env name="FTP_PORT" value="21"/>
16-
<env name="FTP_ROOT" value="."/>
16+
<env name="FTP_ROOT" value="/tmp"/>
1717
</php>
1818
</phpunit>

src/Storage/Service/FTPService.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private function __construct(array $config)
9494
{
9595
$this->validateConfiguration($config);
9696
$this->config = $this->normalizeConfiguration($config);
97-
$this->use_passive_mode = (bool)($this->config[self::CONFIG_PASSIVE] ?? self::DEFAULT_PASSIVE);
97+
$this->use_passive_mode = (bool) ($this->config[self::CONFIG_PASSIVE] ?? self::DEFAULT_PASSIVE);
9898

9999
$this->connect();
100100
}
@@ -147,9 +147,9 @@ public function connect(): void
147147
}
148148

149149
$host = $this->config[self::CONFIG_HOSTNAME];
150-
$port = (int)$this->config[self::CONFIG_PORT];
151-
$timeout = (int)$this->config[self::CONFIG_TIMEOUT];
152-
$use_tls = (bool)$this->config[self::CONFIG_TLS];
150+
$port = (int) $this->config[self::CONFIG_PORT];
151+
$timeout = (int) $this->config[self::CONFIG_TIMEOUT];
152+
$use_tls = (bool) $this->config[self::CONFIG_TLS];
153153

154154
$connection = $this->attemptConnection($host, $port, $timeout, $use_tls);
155155

@@ -171,7 +171,6 @@ public function connect(): void
171171
$this->login();
172172
$this->changePath();
173173
$this->activePassiveMode();
174-
$this->is_connected = true;
175174
} catch (RuntimeException $e) {
176175
$this->disconnect();
177176
throw $e;
@@ -251,7 +250,6 @@ public function disconnect(): void
251250
{
252251
if ($this->connection !== null) {
253252
@ftp_close($this->connection);
254-
$this->is_connected = false;
255253
}
256254
}
257255

@@ -283,7 +281,7 @@ public function changePath(?string $path = null): void
283281
*/
284282
private function ensureConnection(): void
285283
{
286-
if (!$this->is_connected || $this->connection === null) {
284+
if ($this->connection === null) {
287285
throw new RuntimeException('FTP connection is not established');
288286
}
289287
}

src/Storage/Storage.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static function configure(array $config): FilesystemInterface
9999
static::$config = $config;
100100

101101
if (is_null(static::$disk)) {
102-
static::$disk = static::disk($config['disk']['mount']);
102+
static::$disk = static::local($config['disk']['mount']);
103103
}
104104

105105
return static::$disk;
@@ -130,12 +130,26 @@ public static function local(?string $disk = null): DiskFilesystemService
130130

131131
$config = static::$config['disk']['path'][$disk];
132132

133+
if (is_null($config)) {
134+
throw new DiskNotFoundException('The ' . $disk . ' disk is not define.');
135+
}
136+
137+
if (!is_dir($config)) {
138+
// Try to create the directory
139+
if (!mkdir($config, 0755, true)) {
140+
throw new DiskNotFoundException('The ' . $disk . ' disk directory does not exist.');
141+
}
142+
}
143+
133144
return static::$disk = new DiskFilesystemService($config);
134145
}
135146

136147
/**
137148
* Mount disk
138-
* @deprecated version
149+
*
150+
* @param string|null $disk
151+
* @return DiskFilesystemService
152+
* @throws DiskNotFoundException
139153
*/
140154
public static function disk(?string $disk = null): DiskFilesystemService
141155
{

tests/Filesystem/FTPServiceTest.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public static function setUpBeforeClass(): void
2020
Storage::configure($config["storage"]);
2121
}
2222

23+
protected function setUp(): void
24+
{
25+
$this->ftp_service = Storage::service('ftp');
26+
}
27+
28+
protected function tearDown(): void
29+
{
30+
$this->ftp_service->changePath();
31+
}
32+
2333
public function test_the_connection()
2434
{
2535
$this->assertInstanceOf(FTPService::class, $this->ftp_service);
@@ -59,8 +69,8 @@ private function createFile(FTPService $ftp_service, $filename, $content = ''):
5969

6070
public function test_file_should_not_be_existe()
6171
{
62-
$this->expectException(\Bow\Storage\Exception\ResourceException::class);
63-
$this->ftp_service->get('dummy.txt');
72+
$this->expectException(\InvalidArgumentException::class);
73+
$this->ftp_service->get('');
6474
}
6575

6676
public function test_create_the_new_file_and_the_content()
@@ -77,8 +87,7 @@ public function test_delete_file_from_ftp_service()
7787
$result = $this->ftp_service->delete($file_name);
7888

7989
$this->assertTrue($result);
80-
$this->expectException(\Bow\Storage\Exception\ResourceException::class);
81-
$this->ftp_service->get($file_name);
90+
$this->assertEmpty($this->ftp_service->get($file_name));
8291
}
8392

8493
public function test_rename_file()
@@ -180,14 +189,4 @@ public function test_put_content_into_file()
180189

181190
$this->assertTrue(true);
182191
}
183-
184-
protected function setUp(): void
185-
{
186-
$this->ftp_service = Storage::service('ftp');
187-
}
188-
189-
protected function tearDown(): void
190-
{
191-
$this->ftp_service->changePath();
192-
}
193192
}

0 commit comments

Comments
 (0)