Skip to content

Commit

Permalink
tests/Integration: Add FileTest
Browse files Browse the repository at this point in the history
This is a regression test for #812.
  • Loading branch information
jtojnar committed May 23, 2023
1 parent 1337a89 commit 3cf2b9f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"autoload-dev": {
"psr-4": {
"SimplePie\\Tests\\Fixtures\\": "tests/Fixtures",
"SimplePie\\Tests\\Integration\\": "tests/Integration",
"SimplePie\\Tests\\Unit\\": "tests/Unit"
}
},
Expand Down
60 changes: 60 additions & 0 deletions tests/Integration/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
// SPDX-License-Identifier: BSD-3-Clause

declare(strict_types=1);

namespace SimplePie\Tests\Integration;

use Exception;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
use SimplePie\Cache;
use SimplePie\Cache\Base;
use SimplePie\File;
use SimplePie\Misc;
use SimplePie\SimplePie;
use SimplePie\Tests\Fixtures\Cache\BaseCacheWithCallbacksMock;
use SimplePie\Tests\Fixtures\FileMock;
use SimplePie\Tests\Integration\Fixtures\HttpServer;

final class FileTest extends TestCase
{
/**
* @return iterable<array{bool}>
*/
public function redirectsConfigProvider(): iterable
{
yield 'curl' => [
'force_fsockopen' => false,
];

yield 'fsockopen' => [
'force_fsockopen' => true,
];
}

/**
* @dataProvider redirectsConfigProvider
*/
public function testRedirects(bool $force_fsockopen): void
{
$server = new HttpServer(__DIR__ . '/Fixtures/redirects.php');
$server->start();

$base = $server->getBaseUri();
$file = new File(
"{$base}/perm2",
10, // timeout
10, // redirects
null, // headers
null, // useragent
$force_fsockopen
);
$server->terminate();

$this->assertSame("{$base}/temp2", $file->permanent_url);
$this->assertSame("{$base}/final", $file->url);
}
}
37 changes: 37 additions & 0 deletions tests/Integration/Fixtures/redirects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

$redirect = function (int $status, string $location): void {
http_response_code($status);
header("Location: $location");
};

$output = function (int $status, string $text): void {
http_response_code($status);
echo $text;
};

if ($_SERVER['REQUEST_URI'] === '/perm2') {
$redirect(308, '/perm1');
} elseif ($_SERVER['REQUEST_URI'] === '/perm1') {
$redirect(301, '/perm0');
} elseif ($_SERVER['REQUEST_URI'] === '/perm0') {
$redirect(308, '/temp2');
} elseif ($_SERVER['REQUEST_URI'] === '/temp2') {
$redirect(307, '/temp1');
} elseif ($_SERVER['REQUEST_URI'] === '/temp1') {
$redirect(302, '/temp0');
} elseif ($_SERVER['REQUEST_URI'] === '/temp0') {
$redirect(307, '/permA');
} elseif ($_SERVER['REQUEST_URI'] === '/permA') {
$redirect(301, '/permB');
} elseif ($_SERVER['REQUEST_URI'] === '/permB') {
$redirect(308, '/permC');
} elseif ($_SERVER['REQUEST_URI'] === '/permC') {
$redirect(308, '/final');
} elseif ($_SERVER['REQUEST_URI'] === '/final') {
$output(200, '/ok');
} else {
$output(404, 'Not found');
}

0 comments on commit 3cf2b9f

Please sign in to comment.