You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It works but there's still room for improvement. The two alternatives I've tried (Nyholm\Psr7 and Laminas\Diactoros) have scant documentation for this purpose or the specific classes. The code suggests it won't serve my purpose. A modified version of saveFakeImage will look like this:
protectedfunctionsaveFakeImage (string$fileName, int$width, int$height, int$expectedSize = 100):UploadedFile {
$stream = $this->getImageStream($width, $height);
while ($stream->getSize() *1024 < $expectedSize)
$stream->write($this->getImageStream($width, $height));
returnUploadedFileFactory::createUploadedFile($stream, $expectedSize, 0, $fileName);
}
privatefunctiongetImageStream (int$width, int$height):StreamInterface {
$imageResource = imagecreatetruecolor($width, $height);
$stream = StreamFactory::createStream($imageResource); // this is the line of interest
imagedestroy($imageResource);
return$stream;
}
On the line of interest, Laminas\Diactoros creates stream in read only mode. This prevents saveFakeImage from writing to it. On the other hand, Nyholm\Psr7 is gracious enough to open stream in read/write mode. Sadly, it clears stream size after it's being written to, preventing incremental addition by saveFakeImage
Aside the fact that this looks cleaner to the present alternative, there's no creating and deletion of dummy images written to disk, meaning it'll perform better. It also means we won't need the extra Symfony bridge to create UploadedFileInterface instances applicable in both test and live environments
The text was updated successfully, but these errors were encountered:
nmeri17
changed the title
Replace current test image creation with more elegant solution
Replace current test image creation with more elegant PSR compliant solution
Jul 9, 2022
This refers to the methods covered here: https://github.com/nmeri17/Tilwa/blob/1a2afe17993353473dfa5805b639928b261c31df/nmeri/tilwa/src/Testing/Condiments/FilesystemCleaner.php#L136-L177
It works but there's still room for improvement. The two alternatives I've tried (Nyholm\Psr7 and Laminas\Diactoros) have scant documentation for this purpose or the specific classes. The code suggests it won't serve my purpose. A modified version of
saveFakeImage
will look like this:On the line of interest, Laminas\Diactoros creates stream in read only mode. This prevents
saveFakeImage
from writing to it. On the other hand, Nyholm\Psr7 is gracious enough to open stream in read/write mode. Sadly, it clears stream size after it's being written to, preventing incremental addition bysaveFakeImage
Aside the fact that this looks cleaner to the present alternative, there's no creating and deletion of dummy images written to disk, meaning it'll perform better. It also means we won't need the extra Symfony bridge to create UploadedFileInterface instances applicable in both test and live environments
The text was updated successfully, but these errors were encountered: