From f808da1f7495cf9a9077cec6f480a545e098a705 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 9 Oct 2022 13:34:50 +0000 Subject: [PATCH 1/2] Remove PDOMock from EnvironmentTest Signed-off-by: Matthew Peveler --- .../Migration/Manager/EnvironmentTest.php | 21 ++++++++--- .../Phinx/Migration/Manager/Mock/PDOMock.php | 36 ------------------- 2 files changed, 17 insertions(+), 40 deletions(-) delete mode 100644 tests/Phinx/Migration/Manager/Mock/PDOMock.php diff --git a/tests/Phinx/Migration/Manager/EnvironmentTest.php b/tests/Phinx/Migration/Manager/EnvironmentTest.php index 059fe423e..f134dc0b2 100644 --- a/tests/Phinx/Migration/Manager/EnvironmentTest.php +++ b/tests/Phinx/Migration/Manager/EnvironmentTest.php @@ -2,12 +2,12 @@ namespace Test\Phinx\Migration\Manager; +use PDO; use Phinx\Db\Adapter\AdapterFactory; use Phinx\Migration\Manager\Environment; use Phinx\Migration\MigrationInterface; use PHPUnit\Framework\TestCase; use RuntimeException; -use Test\Phinx\Migration\Manager\Mock\PDOMock; class EnvironmentTest extends TestCase { @@ -57,21 +57,34 @@ public function testNoAdapter() $this->environment->getAdapter(); } + private function getPdoMock() + { + $pdoMock = $this->getMockBuilder(PDO::class)->disableOriginalConstructor()->getMock(); + $attributes = []; + $pdoMock->method('setAttribute')->will($this->returnCallback(function ($attribute, $value) use (&$attributes) { + $attributes[$attribute] = $value; + return true; + })); + $pdoMock->method('getAttribute')->will($this->returnCallback(function ($attribute) use (&$attributes) { + return $attributes[$attribute] ?? 'pdomock'; + })); + return $pdoMock; + } + public function testGetAdapterWithExistingPdoInstance() { $adapter = $this->getMockForAbstractClass('\Phinx\Db\Adapter\PdoAdapter', [['foo' => 'bar']]); AdapterFactory::instance()->registerAdapter('pdomock', $adapter); - $this->environment->setOptions(['connection' => new PDOMock()]); + $this->environment->setOptions(['connection' => $this->getPdoMock()]); $options = $this->environment->getAdapter()->getOptions(); $this->assertEquals('pdomock', $options['adapter']); } public function testSetPdoAttributeToErrmodeException() { - $pdoMock = new PDOMock(); $adapter = $this->getMockForAbstractClass('\Phinx\Db\Adapter\PdoAdapter', [['foo' => 'bar']]); AdapterFactory::instance()->registerAdapter('pdomock', $adapter); - $this->environment->setOptions(['connection' => $pdoMock]); + $this->environment->setOptions(['connection' => $this->getPdoMock()]); $options = $this->environment->getAdapter()->getOptions(); $this->assertEquals(\PDO::ERRMODE_EXCEPTION, $options['connection']->getAttribute(\PDO::ATTR_ERRMODE)); } diff --git a/tests/Phinx/Migration/Manager/Mock/PDOMock.php b/tests/Phinx/Migration/Manager/Mock/PDOMock.php deleted file mode 100644 index 7b83e4481..000000000 --- a/tests/Phinx/Migration/Manager/Mock/PDOMock.php +++ /dev/null @@ -1,36 +0,0 @@ -attributes[$attribute] ?? 'pdomock'; - } - - /** - * @param int $attribute Attribute - * @param mixed $value Value - * @return bool - */ - public function setAttribute($attribute, $value) - { - $this->attributes[$attribute] = $value; - - return true; - } -} From 2768d0d63ae2c4709faf2715765b86f604d0ac6c Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 9 Oct 2022 13:42:09 +0000 Subject: [PATCH 2/2] phpcs Signed-off-by: Matthew Peveler --- tests/Phinx/Migration/Manager/EnvironmentTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Phinx/Migration/Manager/EnvironmentTest.php b/tests/Phinx/Migration/Manager/EnvironmentTest.php index f134dc0b2..4ebeea88b 100644 --- a/tests/Phinx/Migration/Manager/EnvironmentTest.php +++ b/tests/Phinx/Migration/Manager/EnvironmentTest.php @@ -63,11 +63,13 @@ private function getPdoMock() $attributes = []; $pdoMock->method('setAttribute')->will($this->returnCallback(function ($attribute, $value) use (&$attributes) { $attributes[$attribute] = $value; + return true; })); $pdoMock->method('getAttribute')->will($this->returnCallback(function ($attribute) use (&$attributes) { return $attributes[$attribute] ?? 'pdomock'; })); + return $pdoMock; }