From d9f5cc52acfa780606a1fba3ec1e9ea4fff3501b Mon Sep 17 00:00:00 2001 From: Beniamin Gheorghita Date: Wed, 16 Aug 2023 13:07:55 +0300 Subject: [PATCH] Changed the approach for PDO returned values on tests and changed stream wrapper --- tests-integration/AfterScriptRunTest.php | 6 ++++-- tests/AnonymizerTest.php | 18 +++++++++++------- tests/Application/Observer/ProgressTest.php | 9 +-------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/tests-integration/AfterScriptRunTest.php b/tests-integration/AfterScriptRunTest.php index 59b7b14..6d6764c 100644 --- a/tests-integration/AfterScriptRunTest.php +++ b/tests-integration/AfterScriptRunTest.php @@ -51,7 +51,9 @@ public static function setUpBeforeClass(): void self::$destination = new PDO($dsnDestination, $dbUser, $dbPass); self::$source->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + self::$source->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); self::$destination->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + self::$destination->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); } public function testNullIsAnonymizedWithNull(): void @@ -216,8 +218,8 @@ public function testTruncatedTable(): void $stmt = self::$destination->query($query); $destRow = $stmt->fetch(PDO::FETCH_ASSOC); - $this->assertGreaterThan(0, (int)$sourceRow['nr']); - $this->assertSame(0, (int)$destRow['nr']); + $this->assertGreaterThan('0', $sourceRow['nr']); + $this->assertSame('0', $destRow['nr']); } /** diff --git a/tests/AnonymizerTest.php b/tests/AnonymizerTest.php index 640aa4a..0f945d0 100644 --- a/tests/AnonymizerTest.php +++ b/tests/AnonymizerTest.php @@ -34,8 +34,6 @@ final class AnonymizerTest extends TestCase /** @var ObserverInterface|MockObject */ private $observerMock; - public const TMP_FILE_PATH = "/tmp/testData"; - public function setUp(): void { parent::setUp(); @@ -54,8 +52,8 @@ public function setUp(): void public function testRun(): void { - $inputStream = $this->makeLineStream(['line1', 'line2']); - $outputStream = $this->makeLineStream([]); + $inputStream = $this->makeInputStream(['line1', 'line2']); + $outputStream = $this->makeOutputStream([]); $data = [ 'table1' => [ @@ -285,12 +283,18 @@ public function testRun(): void $this->assertSame(['INSERT table1' . "\n", 'INSERT table2'], $actual); } - private function makeLineStream(array $lines) + private function makeInputStream(array $lines) { + $fp = fopen('data://text/plain;base64,' . base64_encode(implode("\n", $lines)), 'ab+'); + rewind($fp); - $fp = fopen(self::TMP_FILE_PATH . (string)rand(0, 999), 'w+'); - fwrite($fp, implode("\n", $lines)); + return $fp; + } + private function makeOutputStream(array $lines) + { + $fp = fopen('php://memory', 'ab+'); + fwrite($fp, implode("\n", $lines)); rewind($fp); return $fp; diff --git a/tests/Application/Observer/ProgressTest.php b/tests/Application/Observer/ProgressTest.php index 08bf2ae..e0a916f 100644 --- a/tests/Application/Observer/ProgressTest.php +++ b/tests/Application/Observer/ProgressTest.php @@ -10,8 +10,6 @@ class ProgressTest extends TestCase { private Progress $sut; - public const TMP_FILE_PATH = "/tmp/testData"; - public function setUp(): void { $this->sut = new Progress(); @@ -157,7 +155,7 @@ private function getProperty($name) private function setOutputStream() { - $fp = fopen(self::TMP_FILE_PATH, 'w+'); + $fp = fopen('php://memory', 'ab+'); rewind($fp); $refObject = new ReflectionObject($this->sut); @@ -173,9 +171,4 @@ private function getOutputStream() $refProperty->setAccessible(true); return $refProperty->getValue($this->sut); } - - public static function tearDownAfterClass(): void - { - unlink(self::TMP_FILE_PATH); - } }