Skip to content

Commit

Permalink
use local PHP web server to test HTTP stream wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Apr 3, 2024
1 parent 50268e6 commit 8a7813b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
33 changes: 22 additions & 11 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Filesystem\Exception\InvalidArgumentException;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

/**
* Test class for Filesystem.
Expand Down Expand Up @@ -162,23 +164,32 @@ public function testCopyCreatesTargetDirectoryIfItDoesNotExist()
$this->assertStringEqualsFile($targetFilePath, 'SOURCE FILE');
}

/**
* @group network
*/
public function testCopyForOriginUrlsAndExistingLocalFileDefaultsToCopy()
{
if (!\in_array('https', stream_get_wrappers())) {
$this->markTestSkipped('"https" stream wrapper is not enabled.');
if (!\in_array('http', stream_get_wrappers())) {
$this->markTestSkipped('"http" stream wrapper is not enabled.');
}
$sourceFilePath = 'https://symfony.com/images/common/logo/logo_symfony_header.png';
$targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';

file_put_contents($targetFilePath, 'TARGET FILE');
$finder = new PhpExecutableFinder();
$process = new Process(array_merge([$finder->find(false)], $finder->findArguments(), ['-dopcache.enable=0', '-dvariables_order=EGPCS', '-S', '127.0.0.1:8057']));
$process->setWorkingDirectory(__DIR__.'/Fixtures/web');

$this->filesystem->copy($sourceFilePath, $targetFilePath, false);
$process->start();

$this->assertFileExists($targetFilePath);
$this->assertEquals(file_get_contents($sourceFilePath), file_get_contents($targetFilePath));
do {
usleep(50000);
} while (!@fopen('http://127.0.0.1:8057', 'r'));

try {
$sourceFilePath = 'http://localhost:8057/logo_symfony_header.png';
$targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
file_put_contents($targetFilePath, 'TARGET FILE');
$this->filesystem->copy($sourceFilePath, $targetFilePath, false);
$this->assertFileExists($targetFilePath);
$this->assertEquals(file_get_contents($sourceFilePath), file_get_contents($targetFilePath));
} finally {
$process->stop();
}
}

public function testMkdirCreatesDirectoriesRecursively()
Expand Down
@@ -0,0 +1 @@
<?php
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/Symfony/Component/Filesystem/composer.json
Expand Up @@ -19,7 +19,8 @@
"php": ">=7.2.5",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.8",
"symfony/polyfill-php80": "^1.16"
"symfony/polyfill-php80": "^1.16",
"symfony/process": "^5.4|^6.4"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Filesystem\\": "" },
Expand Down
41 changes: 22 additions & 19 deletions src/Symfony/Component/Mime/Tests/Part/DataPartTest.php
Expand Up @@ -138,32 +138,35 @@ public function testFromPathWithNotAFile()

public function testFromPathWithUrl()
{
if (!\in_array('https', stream_get_wrappers())) {
$this->markTestSkipped('"https" stream wrapper is not enabled.');
if (!\in_array('http', stream_get_wrappers())) {
$this->markTestSkipped('"http" stream wrapper is not enabled.');
}

$finder = new PhpExecutableFinder();
$process = new Process(array_merge([$finder->find(false)], $finder->findArguments(), ['-dopcache.enable=0', '-dvariables_order=EGPCS', '-S', '127.0.0.1:8057']));
$process->setWorkingDirectory(__DIR__.'/../Fixtures/web');
$process->start();

do {
usleep(50000);
} while (!@fopen('http://127.0.0.1:8057', 'r'));

$p = DataPart::fromPath($file = 'http://localhost:8057/logo_symfony_header.png');
$content = file_get_contents($file);
$this->assertEquals($content, $p->getBody());
$maxLineLength = 76;
$this->assertEquals(substr(base64_encode($content), 0, $maxLineLength), substr($p->bodyToString(), 0, $maxLineLength));
$this->assertEquals(substr(base64_encode($content), 0, $maxLineLength), substr(implode('', iterator_to_array($p->bodyToIterable())), 0, $maxLineLength));
$this->assertEquals('image', $p->getMediaType());
$this->assertEquals('png', $p->getMediaSubType());
$this->assertEquals(new Headers(
new ParameterizedHeader('Content-Type', 'image/png', ['name' => 'logo_symfony_header.png']),
new UnstructuredHeader('Content-Transfer-Encoding', 'base64'),
new ParameterizedHeader('Content-Disposition', 'attachment', ['name' => 'logo_symfony_header.png', 'filename' => 'logo_symfony_header.png'])
), $p->getPreparedHeaders());
try {
do {
usleep(50000);
} while (!@fopen('http://127.0.0.1:8057', 'r'));
$p = DataPart::fromPath($file = 'http://localhost:8057/logo_symfony_header.png');
$content = file_get_contents($file);
$this->assertEquals($content, $p->getBody());
$maxLineLength = 76;
$this->assertEquals(substr(base64_encode($content), 0, $maxLineLength), substr($p->bodyToString(), 0, $maxLineLength));
$this->assertEquals(substr(base64_encode($content), 0, $maxLineLength), substr(implode('', iterator_to_array($p->bodyToIterable())), 0, $maxLineLength));
$this->assertEquals('image', $p->getMediaType());
$this->assertEquals('png', $p->getMediaSubType());
$this->assertEquals(new Headers(
new ParameterizedHeader('Content-Type', 'image/png', ['name' => 'logo_symfony_header.png']),
new UnstructuredHeader('Content-Transfer-Encoding', 'base64'),
new ParameterizedHeader('Content-Disposition', 'attachment', ['name' => 'logo_symfony_header.png', 'filename' => 'logo_symfony_header.png'])
), $p->getPreparedHeaders());
} finally {
$process->stop();
}
}

public function testHasContentId()
Expand Down

0 comments on commit 8a7813b

Please sign in to comment.