Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/Filesystem/Test/Node/FileAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/*
* This file is part of the zenstruck/filesystem package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Filesystem\Test\Node;

use League\Flysystem\UnableToGeneratePublicUrl;
use Zenstruck\Assert;
use Zenstruck\Filesystem\Node\File\Image;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait FileAssertions
{
public function assertContentIs(string $expected): self
{
Assert::that($this->contents())->is($expected);

return $this;
}

public function assertContentIsNot(string $expected): self
{
Assert::that($this->contents())->isNot($expected);

return $this;
}

public function assertContentContains(string $expected): self
{
Assert::that($this->contents())->contains($expected);

return $this;
}

public function assertContentDoesNotContain(string $expected): self
{
Assert::that($this->contents())->doesNotContain($expected);

return $this;
}

public function assertMimeTypeIs(string $expected): self
{
Assert::that($this->mimeType())->is($expected);

return $this;
}

public function assertMimeTypeIsNot(string $expected): self
{
Assert::that($this->mimeType())->isNot($expected);

return $this;
}

public function assertSize(int $expected): self
{
Assert::that($this->size())->is($expected);

return $this;
}

public function assertChecksum(string $expected): self
{
Assert::that($this->checksum())->is($expected);

return $this;
}

public function dump(): static
{
$what = [
'path' => (string) $this->path(),
'mimeType' => $this->mimeType(),
'lastModified' => $this->lastModified(),
'size' => $this->size(),
];

try {
$what['public_url'] = $this->publicUrl();
} catch (UnableToGeneratePublicUrl) {
}

if ($this instanceof Image) {
$what['image']['height'] = $this->dimensions()->height();
$what['image']['width'] = $this->dimensions()->width();
}

\function_exists('dump') ? dump($what) : \var_dump($what);

return $this;
}
}
11 changes: 1 addition & 10 deletions src/Filesystem/Test/Node/TestDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function assertCount(int $expected): self
return $this;
}

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

Expand All @@ -42,15 +42,6 @@ public function dump(): self
return $this;
}

/**
* @return no-return
*/
public function dd(): void
{
$this->dump();
exit(1);
}

protected function inner(): Directory
{
return $this->inner;
Expand Down
85 changes: 1 addition & 84 deletions src/Filesystem/Test/Node/TestFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,103 +11,20 @@

namespace Zenstruck\Filesystem\Test\Node;

use League\Flysystem\UnableToGeneratePublicUrl;
use Zenstruck\Assert;
use Zenstruck\Filesystem\Node\File;
use Zenstruck\Filesystem\Node\File\DecoratedFile;
use Zenstruck\Filesystem\Node\File\Image;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
class TestFile extends TestNode implements File
{
use DecoratedFile;
use DecoratedFile, FileAssertions;

public function __construct(private File $inner)
{
}

public function assertContentIs(string $expected): self
{
Assert::that($this->contents())->is($expected);

return $this;
}

public function assertContentIsNot(string $expected): self
{
Assert::that($this->contents())->isNot($expected);

return $this;
}

public function assertContentContains(string $expected): self
{
Assert::that($this->contents())->contains($expected);

return $this;
}

public function assertContentDoesNotContain(string $expected): self
{
Assert::that($this->contents())->doesNotContain($expected);

return $this;
}

public function assertMimeTypeIs(string $expected): self
{
Assert::that($this->mimeType())->is($expected);

return $this;
}

public function assertMimeTypeIsNot(string $expected): self
{
Assert::that($this->mimeType())->isNot($expected);

return $this;
}

public function assertSize(int $expected): self
{
Assert::that($this->size())->is($expected);

return $this;
}

public function assertChecksum(string $expected): self
{
Assert::that($this->checksum())->is($expected);

return $this;
}

public function dump(): self
{
$what = [
'path' => (string) $this->path(),
'mimeType' => $this->mimeType(),
'lastModified' => $this->lastModified(),
'size' => $this->size(),
];

try {
$what['public_url'] = $this->publicUrl();
} catch (UnableToGeneratePublicUrl) {
}

if ($this instanceof Image) {
$what['image']['height'] = $this->dimensions()->height();
$what['image']['width'] = $this->dimensions()->width();
}

\function_exists('dump') ? dump($what) : \var_dump($what);

return $this;
}

protected function inner(): File
{
return $this->inner;
Expand Down
2 changes: 1 addition & 1 deletion src/Filesystem/Test/Node/TestImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
final class TestImage extends TestNode implements Image
{
use DecoratedFile, DecoratedImage;
use DecoratedFile, DecoratedImage, FileAssertions;

public function __construct(private Image $inner)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Filesystem/Test/Node/TestNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public function assertLastModified(string|\DateTimeInterface|callable $expected)
return $this;
}

abstract public function dump(): static;

/**
* @return no-return
*/
public function dd(): void
{
$this->dump();
exit(1);
}

protected function inner(): Node
{
return $this->inner;
Expand Down
3 changes: 3 additions & 0 deletions tests/Filesystem/Test/TestFilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function can_make_assertions(): void
->assertContentContains('1')
->assertContentDoesNotContain('foo')
->assertMimeTypeIs('text/plain')
->assertSize(9)
->assertMimeTypeIsNot('foo')
->assertLastModified(function(\DateTimeInterface $actual) {
$this->assertTrue($actual->getTimestamp() > 0);
Expand All @@ -71,6 +72,8 @@ public function can_make_assertions(): void
})
->assertImageExists('symfony.png', function(TestImage $image) {
$image
->assertMimeTypeIs('image/png')
->assertSize(10862)
->assertHeight(678)
->assertWidth(563)
;
Expand Down
Loading