Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace current test image creation with more elegant PSR compliant solution #26

Open
nmeri17 opened this issue Jul 9, 2022 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@nmeri17
Copy link
Owner

nmeri17 commented 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:

protected function saveFakeImage (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));

	return UploadedFileFactory::createUploadedFile($stream, $expectedSize, 0, $fileName);
}

private function getImageStream (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

@nmeri17 nmeri17 added enhancement New feature or request help wanted Extra attention is needed labels Jul 9, 2022
@nmeri17 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant