Skip to content

Commit f0d548f

Browse files
authored
Merge pull request #119 from kbond/test-image-file
bug(test): give `TestImage` the same assertions as `TestFile`
1 parent 8e41ddb commit f0d548f

File tree

6 files changed

+119
-95
lines changed

6 files changed

+119
-95
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the zenstruck/filesystem package.
5+
*
6+
* (c) Kevin Bond <kevinbond@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Zenstruck\Filesystem\Test\Node;
13+
14+
use League\Flysystem\UnableToGeneratePublicUrl;
15+
use Zenstruck\Assert;
16+
use Zenstruck\Filesystem\Node\File\Image;
17+
18+
/**
19+
* @author Kevin Bond <kevinbond@gmail.com>
20+
*/
21+
trait FileAssertions
22+
{
23+
public function assertContentIs(string $expected): self
24+
{
25+
Assert::that($this->contents())->is($expected);
26+
27+
return $this;
28+
}
29+
30+
public function assertContentIsNot(string $expected): self
31+
{
32+
Assert::that($this->contents())->isNot($expected);
33+
34+
return $this;
35+
}
36+
37+
public function assertContentContains(string $expected): self
38+
{
39+
Assert::that($this->contents())->contains($expected);
40+
41+
return $this;
42+
}
43+
44+
public function assertContentDoesNotContain(string $expected): self
45+
{
46+
Assert::that($this->contents())->doesNotContain($expected);
47+
48+
return $this;
49+
}
50+
51+
public function assertMimeTypeIs(string $expected): self
52+
{
53+
Assert::that($this->mimeType())->is($expected);
54+
55+
return $this;
56+
}
57+
58+
public function assertMimeTypeIsNot(string $expected): self
59+
{
60+
Assert::that($this->mimeType())->isNot($expected);
61+
62+
return $this;
63+
}
64+
65+
public function assertSize(int $expected): self
66+
{
67+
Assert::that($this->size())->is($expected);
68+
69+
return $this;
70+
}
71+
72+
public function assertChecksum(string $expected): self
73+
{
74+
Assert::that($this->checksum())->is($expected);
75+
76+
return $this;
77+
}
78+
79+
public function dump(): static
80+
{
81+
$what = [
82+
'path' => (string) $this->path(),
83+
'mimeType' => $this->mimeType(),
84+
'lastModified' => $this->lastModified(),
85+
'size' => $this->size(),
86+
];
87+
88+
try {
89+
$what['public_url'] = $this->publicUrl();
90+
} catch (UnableToGeneratePublicUrl) {
91+
}
92+
93+
if ($this instanceof Image) {
94+
$what['image']['height'] = $this->dimensions()->height();
95+
$what['image']['width'] = $this->dimensions()->width();
96+
}
97+
98+
\function_exists('dump') ? dump($what) : \var_dump($what);
99+
100+
return $this;
101+
}
102+
}

src/Filesystem/Test/Node/TestDirectory.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function assertCount(int $expected): self
3333
return $this;
3434
}
3535

36-
public function dump(): self
36+
public function dump(): static
3737
{
3838
$files = \array_map(static fn($d) => (string) $d->path(), \iterator_to_array($this));
3939

@@ -42,15 +42,6 @@ public function dump(): self
4242
return $this;
4343
}
4444

45-
/**
46-
* @return no-return
47-
*/
48-
public function dd(): void
49-
{
50-
$this->dump();
51-
exit(1);
52-
}
53-
5445
protected function inner(): Directory
5546
{
5647
return $this->inner;

src/Filesystem/Test/Node/TestFile.php

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,103 +11,20 @@
1111

1212
namespace Zenstruck\Filesystem\Test\Node;
1313

14-
use League\Flysystem\UnableToGeneratePublicUrl;
15-
use Zenstruck\Assert;
1614
use Zenstruck\Filesystem\Node\File;
1715
use Zenstruck\Filesystem\Node\File\DecoratedFile;
18-
use Zenstruck\Filesystem\Node\File\Image;
1916

2017
/**
2118
* @author Kevin Bond <kevinbond@gmail.com>
2219
*/
2320
class TestFile extends TestNode implements File
2421
{
25-
use DecoratedFile;
22+
use DecoratedFile, FileAssertions;
2623

2724
public function __construct(private File $inner)
2825
{
2926
}
3027

31-
public function assertContentIs(string $expected): self
32-
{
33-
Assert::that($this->contents())->is($expected);
34-
35-
return $this;
36-
}
37-
38-
public function assertContentIsNot(string $expected): self
39-
{
40-
Assert::that($this->contents())->isNot($expected);
41-
42-
return $this;
43-
}
44-
45-
public function assertContentContains(string $expected): self
46-
{
47-
Assert::that($this->contents())->contains($expected);
48-
49-
return $this;
50-
}
51-
52-
public function assertContentDoesNotContain(string $expected): self
53-
{
54-
Assert::that($this->contents())->doesNotContain($expected);
55-
56-
return $this;
57-
}
58-
59-
public function assertMimeTypeIs(string $expected): self
60-
{
61-
Assert::that($this->mimeType())->is($expected);
62-
63-
return $this;
64-
}
65-
66-
public function assertMimeTypeIsNot(string $expected): self
67-
{
68-
Assert::that($this->mimeType())->isNot($expected);
69-
70-
return $this;
71-
}
72-
73-
public function assertSize(int $expected): self
74-
{
75-
Assert::that($this->size())->is($expected);
76-
77-
return $this;
78-
}
79-
80-
public function assertChecksum(string $expected): self
81-
{
82-
Assert::that($this->checksum())->is($expected);
83-
84-
return $this;
85-
}
86-
87-
public function dump(): self
88-
{
89-
$what = [
90-
'path' => (string) $this->path(),
91-
'mimeType' => $this->mimeType(),
92-
'lastModified' => $this->lastModified(),
93-
'size' => $this->size(),
94-
];
95-
96-
try {
97-
$what['public_url'] = $this->publicUrl();
98-
} catch (UnableToGeneratePublicUrl) {
99-
}
100-
101-
if ($this instanceof Image) {
102-
$what['image']['height'] = $this->dimensions()->height();
103-
$what['image']['width'] = $this->dimensions()->width();
104-
}
105-
106-
\function_exists('dump') ? dump($what) : \var_dump($what);
107-
108-
return $this;
109-
}
110-
11128
protected function inner(): File
11229
{
11330
return $this->inner;

src/Filesystem/Test/Node/TestImage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
final class TestImage extends TestNode implements Image
2424
{
25-
use DecoratedFile, DecoratedImage;
25+
use DecoratedFile, DecoratedImage, FileAssertions;
2626

2727
public function __construct(private Image $inner)
2828
{

src/Filesystem/Test/Node/TestNode.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ public function assertLastModified(string|\DateTimeInterface|callable $expected)
8686
return $this;
8787
}
8888

89+
abstract public function dump(): static;
90+
91+
/**
92+
* @return no-return
93+
*/
94+
public function dd(): void
95+
{
96+
$this->dump();
97+
exit(1);
98+
}
99+
89100
protected function inner(): Node
90101
{
91102
return $this->inner;

tests/Filesystem/Test/TestFilesystemTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function can_make_assertions(): void
6262
->assertContentContains('1')
6363
->assertContentDoesNotContain('foo')
6464
->assertMimeTypeIs('text/plain')
65+
->assertSize(9)
6566
->assertMimeTypeIsNot('foo')
6667
->assertLastModified(function(\DateTimeInterface $actual) {
6768
$this->assertTrue($actual->getTimestamp() > 0);
@@ -71,6 +72,8 @@ public function can_make_assertions(): void
7172
})
7273
->assertImageExists('symfony.png', function(TestImage $image) {
7374
$image
75+
->assertMimeTypeIs('image/png')
76+
->assertSize(10862)
7477
->assertHeight(678)
7578
->assertWidth(563)
7679
;

0 commit comments

Comments
 (0)