From 79ffb61f94139a4cc5f51568b6a0c1dfbd6ee191 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Sat, 23 Mar 2024 09:49:19 -0600 Subject: [PATCH] phpunit fixes for deprecated notices. Also pdowrapper reset removal --- flight/database/PdoWrapper.php | 3 ++- flight/util/Collection.php | 28 ---------------------------- phpunit.xml | 1 + tests/CollectionTest.php | 23 ----------------------- tests/DispatcherTest.php | 2 +- tests/EngineTest.php | 5 +++-- 6 files changed, 7 insertions(+), 55 deletions(-) diff --git a/flight/database/PdoWrapper.php b/flight/database/PdoWrapper.php index bdbabb8..297121a 100644 --- a/flight/database/PdoWrapper.php +++ b/flight/database/PdoWrapper.php @@ -49,7 +49,8 @@ class PdoWrapper extends PDO public function fetchField(string $sql, array $params = []) { $result = $this->fetchRow($sql, $params); - return reset($result); + $data = $result->getData(); + return reset($data); } /** diff --git a/flight/util/Collection.php b/flight/util/Collection.php index 29147d1..6ffe0b5 100644 --- a/flight/util/Collection.php +++ b/flight/util/Collection.php @@ -20,18 +20,6 @@ use JsonSerializable; */ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable { - /** - * This is to allow for reset() to work properly. - * - * WARNING! This MUST be the first variable in this class!!! - * - * PHPStan is ignoring this because we don't need actually to read the property - * - * @var mixed - * @phpstan-ignore-next-line - */ - private $first_property = null; - /** * Collection data. * @@ -47,7 +35,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable public function __construct(array $data = []) { $this->data = $data; - $this->handleReset(); } /** @@ -68,7 +55,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable public function __set(string $key, $value): void { $this->data[$key] = $value; - $this->handleReset(); } /** @@ -85,7 +71,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable public function __unset(string $key): void { unset($this->data[$key]); - $this->handleReset(); } /** @@ -115,7 +100,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable } else { $this->data[$offset] = $value; } - $this->handleReset(); } /** @@ -136,17 +120,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable public function offsetUnset($offset): void { unset($this->data[$offset]); - $this->handleReset(); - } - - /** - * This is to allow for reset() of a Collection to work properly. - * - * @return void - */ - public function handleReset() - { - $this->first_property = reset($this->data); } /** @@ -234,7 +207,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable public function setData(array $data): void { $this->data = $data; - $this->handleReset(); } #[\ReturnTypeWillChange] diff --git a/phpunit.xml b/phpunit.xml index c97f669..18134c0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -23,6 +23,7 @@ + diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 4f742fe..b6eac46 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -99,27 +99,4 @@ class CollectionTest extends TestCase $this->collection->clear(); $this->assertEquals(0, $this->collection->count()); } - - public function testResetByProperty() - { - $this->collection->a = 11; - $this->collection->b = 22; - $result = reset($this->collection); - $this->assertEquals(11, $result); - } - - public function testResetBySetData() - { - $this->collection->setData(['a' => 11, 'b' => 22]); - $result = reset($this->collection); - $this->assertEquals(11, $result); - } - - public function testResetByArraySet() - { - $this->collection['a'] = 11; - $this->collection['b'] = 22; - $result = reset($this->collection); - $this->assertEquals(11, $result); - } } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index d4e1ca1..a755666 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -305,7 +305,7 @@ class DispatcherTest extends TestCase public function testExecuteStringClassDefaultContainerButForgotInjectingEngine(): void { $this->expectException(TypeError::class); - $this->expectExceptionMessageMatches('#Argument 1 passed to tests\\\\classes\\\\ContainerDefault::__construct\(\) must be an instance of flight\\\\Engine, null given#'); + $this->expectExceptionMessageMatches('#tests\\\\classes\\\\ContainerDefault::__construct\(\).+flight\\\\Engine, null given#'); $result = $this->dispatcher->execute([ ContainerDefault::class, 'testTheContainer' ]); } } diff --git a/tests/EngineTest.php b/tests/EngineTest.php index a8d58ef..bdf511e 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace tests; +use ErrorException; use Exception; use flight\database\PdoWrapper; use flight\Engine; @@ -750,8 +751,8 @@ class EngineTest extends TestCase $engine->route('/container', Container::class.'->testThePdoWrapper'); $engine->request()->url = '/container'; - $this->expectException(PDOException::class); - $this->expectExceptionMessage("invalid data source name"); + $this->expectException(ErrorException::class); + $this->expectExceptionMessageMatches("/Passing null to parameter/"); $engine->start(); }