Skip to content

Commit

Permalink
Modernize tests by removing LegacyPHPUnit
Browse files Browse the repository at this point in the history
Run PHPStan and PHPCSFixer on tests
  • Loading branch information
phpfui committed Jan 11, 2023
1 parent 3aebb78 commit 642797f
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 241 deletions.
85 changes: 43 additions & 42 deletions tests/StreamDecorators/Base64StreamTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace ZBateson\StreamDecorators;

use LegacyPHPUnit\TestCase;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\StreamWrapper;
use PHPUnit\Framework\TestCase;
use RuntimeException;

/**
Expand All @@ -15,112 +16,112 @@
*/
class Base64StreamTest extends TestCase
{
public function testReadContents()
public function testReadContents() : void
{
$str = 'é J\'interdis aux marchands de vanter trop leur marchandises. Car '
. 'ils se font vite pédagogues et t\'enseignent comme but ce qui '
. 'n\'est par essence qu\'un moyen, et te trompant ainsi sur la '
. 'route à suivre les voilà bientôt qui te dégradent, car si leur '
. 'musique est vulgaire ils te fabriquent pour te la vendre une âme '
. 'vulgaire.é';
for ($i = 0; $i < strlen($str); ++$i) {
$substr = substr($str, 0, $i + 1);
$stream = Psr7\Utils::streamFor(base64_encode($substr));
for ($i = 0; $i < \strlen($str); ++$i) {
$substr = \substr($str, 0, $i + 1);
$stream = Psr7\Utils::streamFor(\base64_encode($substr));
$b64Stream = new Base64Stream($stream);
$this->assertSame($substr, $b64Stream->getContents());
}
}

public function testReadToEof()
public function testReadToEof() : void
{
$str = 'é J\'interdis aux marchands de vanter trop leur marchandises. Car '
. 'ils se font vite pédagogues et t\'enseignent comme but ce qui '
. 'n\'est par essence qu\'un moyen, et te trompant ainsi sur la '
. 'route à suivre les voilà bientôt qui te dégradent, car si leur '
. 'musique est vulgaire ils te fabriquent pour te la vendre une âme '
. 'vulgaire.é';
for ($i = 0; $i < strlen($str); ++$i) {
$stream = Psr7\Utils::streamFor(base64_encode(substr($str, $i)));
for ($i = 0; $i < \strlen($str); ++$i) {
$stream = Psr7\Utils::streamFor(\base64_encode(\substr($str, $i)));
$b64Stream = new Base64Stream($stream);
for ($j = $i; !$b64Stream->eof(); ++$j) {
$this->assertEquals(substr($str, $j, 1), $b64Stream->read(1), "Failed reading to EOF on substr $i iteration $j");
$this->assertEquals(\substr($str, $j, 1), $b64Stream->read(1), "Failed reading to EOF on substr $i iteration $j");
}
}
}

public function testGetSize()
public function testGetSize() : void
{
$str = 'Sweetest little pie';
$stream = Psr7\Utils::streamFor(base64_encode($str));
$stream = Psr7\Utils::streamFor(\base64_encode($str));
$b64Stream = new Base64Stream($stream);
$this->assertNull($b64Stream->getSize());
}

public function testTell()
public function testTell() : void
{
$str = 'é J\'interdis aux marchands de vanter trop leur marchandises. Car '
. 'ils se font vite pédagogues et t\'enseignent comme but ce qui '
. 'n\'est par essence qu\'un moyen, et te trompant ainsi sur la '
. 'route à suivre les voilà bientôt qui te dégradent, car si leur '
. 'musique est vulgaire ils te fabriquent pour te la vendre une âme '
. 'vulgaire.é';
$stream = Psr7\Utils::streamFor(base64_encode($str));
for ($i = 1; $i < strlen($str); ++$i) {
$stream = Psr7\Utils::streamFor(\base64_encode($str));
for ($i = 1; $i < \strlen($str); ++$i) {
$stream->rewind();
$b64Stream = new Base64Stream(new NonClosingStream($stream));
for ($j = 0; $j < strlen($str); $j += $i) {
for ($j = 0; $j < \strlen($str); $j += $i) {
$this->assertSame($j, $b64Stream->tell(), "Tell at $j failed with $i step");
$b64Stream->read($i);
}
$this->assertSame(strlen($str), $b64Stream->tell(), "Final tell failed with $i step");
$this->assertSame(\strlen($str), $b64Stream->tell(), "Final tell failed with $i step");
}
}

public function testDecodeFile()
public function testDecodeFile() : void
{
$encoded = __DIR__ . '/../_data/blueball.b64.txt';
$org = __DIR__ . '/../_data/blueball.png';
$f = fopen($encoded, 'r');
$f = \fopen($encoded, 'r');

$streamDecorator = new Base64Stream(new PregReplaceFilterStream(Psr7\Utils::streamFor($f), '/[^a-zA-Z0-9\/\+=]/', ''));
$handle = StreamWrapper::getResource($streamDecorator);

$this->assertSame(file_get_contents($org), stream_get_contents($handle), 'Decoded blueball not equal to original file');
$this->assertSame(\file_get_contents($org), \stream_get_contents($handle), 'Decoded blueball not equal to original file');

fclose($handle);
fclose($f);
\fclose($handle);
\fclose($f);
}

public function testDecodeWordFileWithStreamGetContents()
public function testDecodeWordFileWithStreamGetContents() : void
{
$encoded = __DIR__ . '/../_data/test.b64.txt';
$org = __DIR__ . '/../_data/test.doc';
$f = fopen($encoded, 'r');
$f = \fopen($encoded, 'r');

$streamDecorator = new Base64Stream(new PregReplaceFilterStream(Psr7\Utils::streamFor($f), '/[^a-zA-Z0-9\/\+=]/', ''));
$handle = StreamWrapper::getResource($streamDecorator);

$horg = fopen($org, 'r');
$this->assertSame(stream_get_contents($horg), stream_get_contents($handle));
$horg = \fopen($org, 'r');
$this->assertSame(\stream_get_contents($horg), \stream_get_contents($handle));

fclose($horg);
fclose($handle);
fclose($f);
\fclose($horg);
\fclose($handle);
\fclose($f);
}

public function testWriteAndDetach()
public function testWriteAndDetach() : void
{
$org = __DIR__ . '/../_data/blueball.png';
$contents = file_get_contents($org);
$contents = \file_get_contents($org);

for ($i = 1; $i < strlen($contents); ++$i) {
for ($i = 1; $i < \strlen($contents); ++$i) {

$f = fopen('php://temp', 'r+');
$f = \fopen('php://temp', 'r+');
$stream = Psr7\Utils::streamFor($f);

$ostream = new Base64Stream(new NonClosingStream($stream));
for ($j = 0; $j < strlen($contents); $j += $i) {
$ostream->write(substr($contents, $j, $i));
for ($j = 0; $j < \strlen($contents); $j += $i) {
$ostream->write(\substr($contents, $j, $i));
}
$ostream->detach();

Expand All @@ -130,7 +131,7 @@ public function testWriteAndDetach()
}
}

public function testWriteDifferentContentLengths()
public function testWriteDifferentContentLengths() : void
{
$contents = 'é J\'interdis aux marchands de vanter trop leur marchandises. Car '
. 'ils se font vite pédagogues et t\'enseignent comme but ce qui '
Expand All @@ -139,15 +140,15 @@ public function testWriteDifferentContentLengths()
. 'musique est vulgaire ils te fabriquent pour te la vendre une âme '
. 'vulgaire.é';

for ($i = 1; $i < strlen($contents); ++$i) {
for ($i = 1; $i < \strlen($contents); ++$i) {

$str = substr($contents, 0, strlen($contents) - $i);
$f = fopen('php://temp', 'r+');
$str = \substr($contents, 0, \strlen($contents) - $i);
$f = \fopen('php://temp', 'r+');
$stream = Psr7\Utils::streamFor($f);

$ostream = new Base64Stream(new NonClosingStream($stream));
for ($j = 0; $j < strlen($str); $j += $i) {
$ostream->write(substr($str, $j, $i));
for ($j = 0; $j < \strlen($str); $j += $i) {
$ostream->write(\substr($str, $j, $i));
}
$ostream->close();

Expand All @@ -157,10 +158,10 @@ public function testWriteDifferentContentLengths()
}
}

public function testSeekUnsopported()
public function testSeekUnsopported() : void
{
$str = 'Sweetest little pie';
$stream = Psr7\Utils::streamFor(base64_encode($str));
$stream = Psr7\Utils::streamFor(\base64_encode($str));
$test = new Base64Stream($stream);
$this->assertFalse($test->isSeekable());
$exceptionThrown = false;
Expand Down
83 changes: 43 additions & 40 deletions tests/StreamDecorators/CharsetStreamTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace ZBateson\StreamDecorators;

use LegacyPHPUnit\TestCase;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\StreamWrapper;
use ZBateson\MbWrapper\MbWrapper;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use ZBateson\MbWrapper\MbWrapper;

/**
* Description of CharsetStreamTest
Expand All @@ -16,89 +16,92 @@
*/
class CharsetStreamTest extends TestCase
{
/**
* @var MbWrapper
*/
private $converter;

protected function legacySetUp()
protected function SetUp() : void
{
$this->converter = new MbWrapper();
}

public function testRead()
public function testRead() : void
{
$str = str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 50);
$str = \str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 50);

$stream = Psr7\Utils::streamFor($this->converter->convert($str, 'UTF-8', 'UTF-32'));
for ($i = 1; $i < mb_strlen($str, 'UTF-8'); ++$i) {
for ($i = 1; $i < \mb_strlen($str, 'UTF-8'); ++$i) {
$stream->rewind();
$csStream = new CharsetStream(new NonClosingStream($stream), 'UTF-32', 'UTF-8');
for ($j = 0; $j < mb_strlen($str, 'UTF-8'); $j += $i) {
for ($j = 0; $j < \mb_strlen($str, 'UTF-8'); $j += $i) {
$char = $csStream->read($i);
$this->assertSame(mb_substr($str, $j, $i, 'UTF-8'), $char, "Read $j failed at $i step");
$this->assertSame(\mb_substr($str, $j, $i, 'UTF-8'), $char, "Read $j failed at $i step");
}
$this->assertSame(mb_strlen($str, 'UTF-8'), $csStream->tell(), "Final tell failed with $i step");
$this->assertSame(\mb_strlen($str, 'UTF-8'), $csStream->tell(), "Final tell failed with $i step");
}
}

public function testReadContents()
public function testReadContents() : void
{
$str = str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 50);
$str = \str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 50);

for ($i = 0; $i < mb_strlen($str); ++$i) {
$substr = mb_substr($str, 0, $i + 1, 'UTF-8');
for ($i = 0; $i < \mb_strlen($str); ++$i) {
$substr = \mb_substr($str, 0, $i + 1, 'UTF-8');
$stream = Psr7\Utils::streamFor($this->converter->convert($substr, 'UTF-8', 'UTF-16'));
$csStream = new CharsetStream($stream, 'UTF-16', 'UTF-8');
$this->assertSame($substr, $csStream->getContents());
}
}

public function testReadToEof()
public function testReadToEof() : void
{
$str = str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
for ($i = 0; $i < mb_strlen($str, 'UTF-8'); ++$i) {
$substr = mb_substr($str, $i, null, 'UTF-8');
$str = \str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
for ($i = 0; $i < \mb_strlen($str, 'UTF-8'); ++$i) {
$substr = \mb_substr($str, $i, null, 'UTF-8');
$stream = Psr7\Utils::streamFor($this->converter->convert($substr, 'UTF-8', 'WINDOWS-1256'));
$csStream = new CharsetStream($stream, 'WINDOWS-1256', 'UTF-8');
for ($j = 0; !$csStream->eof(); ++$j) {
$read = $csStream->read(1);
$this->assertSame(mb_substr($substr, $j, 1, 'UTF-8'), $read, "Failed reading to EOF on substr $i iteration $j");
$this->assertSame(\mb_substr($substr, $j, 1, 'UTF-8'), $read, "Failed reading to EOF on substr $i iteration $j");
}
}
}

public function testReadUtf16LeToEof()
public function testReadUtf16LeToEof() : void
{
$str = str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
for ($i = 0; $i < mb_strlen($str, 'UTF-8'); ++$i) {
$substr = mb_substr($str, $i, null, 'UTF-8');
$str = \str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
for ($i = 0; $i < \mb_strlen($str, 'UTF-8'); ++$i) {
$substr = \mb_substr($str, $i, null, 'UTF-8');
$stream = Psr7\Utils::streamFor($this->converter->convert($substr, 'UTF-8', 'UTF-16LE'));
$csStream = new CharsetStream($stream, 'UTF-16LE', 'UTF-8');
for ($j = 0; !$csStream->eof(); ++$j) {
$read = $csStream->read(1);
$this->assertSame(mb_substr($substr, $j, 1, 'UTF-8'), $read, "Failed reading to EOF on substr $i iteration $j");
$this->assertSame(\mb_substr($substr, $j, 1, 'UTF-8'), $read, "Failed reading to EOF on substr $i iteration $j");
}
}
}

public function testReadToEmpty()
public function testReadToEmpty() : void
{
$str = str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
$str = \str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
$stream = Psr7\Utils::streamFor($this->converter->convert($str, 'UTF-8', 'WINDOWS-1256'));
$csStream = new CharsetStream($stream, 'WINDOWS-1256', 'UTF-8');
$i = 0;
while (($chr = $csStream->read(1)) !== '') {
$this->assertSame(mb_substr($str, $i++, 1, 'UTF-8'), $chr, "Failed reading to false on substr $i");
$this->assertSame(\mb_substr($str, $i++, 1, 'UTF-8'), $chr, "Failed reading to false on substr $i");
}
}

public function testGetSize()
public function testGetSize() : void
{
$str = 'Sweetest little pie';
$stream = Psr7\Utils::streamFor($this->converter->convert($str, 'UTF-8', 'UTF-16'));
$csStream = new CharsetStream($stream, 'UTF-16', 'UTF-8');
$this->assertNull($csStream->getSize());
}

public function testTell()
public function testTell() : void
{
$str = 'é J\'interdis aux marchands de vanter trop leur marchandises. Car '
. 'ils se font vite pédagogues et t\'enseignent comme but ce qui '
Expand All @@ -108,18 +111,18 @@ public function testTell()
. 'vulgaire.é';
$stream = Psr7\Utils::streamFor($str);

for ($i = 1; $i < strlen($str); ++$i) {
for ($i = 1; $i < \strlen($str); ++$i) {
$stream->rewind();
$csStream = new CharsetStream(new NonClosingStream($stream));
for ($j = 0; $j < strlen($str); $j += $i) {
for ($j = 0; $j < \strlen($str); $j += $i) {
$this->assertSame($j, $csStream->tell(), "Tell at $j failed with $i step");
$csStream->read($i);
}
$this->assertSame(strlen($str), $csStream->tell(), "Final tell failed with $i step");
$this->assertSame(\strlen($str), $csStream->tell(), "Final tell failed with $i step");
}
}

public function testSeekUnsopported()
public function testSeekUnsopported() : void
{
$stream = Psr7\Utils::streamFor('Sweetest little pie');
$test = new CharsetStream($stream);
Expand All @@ -133,14 +136,14 @@ public function testSeekUnsopported()
$this->assertTrue($exceptionThrown);
}

public function testWrite()
public function testWrite() : void
{
$str = str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
for ($i = 1; $i < mb_strlen($str); ++$i) {
$stream = Psr7\Utils::streamFor(fopen('php://temp', 'r+'));
$str = \str_repeat('هلا هلا شخبار بعد؟ شلون تبرمج؟', 10);
for ($i = 1; $i < \mb_strlen($str); ++$i) {
$stream = Psr7\Utils::streamFor(\fopen('php://temp', 'r+'));
$oStream = new CharsetStream(new NonClosingStream($stream), 'UTF-32', 'UTF-8');
for ($j = 0; $j < mb_strlen($str, 'UTF-8'); $j += $i) {
$oStream->write(mb_substr($str, $j, $i, 'UTF-8'));
for ($j = 0; $j < \mb_strlen($str, 'UTF-8'); $j += $i) {
$oStream->write(\mb_substr($str, $j, $i, 'UTF-8'));
}
$stream->rewind();

Expand All @@ -150,7 +153,7 @@ public function testWrite()
$stream->rewind();
$streamContents = $stream->getContents();
$this->assertNotEquals($str, $streamContents);
$this->assertGreaterThan(strlen($str), strlen($streamContents));
$this->assertGreaterThan(\strlen($str), \strlen($streamContents));
$this->assertSame($str, $this->converter->convert($streamContents, 'UTF-32', 'UTF-8'));
}
}
Expand Down
Loading

0 comments on commit 642797f

Please # to comment.