From 8a7813b6c568c44775f6dc4572108a2faaf3c93b Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 24 Mar 2024 22:34:48 +0100 Subject: [PATCH] use local PHP web server to test HTTP stream wrappers --- .../Filesystem/Tests/FilesystemTest.php | 33 +++++++++----- .../Filesystem/Tests/Fixtures/web/index.php | 1 + .../Fixtures/web/logo_symfony_header.png | Bin 0 -> 1613 bytes .../Component/Filesystem/composer.json | 3 +- .../Mime/Tests/Part/DataPartTest.php | 41 ++++++++++-------- 5 files changed, 47 insertions(+), 31 deletions(-) create mode 100644 src/Symfony/Component/Filesystem/Tests/Fixtures/web/index.php create mode 100644 src/Symfony/Component/Filesystem/Tests/Fixtures/web/logo_symfony_header.png diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 7280d51357bb..47c89995f789 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -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. @@ -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() diff --git a/src/Symfony/Component/Filesystem/Tests/Fixtures/web/index.php b/src/Symfony/Component/Filesystem/Tests/Fixtures/web/index.php new file mode 100644 index 000000000000..b3d9bbc7f371 --- /dev/null +++ b/src/Symfony/Component/Filesystem/Tests/Fixtures/web/index.php @@ -0,0 +1 @@ +P+)L?kDsr<&)@GO8iI-d00p^8L_t(|+TEM!dZRiFh9%=g7;pD~ zwI}6ZzB(8;nKJD&yuYTMVCl0KFQjyi)<#6Mp6LE4>r(;ITc-QME|w#UrF)|0T>4~T zx*aTKKz4L9bSh)wM0a8?U*qUDa4fIjneM@q%4{5WD0L7g3u)_D4#1)Wz1Bk z#1XoAW&(90lBP>34+4R|-LX)PxvY3eXH}Xo370YbN`T`@Q%ir}H(l%KTI8s4771lT zpB1?|`u1-KPNgMGL`v7AaX>?QV);h}Po-tvx??a?toF9PET!S#o2BuuZ6+^5=z~p|JQ9!92I2 zVF)gI9S-Ad))Rrad6S=ea-9Rrkl$>&(h!J%)bLcQ*Q(6|yL~q10x~xOr49#F=YSI# zf?Xpcb5(&5tGbiuh`?+GIPTfx(ZaO3lN`4@L)UKTn4rdM;{QF3A5U5)6RW>YMEv?8 zd7$^8C#y)=P!(6fAES7~HR*k|KBK@k>$(>;+h(nPKqi!D`g>_y=W?6VY4d~xyHhye z8S`d4O-q_Gas_fWvzp+0rY(0<6Od)M2N+LPYg2eoI;j4Fj?=5(K81`n_~06PM2-mu zH-8k&8G7NNf?(4WlCIXL&lznyn<#0{6Y!yjrWQ2Hz>o7BNdP0H;9>yv^7R)DIQq`u_J7Wn&DcA&Iq?J&Aio0P|C%2Cpoi{U9``ryk z5KQk27W$0TU!g!S0@l;G`ym~{1`TBQ*_1P=3mRqZHLAv1T`5?97A#%Cwi;EiPR)vh zY&82*b2%Z=8E61pb9zQfUKwFG5)A9oHc#wf_hM%B)L2dkbOL`lZXL zy#8~>{^@POh>Ll)5osV8trGQA>ls9BBQq@Z$kYefhOAl*o9_s=C?b5WxUU4yjde^y zmoUy~KfapD%@!Ix?hgf1>g=U6j|9Vd8y+{TQ7LDb@g0gZ7r0m|-xrJ@Fo=T-L%|eV z>%$o8;aiw=Sc!rdt<9X8(>bP0eM2xbCed2Egd7_RMmVRhFXHn!z_zp301@&O?oz%b zn517*W5L`Bk2VT?L`H{K z15CyzjG#L20J#yIpo7{93;0j{d`ZSGRU6VAJ{E2T2Zm#vZ9nHWuD*uQm3MYoN;?sw zxw{Qn=o*v}5i@=BjhsPz_q;cwJx5mbBDk&mB^0c8k{ahoKhXPz`?%ZrEu{ZiGa|w? zrFB(tFNR3j<*Kh~sRb$VV~njs z@oR+F_1X~e>f@(@bwyn`6mUuXD%{!r0{rzcR?3)nzY?eMO75qD^{l`11@WNXtDq3V zVwo=Edh3$QImy!fU`W2XT+m&UyrCPzW)1~}8ES8g(*JX>zgGGWdRYnkHtkA|00000 LNkvXXu0mjf1C89g literal 0 HcmV?d00001 diff --git a/src/Symfony/Component/Filesystem/composer.json b/src/Symfony/Component/Filesystem/composer.json index e756104cd5fa..95e9f3f035ee 100644 --- a/src/Symfony/Component/Filesystem/composer.json +++ b/src/Symfony/Component/Filesystem/composer.json @@ -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\\": "" }, diff --git a/src/Symfony/Component/Mime/Tests/Part/DataPartTest.php b/src/Symfony/Component/Mime/Tests/Part/DataPartTest.php index d0a692761fdf..361bb00be5d1 100644 --- a/src/Symfony/Component/Mime/Tests/Part/DataPartTest.php +++ b/src/Symfony/Component/Mime/Tests/Part/DataPartTest.php @@ -138,8 +138,8 @@ 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(); @@ -147,23 +147,26 @@ public function testFromPathWithUrl() $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()