Skip to content

Commit

Permalink
Merge pull request #27 from Bee-Lab/fix-injector
Browse files Browse the repository at this point in the history
🩹 add missing arguments to FileInjector
  • Loading branch information
garak authored Oct 17, 2024
2 parents 2ecaddb + b9ba874 commit a0a815b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ services:
You can also use `self::getFile('0', $data, 'png', 'image/png')` and pass directly your file data.

💡 ** Bonus** The above methods are shortcuts for the `Beelab\TestBundle\File\FileInjector` class, so you
can use them in your fixtures, too. Import the class and use `FileInjector::getFile()` etc.
can use them in your fixtures, too. Import the class and use `FileInjector::getFile(fixture: true)` etc.
Don't forget to pass the `$fixture` argument.

* Form values shortcut

Expand Down
20 changes: 10 additions & 10 deletions src/File/FileInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

final class FileInjector
{
public static function getFile(string $file, string $data, string $ext, string $mime): UploadedFile
public static function getFile(string $file, string $data, string $ext, string $mime, bool $fixture = false): UploadedFile
{
$name = 'file_'.$file.'.'.$ext;
$path = \tempnam(\sys_get_temp_dir(), 'sf_test_').$name;
\file_put_contents($path, \str_starts_with($mime, 'text') ? $data : \base64_decode($data));

return new UploadedFile($path, $name, $mime);
return new UploadedFile($path, $name, $mime, null, $fixture);
}

public static function getImageFile(string $file = '0'): UploadedFile
public static function getImageFile(string $file = '0', bool $fixture = false): UploadedFile
{
$data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC';

return self::getFile($file, $data, 'png', 'image/png');
return self::getFile($file, $data, 'png', 'image/png', $fixture);
}

public static function getPdfFile(string $file = '0'): UploadedFile
public static function getPdfFile(string $file = '0', bool $fixture = false): UploadedFile
{
$data = <<<'EOF'
JVBERi0xLjEKJcKlwrHDqwoKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAgL1BhZ2VzIDIgMCBSCiAgPj4KZW5kb2JqCgoyIDAgb2JqCiAgP
Expand All @@ -36,23 +36,23 @@ public static function getPdfFile(string $file = '0'): UploadedFile
ICAgIC9TaXplIDUKICA+PgpzdGFydHhyZWYKNTY1CiUlRU9GCg==
EOF;

return self::getFile($file, $data, 'pdf', 'application/pdf');
return self::getFile($file, $data, 'pdf', 'application/pdf', $fixture);
}

public static function getTxtFile(string $file = '0'): UploadedFile
public static function getTxtFile(string $file = '0', bool $fixture = false): UploadedFile
{
$data = 'Lorem ipsum dolor sit amet';

return self::getFile($file, $data, 'txt', 'text/plain');
return self::getFile($file, $data, 'txt', 'text/plain', $fixture);
}

public static function getZipFile(string $file = '0'): UploadedFile
public static function getZipFile(string $file = '0', bool $fixture = false): UploadedFile
{
$data = <<<'EOF'
UEsDBAoAAgAAAM5RjEVOGigMAgAAAAIAAAAFABwAaC50eHRVVAkAA/OxilTzsYpUdXgLAAEE6AMAAARkAAAAaApQSwECHgMKAAIAAADOUYxF
ThooDAIAAAACAAAABQAYAAAAAAABAAAApIEAAAAAaC50eHRVVAUAA/OxilR1eAsAAQToAwAABGQAAABQSwUGAAAAAAEAAQBLAAAAQQAAAAAA
EOF;

return self::getFile($file, $data, 'zip', 'application/zip');
return self::getFile($file, $data, 'zip', 'application/zip', $fixture);
}
}

0 comments on commit a0a815b

Please sign in to comment.