Skip to content

Commit 00d865f

Browse files
authored
Merge pull request #11 from samsonasik/test-display2
Continue add more tests to DisplayTest
2 parents a610a55 + d50acad commit 00d865f

5 files changed

Lines changed: 95 additions & 5 deletions

File tree

src/Entities/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected static function locateDefaultThumbnail(): string
4141
}
4242
}
4343

44-
return self::$defaultThumbnail;
44+
return (string) self::$defaultThumbnail;
4545
}
4646

4747
//--------------------------------------------------------------------

src/Views/Formats/cards.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="card-deck">
55
<?php foreach ($files as $file): ?>
66
<div class="card mb-4" style="min-width: 10rem; max-width: 200px;">
7-
<img src="<?= img_data($file->thumbnail) ?>" class="card-img-top img-thumbnail" alt="<?= $file->filename ?>">
7+
<img src="<?= img_data($file->getThumbnail()) ?>" class="card-img-top img-thumbnail" alt="<?= $file->filename ?>">
88
<div class="card-header">
99
<?= view('Tatter\Files\Views\Menus\single', ['file' => $file, 'access' => $access]) ?>
1010
</div>

src/Views/Formats/list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<tbody>
1616
<?php foreach ($files as $file): ?>
1717
<tr>
18-
<td><img src="<?= img_data($file->thumbnail) ?>" class="img-fluid rounded" alt="<?= $file->filename ?>" style="max-height:40px;"></td>
18+
<td><img src="<?= img_data($file->getThumbnail()) ?>" class="img-fluid rounded" alt="<?= $file->filename ?>" style="max-height:40px;"></td>
1919
<td class="align-middle"><?= $file->filename ?></td>
2020
<td class="align-middle"><?= $file->type ?></td>
2121
<td class="align-middle"><?= bytes2human($file->size) ?></td>

tests/feature/DisplayTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@ public function testDefaultDisplaysCards()
2626
$result->assertSee($file->filename);
2727
}
2828

29+
public function testDataUsesSettings()
30+
{
31+
service('settings')->filesSort = 'type';
32+
service('settings')->filesOrder = 'asc';
33+
service('settings')->filesFormat = 'cards';
34+
35+
$file = fake(FileFaker::class);
36+
$result = $this->get('files');
37+
$result->assertStatus(200);
38+
$result->assertSee($file->filename);
39+
}
40+
41+
public function provideFormat()
42+
{
43+
yield ['cards', 'cards'];
44+
yield ['list', 'list'];
45+
yield ['select', 'select'];
46+
yield ['invalid', config('Files')->defaultFormat];
47+
}
48+
49+
/**
50+
* @dataProvider provideFormat
51+
*/
52+
53+
public function testFormat(string $format, string $configFormat)
54+
{
55+
$_REQUEST['format'] = $format;
56+
57+
$file = fake(FileFaker::class);
58+
$result = $this->get('files');
59+
60+
$result->assertStatus(200);
61+
$this->assertEquals($configFormat, service('settings')->filesFormat);
62+
}
63+
2964
public function provideSort()
3065
{
3166
yield ['filename', 'filename'];
@@ -75,6 +110,7 @@ public function testOrders(string $order, string $configOrder)
75110
public function provideSearch()
76111
{
77112
yield ['Heathcote'];
113+
yield ['will never be found'];
78114
}
79115

80116
/**
@@ -90,9 +126,12 @@ public function testSearches(string $keyword)
90126
$result->assertStatus(200);
91127
$content = $result->response->getBody();
92128

93-
if (strpos($content, $keyword) !== false) {
129+
if (strpos($content, $keyword) !== false)
130+
{
94131
$result->assertSee($keyword);
95-
} else {
132+
}
133+
else
134+
{
96135
$result->assertSee('You have no files!');
97136
}
98137
}

tests/unit/ControllerTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

33
use CodeIgniter\Config\Config;
4+
use CodeIgniter\Files\Exceptions\FileNotFoundException;
45
use Tatter\Files\Controllers\Files;
6+
use Tatter\Files\Entities\File;
57
use Tatter\Files\Exceptions\FilesException;
8+
use Tests\Support\Fakers\FileFaker;
69
use Tests\Support\FilesTestCase;
710
use Tests\Support\Models\FileModel;
811

@@ -177,4 +180,52 @@ public function testGetFormatIgnoresInvalid()
177180

178181
$this->assertEquals('cards', $result);
179182
}
183+
184+
public function testDataUsesVarWithFaker()
185+
{
186+
$file = fake(FileFaker::class);
187+
188+
$controller = new Files();
189+
$controller->initController(service('request'), service('response'), service('logger'));
190+
191+
$method = $this->getPrivateMethodInvoker($controller, 'setData');
192+
$method([
193+
'files' => [
194+
0 => $file
195+
],
196+
]);
197+
198+
$result = $controller->display();
199+
$this->assertStringContainsString($file->filename, $result);
200+
}
201+
202+
public function testDataUsesVarViaPassEntity()
203+
{
204+
$controller = new Files();
205+
$controller->initController(service('request'), service('response'), service('logger'));
206+
207+
$file = new File;
208+
$file->filename ='foo.txt';
209+
$file->thumbnail = '';
210+
$file->type = '';
211+
$file->localname = '';
212+
$file->clientname = '';
213+
$file->size = 1;
214+
$file->created_at = new class {
215+
public function humanize()
216+
{
217+
return '';
218+
}
219+
};
220+
221+
$method = $this->getPrivateMethodInvoker($controller, 'setData');
222+
$method([
223+
'files' => [
224+
0 => $file
225+
],
226+
]);
227+
228+
$result = $controller->display();
229+
$this->assertStringContainsString($file->filename, $result);
230+
}
180231
}

0 commit comments

Comments
 (0)