Skip to content

Commit

Permalink
Merge pull request #24 from Bee-Lab/file-injector
Browse files Browse the repository at this point in the history
♻️  FileInjector
  • Loading branch information
garak authored Oct 14, 2024
2 parents 5fa0379 + f597bbe commit 1ba52f7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ services:
In forms with more than a field of the same type, use `self::getImageFile('1')`, `self::getImageFile('2')`, etc.
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.

* Form values shortcut

If you need to retrieve the value of a form field, you can use `self::getFormValue('form_field')`.
Expand Down
58 changes: 58 additions & 0 deletions src/File/FileInjector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Beelab\TestBundle\File;

use Symfony\Component\HttpFoundation\File\UploadedFile;

final class FileInjector
{
public static function getFile(string $file, string $data, string $ext, string $mime): 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);
}

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

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

public static function getPdfFile(string $file = '0'): UploadedFile
{
$data = <<<'EOF'
JVBERi0xLjEKJcKlwrHDqwoKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAgL1BhZ2VzIDIgMCBSCiAgPj4KZW5kb2JqCgoyIDAgb2JqCiAgP
DwgL1R5cGUgL1BhZ2VzCiAgICAgL0tpZHMgWzMgMCBSXQogICAgIC9Db3VudCAxCiAgICAgL01lZGlhQm94IFswIDAgMzAwIDE0NF0KICA+PgplbmRvYm
oKCjMgMCBvYmoKICA8PCAgL1R5cGUgL1BhZ2UKICAgICAgL1BhcmVudCAyIDAgUgogICAgICAvUmVzb3VyY2VzCiAgICAgICA8PCAvRm9udAogICAgICA
gICAgIDw8IC9GMQogICAgICAgICAgICAgICA8PCAvVHlwZSAvRm9udAogICAgICAgICAgICAgICAgICAvU3VidHlwZSAvVHlwZTEKICAgICAgICAgICAg
ICAgICAgL0Jhc2VGb250IC9UaW1lcy1Sb21hbgogICAgICAgICAgICAgICA+PgogICAgICAgICAgID4+CiAgICAgICA+PgogICAgICAvQ29udGVudHMgN
CAwIFIKICA+PgplbmRvYmoKCjQgMCBvYmoKICA8PCAvTGVuZ3RoIDU1ID4+CnN0cmVhbQogIEJUCiAgICAvRjEgMTggVGYKICAgIDAgMCBUZAogICAgKE
hlbGxvIFdvcmxkKSBUagogIEVUCmVuZHN0cmVhbQplbmRvYmoKCnhyZWYKMCA1CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAxOCAwMDAwMCBuIAo
wMDAwMDAwMDc3IDAwMDAwIG4gCjAwMDAwMDAxNzggMDAwMDAgbiAKMDAwMDAwMDQ1NyAwMDAwMCBuIAp0cmFpbGVyCiAgPDwgIC9Sb290IDEgMCBSCiAg
ICAgIC9TaXplIDUKICA+PgpzdGFydHhyZWYKNTY1CiUlRU9GCg==
EOF;

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

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

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

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

return self::getFile($file, $data, 'zip', 'application/zip');
}
}
38 changes: 7 additions & 31 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Beelab\TestBundle\Test;

use Beelab\TestBundle\File\FileInjector;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Loader;
Expand Down Expand Up @@ -118,52 +119,31 @@ protected static function login(string $username = '[email protected]', ?string
*/
protected static function getImageFile(string $file = '0'): UploadedFile
{
$data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC';

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

/**
* Get a pdf file to be used in a form.
*/
protected static function getPdfFile(string $file = '0'): UploadedFile
{
$data = <<<'EOF'
JVBERi0xLjEKJcKlwrHDqwoKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAgL1BhZ2VzIDIgMCBSCiAgPj4KZW5kb2JqCgoyIDAgb2JqCiAgP
DwgL1R5cGUgL1BhZ2VzCiAgICAgL0tpZHMgWzMgMCBSXQogICAgIC9Db3VudCAxCiAgICAgL01lZGlhQm94IFswIDAgMzAwIDE0NF0KICA+PgplbmRvYm
oKCjMgMCBvYmoKICA8PCAgL1R5cGUgL1BhZ2UKICAgICAgL1BhcmVudCAyIDAgUgogICAgICAvUmVzb3VyY2VzCiAgICAgICA8PCAvRm9udAogICAgICA
gICAgIDw8IC9GMQogICAgICAgICAgICAgICA8PCAvVHlwZSAvRm9udAogICAgICAgICAgICAgICAgICAvU3VidHlwZSAvVHlwZTEKICAgICAgICAgICAg
ICAgICAgL0Jhc2VGb250IC9UaW1lcy1Sb21hbgogICAgICAgICAgICAgICA+PgogICAgICAgICAgID4+CiAgICAgICA+PgogICAgICAvQ29udGVudHMgN
CAwIFIKICA+PgplbmRvYmoKCjQgMCBvYmoKICA8PCAvTGVuZ3RoIDU1ID4+CnN0cmVhbQogIEJUCiAgICAvRjEgMTggVGYKICAgIDAgMCBUZAogICAgKE
hlbGxvIFdvcmxkKSBUagogIEVUCmVuZHN0cmVhbQplbmRvYmoKCnhyZWYKMCA1CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAxOCAwMDAwMCBuIAo
wMDAwMDAwMDc3IDAwMDAwIG4gCjAwMDAwMDAxNzggMDAwMDAgbiAKMDAwMDAwMDQ1NyAwMDAwMCBuIAp0cmFpbGVyCiAgPDwgIC9Sb290IDEgMCBSCiAg
ICAgIC9TaXplIDUKICA+PgpzdGFydHhyZWYKNTY1CiUlRU9GCg==
EOF;

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

/**
* Get a pdf file to be used in a form.
*/
protected static function getZipFile(string $file = '0'): UploadedFile
{
$data = <<<'EOF'
UEsDBAoAAgAAAM5RjEVOGigMAgAAAAIAAAAFABwAaC50eHRVVAkAA/OxilTzsYpUdXgLAAEE6AMAAARkAAAAaApQSwECHgMKAAIAAADOUYxF
ThooDAIAAAACAAAABQAYAAAAAAABAAAApIEAAAAAaC50eHRVVAUAA/OxilR1eAsAAQToAwAABGQAAABQSwUGAAAAAAEAAQBLAAAAQQAAAAAA
EOF;

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

/**
* Get a txt file to be used in a form.
*/
protected static function getTxtFile(string $file = '0'): UploadedFile
{
$data = 'Lorem ipsum dolor sit amet';

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

/**
Expand Down Expand Up @@ -271,15 +251,11 @@ protected function getReference(string $name): object
*/
protected static function getFile(string $file, string $data, string $ext, string $mime): 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 FileInjector::getFile($file, $data, $ext, $mime);
}

/**
* Submit a form that needs extra values (tipically, a form with collections).
* Submit a form that needs extra values (typically, a form with collections).
*
* @param string $name The name of form
* @param array $values The values to submit
Expand Down

0 comments on commit 1ba52f7

Please sign in to comment.