Skip to content

[Filesystem] use local PHP web server to test HTTP stream wrappers #54388

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

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we stop the process in a finally block to ensure it is properly closed ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, done


$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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Loading
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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