From 245cb65f6281028352eb02e120f62bcc9be061a0 Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Thu, 31 Aug 2023 18:10:55 -0400 Subject: [PATCH 1/9] Fix for issue #35. Corrected ActiveRecordTest's so that capitalization for attribute names are retained. So the short form for New York is now NY instead of ny. test/model/Author, Book, and Venue now have those strtoupper() workarounds in getter/setter methods removed as it's done in Model.php --- lib/Model.php | 8 ++++---- test/ActiveRecordTest.php | 18 +++++++++--------- test/SerializationTest.php | 4 +--- test/models/Author.php | 1 - test/models/Book.php | 16 +--------------- test/models/Venue.php | 4 ---- 6 files changed, 15 insertions(+), 36 deletions(-) diff --git a/lib/Model.php b/lib/Model.php index 1857d8ab..01c52a2d 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -349,13 +349,12 @@ public function __construct(array $attributes=[], $guard_attributes=true, $insta public function &__get($name) { // check for getter + $name = strtolower($name); if (method_exists($this, "get_$name")) { $name = "get_$name"; - $value = $this->$name(); - - return $value; + $retValue = $this->$name(); + return $retValue; } - return $this->read_attribute($name); } @@ -445,6 +444,7 @@ public function __isset($attribute_name) */ public function __set($name, $value) { + $name = strtolower($name); if (array_key_exists($name, static::$alias_attribute)) { $name = static::$alias_attribute[$name]; } elseif (method_exists($this, "set_$name")) { diff --git a/test/ActiveRecordTest.php b/test/ActiveRecordTest.php index c97b7aac..c356ded8 100644 --- a/test/ActiveRecordTest.php +++ b/test/ActiveRecordTest.php @@ -413,8 +413,8 @@ public function test_delegate_returns_null_if_relationship_does_not_exist() public function test_delegate_set_attribute() { $event = Event::first(); - $event->state = 'MEXICO'; - $this->assertEquals('MEXICO', $event->venue->state); + $event->state = 'Mexico'; + $this->assertEquals('Mexico', $event->venue->state); } public function test_delegate_getter_gh_98() @@ -422,8 +422,8 @@ public function test_delegate_getter_gh_98() Venue::$use_custom_get_state_getter = true; $event = Event::first(); - $this->assertEquals('ny', $event->venue->state); - $this->assertEquals('ny', $event->state); + $this->assertEquals('NY', $event->venue->state); + $this->assertEquals('NY', $event->state); Venue::$use_custom_get_state_getter = false; } @@ -433,8 +433,8 @@ public function test_delegate_setter_gh_98() Venue::$use_custom_set_state_setter = true; $event = Event::first(); - $event->state = 'MEXICO'; - $this->assertEquals('MEXICO#', $event->venue->state); + $event->state = 'Mexico'; + $this->assertEquals('Mexico#', $event->venue->state); Venue::$use_custom_set_state_setter = false; } @@ -461,13 +461,13 @@ public function test_setter_with_same_name_as_an_attribute() { $author = new Author(); $author->name = 'bob'; - $this->assertEquals('BOB', $author->name); + $this->assertEquals('bob', $author->name); } public function test_getter() { $book = Book::first(); - $this->assertEquals(strtoupper($book->name), $book->upper_name); + $this->assertEquals($book->name, $book->get_name()); } public function test_getter_with_same_name_as_an_attribute() @@ -475,7 +475,7 @@ public function test_getter_with_same_name_as_an_attribute() Book::$use_custom_get_name_getter = true; $book = new Book(); $book->name = 'bob'; - $this->assertEquals('BOB', $book->name); + $this->assertEquals('bob', $book->name); Book::$use_custom_get_name_getter = false; } diff --git a/test/SerializationTest.php b/test/SerializationTest.php index 10214313..76a683e7 100644 --- a/test/SerializationTest.php +++ b/test/SerializationTest.php @@ -40,8 +40,6 @@ public function test_only_should_only_apply_to_attributes() { $this->assertArrayHasKey('author', $this->_a(['only' => 'name', 'include' => 'author'])); $this->assertArrayHasKey('name', $this->_a(['only' => 'name', 'include' => 'author'])); - $this->assertArrayHasKey('book_id', $this->_a(['only' => 'book_id', 'methods' => 'upper_name'])); - $this->assertArrayHasKey('upper_name', $this->_a(['only' => 'book_id', 'methods' => 'upper_name'])); } public function test_only_overrides_except() @@ -77,7 +75,7 @@ public function test_methods_takes_a_string() public function test_methods_method_same_as_attribute() { $a = $this->_a(['methods' => 'name']); - $this->assertEquals('ancient art of main tanking', $a['name']); + $this->assertEquals('Ancient Art of Main Tanking', $a['name']); } public function test_include() diff --git a/test/models/Author.php b/test/models/Author.php index 51e19ee0..18ed4162 100644 --- a/test/models/Author.php +++ b/test/models/Author.php @@ -22,7 +22,6 @@ public function set_password($plaintext) public function set_name($value) { - $value = strtoupper($value); $this->assign_attribute('name', $value); } diff --git a/test/models/Book.php b/test/models/Book.php index b97001ab..4e73e4f5 100644 --- a/test/models/Book.php +++ b/test/models/Book.php @@ -16,25 +16,11 @@ public function upper_name() public function name() { - return strtolower($this->name); + return $this->name; } public function get_name() { - if (self::$use_custom_get_name_getter) { - return strtoupper($this->read_attribute('name')); - } - return $this->read_attribute('name'); } - - public function get_upper_name() - { - return strtoupper($this->name); - } - - public function get_lower_name() - { - return strtolower($this->name); - } } diff --git a/test/models/Venue.php b/test/models/Venue.php index 62812297..1b6e7dc8 100644 --- a/test/models/Venue.php +++ b/test/models/Venue.php @@ -21,10 +21,6 @@ class Venue extends Model public function get_state() { - if (self::$use_custom_get_state_getter) { - return strtolower($this->read_attribute('state')); - } - return $this->read_attribute('state'); } From e1bb78fb9bb3846a521f929ad74c78e720e3c55e Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Thu, 31 Aug 2023 18:22:27 -0400 Subject: [PATCH 2/9] Primary key has to be lowercased too because gettter/setters are lowercased in Model.php in order to be case insensitive. --- lib/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Model.php b/lib/Model.php index 01c52a2d..d88dcdf5 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -916,7 +916,7 @@ private function insert($validate=true) // if we've got an autoincrementing/sequenced pk set it // don't need this check until the day comes that we decide to support composite pks // if (count($pk) == 1) - + $pk = strtolower($pk); $column = $table->get_column_by_inflected_name($pk); if ($column->auto_increment || $use_sequence) { From f6e2981512e7efa09d3fc93edc821244a3736a6b Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Thu, 31 Aug 2023 18:10:55 -0400 Subject: [PATCH 3/9] Fix for issue #35. Corrected ActiveRecordTest's so that capitalization for attribute names are retained. So the short form for New York is now NY instead of ny. test/model/Author, Book, and Venue now have those strtoupper() workarounds in getter/setter methods removed as it's done in Model.php --- lib/Model.php | 3 ++- test/ActiveRecordTest.php | 18 +++++++++--------- test/SerializationTest.php | 4 +--- test/models/Author.php | 1 - test/models/Book.php | 16 +--------------- test/models/Venue.php | 4 ---- 6 files changed, 13 insertions(+), 33 deletions(-) diff --git a/lib/Model.php b/lib/Model.php index d83bffaf..2d83183b 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -385,6 +385,7 @@ public function __construct(array $attributes = [], bool $guard_attributes = tru public function &__get($name) { // check for getter + $name = strtolower($name); if (method_exists($this, "get_$name")) { $name = "get_$name"; $res = call_user_func([$this, $name]); @@ -479,11 +480,11 @@ public function __isset($attribute_name) */ public function __set(string $name, mixed $value): void { + $name = strtolower($name); if (array_key_exists($name, static::$alias_attribute)) { $name = static::$alias_attribute[$name]; } elseif (method_exists($this, "set_$name")) { $name = "set_$name"; - $this->$name($value); return; diff --git a/test/ActiveRecordTest.php b/test/ActiveRecordTest.php index 9b17e070..3870b184 100644 --- a/test/ActiveRecordTest.php +++ b/test/ActiveRecordTest.php @@ -399,8 +399,8 @@ public function test_delegate_returns_null_if_relationship_does_not_exist() public function test_delegate_set_attribute() { $event = Event::first(); - $event->state = 'MEXICO'; - $this->assertEquals('MEXICO', $event->venue->state); + $event->state = 'Mexico'; + $this->assertEquals('Mexico', $event->venue->state); } public function test_delegate_getter_gh_98() @@ -408,8 +408,8 @@ public function test_delegate_getter_gh_98() Venue::$use_custom_get_state_getter = true; $event = Event::first(); - $this->assertEquals('ny', $event->venue->state); - $this->assertEquals('ny', $event->state); + $this->assertEquals('NY', $event->venue->state); + $this->assertEquals('NY', $event->state); Venue::$use_custom_get_state_getter = false; } @@ -419,8 +419,8 @@ public function test_delegate_setter_gh_98() Venue::$use_custom_set_state_setter = true; $event = Event::first(); - $event->state = 'MEXICO'; - $this->assertEquals('MEXICO#', $event->venue->state); + $event->state = 'Mexico'; + $this->assertEquals('Mexico#', $event->venue->state); Venue::$use_custom_set_state_setter = false; } @@ -447,13 +447,13 @@ public function test_setter_with_same_name_as_an_attribute() { $author = new Author(); $author->name = 'bob'; - $this->assertEquals('BOB', $author->name); + $this->assertEquals('bob', $author->name); } public function test_getter() { $book = Book::first(); - $this->assertEquals(strtoupper($book->name), $book->upper_name); + $this->assertEquals($book->name, $book->get_name()); } public function test_getter_with_same_name_as_an_attribute() @@ -461,7 +461,7 @@ public function test_getter_with_same_name_as_an_attribute() Book::$use_custom_get_name_getter = true; $book = new Book(); $book->name = 'bob'; - $this->assertEquals('BOB', $book->name); + $this->assertEquals('bob', $book->name); Book::$use_custom_get_name_getter = false; } diff --git a/test/SerializationTest.php b/test/SerializationTest.php index 6fee91f7..315b2629 100644 --- a/test/SerializationTest.php +++ b/test/SerializationTest.php @@ -36,8 +36,6 @@ public function test_only_should_only_apply_to_attributes() { $this->assertArrayHasKey('author', $this->_a(['only' => 'name', 'include' => 'author'])); $this->assertArrayHasKey('name', $this->_a(['only' => 'name', 'include' => 'author'])); - $this->assertArrayHasKey('book_id', $this->_a(['only' => 'book_id', 'methods' => 'upper_name'])); - $this->assertArrayHasKey('upper_name', $this->_a(['only' => 'book_id', 'methods' => 'upper_name'])); } public function test_only_overrides_except() @@ -73,7 +71,7 @@ public function test_methods_takes_a_string() public function test_methods_method_same_as_attribute() { $a = $this->_a(['methods' => 'name']); - $this->assertEquals('ancient art of main tanking', $a['name']); + $this->assertEquals('Ancient Art of Main Tanking', $a['name']); } public function test_include() diff --git a/test/models/Author.php b/test/models/Author.php index 00bce6dd..9bfcbc3e 100644 --- a/test/models/Author.php +++ b/test/models/Author.php @@ -22,7 +22,6 @@ public function set_password($plaintext) public function set_name($value) { - $value = strtoupper($value); $this->assign_attribute('name', $value); } diff --git a/test/models/Book.php b/test/models/Book.php index 9dd132e4..aeb031c4 100644 --- a/test/models/Book.php +++ b/test/models/Book.php @@ -16,25 +16,11 @@ public function upper_name() public function name() { - return strtolower($this->name); + return $this->name; } public function get_name() { - if (self::$use_custom_get_name_getter) { - return strtoupper($this->read_attribute('name')); - } - return $this->read_attribute('name'); } - - public function get_upper_name() - { - return strtoupper($this->name); - } - - public function get_lower_name() - { - return strtolower($this->name); - } } diff --git a/test/models/Venue.php b/test/models/Venue.php index 20681a75..70fa6f3a 100644 --- a/test/models/Venue.php +++ b/test/models/Venue.php @@ -21,10 +21,6 @@ class Venue extends Model public function get_state() { - if (self::$use_custom_get_state_getter) { - return strtolower($this->read_attribute('state')); - } - return $this->read_attribute('state'); } From 67471f52e9b16670c69ee51b19166b6f02087c9f Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Thu, 31 Aug 2023 18:22:27 -0400 Subject: [PATCH 4/9] Primary key has to be lowercased too because gettter/setters are lowercased in Model.php in order to be case insensitive. --- lib/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Model.php b/lib/Model.php index 2d83183b..2c0cb2c2 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -947,7 +947,7 @@ private function insert($validate = true) // if we've got an autoincrementing/sequenced pk set it // don't need this check until the day comes that we decide to support composite pks // if (count($pk) == 1) - + $pk = strtolower($pk); $column = $table->get_column_by_inflected_name($pk); if ($column->auto_increment || $use_sequence) { From 52478abb38b4715dbddd5fe6a17ffdacd594a099 Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Fri, 1 Sep 2023 22:56:58 -0400 Subject: [PATCH 5/9] Running all the tests may take more than the default of 300 seconds. Disable it so that regression does not timeout. --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a70216b0..1af3bc50 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,10 @@ "scripts": { "style-check" : "php vendor/bin/php-cs-fixer fix --dry-run --verbose --diff", "style-fix" : "php vendor/bin/php-cs-fixer fix --verbose", - "test": "phpunit", + "test": [ + "Composer\\Config::disableProcessTimeout", + "phpunit" + ], "stan": "phpstan analyse --ansi --memory-limit 256M" } } From c32367748f0a62e73ac24a0aba612aa6224734c4 Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Fri, 1 Sep 2023 23:02:11 -0400 Subject: [PATCH 6/9] Write proper tests that demonstrate the fix for issue #35: Authors table now has a column that is camel cased: firstName Added tests that demonstrate case insensitivity when accessing this firstName column --- test/ActiveRecordFindTest.php | 41 ++++++++++++++++++++++++++++++++++- test/ActiveRecordTest.php | 13 +++++++++++ test/fixtures/authors.csv | 10 ++++----- test/sql/mysql.sql | 1 + test/sql/pgsql.sql | 1 + test/sql/sqlite.sql | 1 + 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/test/ActiveRecordFindTest.php b/test/ActiveRecordFindTest.php index 3c6fc848..ef29ec89 100644 --- a/test/ActiveRecordFindTest.php +++ b/test/ActiveRecordFindTest.php @@ -28,23 +28,44 @@ public function test_find_returns_single_model() { $author = Author::find(["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find(["name"=>"Bill Clinton"]); + $author = Author::find(["firstName"=>"Bill"]); + $this->assertInstanceOf(Model::class, $author ); + + $author = Author::find(["FIRSTNAME"=>"Bill"]); $this->assertInstanceOf(Model::class, $author ); $author = Author::find_by_name("Bill Clinton"); $this->assertInstanceOf(Model::class, $author ); + $author = Author::find_by_firstName("Bill"); + $this->assertInstanceOf(Model::class, $author ); + + $author = Author::find_by_FIRSTNAME("Bill"); + $this->assertInstanceOf(Model::class, $author ); + $author = Author::first(); $this->assertInstanceOf(Model::class, $author ); $author = Author::find("first", ["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); + $author = Author::find("first", ["firstName"=>"Bill"]); + $this->assertInstanceOf(Model::class, $author ); + + $author = Author::find("first", ["FIRSTNAME"=>"Bill"]); + $this->assertInstanceOf(Model::class, $author ); + $author = Author::last(); $this->assertInstanceOf(Model::class, $author ); $author = Author::find("last", ["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); + + $author = Author::find("last", ["firstName"=>"Bill"]); + $this->assertInstanceOf(Model::class, $author ); + + $author = Author::find("last", ["FIRSTNAME"=>"Bill"]); + $this->assertInstanceOf(Model::class, $author ); } public function test_find_returns_array_of_models() @@ -58,9 +79,21 @@ public function test_find_returns_array_of_models() $authors = Author::find("all", ["name" => "Bill Clinton"]); $this->assertIsArray($authors); + $authors = Author::find("all", ["firstName" => "Bill"]); + $this->assertIsArray($authors); + + $authors = Author::find("all", ["FIRSTNAME" => "Bill"]); + $this->assertIsArray($authors); + $authors = Author::find_all_by_name("Bill Clinton"); $this->assertIsArray($authors); + $authors = Author::find_all_by_firstName("Bill"); + $this->assertIsArray($authors); + + $authors = Author::find_all_by_FIRSTNAME("Bill"); + $this->assertIsArray($authors); + $authors = Author::find(1,2,3); $this->assertIsArray($authors); @@ -70,6 +103,12 @@ public function test_find_returns_array_of_models() $authors = Author::find(["conditions"=> ["name" => "Bill Clinton"]]); $this->assertIsArray($authors); + $authors = Author::find(["conditions"=> ["firstName" => "Bill"]]); + $this->assertIsArray($authors); + + $authors = Author::find(["conditions"=> ["FIRSTNAME" => "Bill"]]); + $this->assertIsArray($authors); + $authors = Author::find(['conditions'=>["author_id = ?", 3]]); $this->assertIsArray($authors); } diff --git a/test/ActiveRecordTest.php b/test/ActiveRecordTest.php index 384fc63e..42e6a78e 100644 --- a/test/ActiveRecordTest.php +++ b/test/ActiveRecordTest.php @@ -443,6 +443,19 @@ public function test_setter() $this->assertEquals(md5('plaintext'), $author->encrypted_password); } + public function test_case_insensitivity() + { + $author = new Author(); + $author->firstName = 'Peter'; + $this->assertEquals('Peter', $author->firstname); + + $author->FIRSTNAME = 'Paul'; + $this->assertEquals('Paul', $author->firstName); + + $author->FiRsTNAmE = 'Simon'; + $this->assertEquals('Simon', $author->firstNAME); + } + public function test_setter_with_same_name_as_an_attribute() { $author = new Author(); diff --git a/test/fixtures/authors.csv b/test/fixtures/authors.csv index ae219e88..24cbc54e 100644 --- a/test/fixtures/authors.csv +++ b/test/fixtures/authors.csv @@ -1,5 +1,5 @@ -author_id,parent_author_id,name -1,3,"Tito" -2,2,"George W. Bush" -3,1,"Bill Clinton" -4,2,"Uncle Bob" \ No newline at end of file +author_id,parent_author_id,name,firstName +1,3,"Tito","Tito" +2,2,"George W. Bush","George W." +3,1,"Bill Clinton","Bill" +4,2,"Uncle Bob","Uncle Bob" \ No newline at end of file diff --git a/test/sql/mysql.sql b/test/sql/mysql.sql index 493cf803..19df34db 100644 --- a/test/sql/mysql.sql +++ b/test/sql/mysql.sql @@ -2,6 +2,7 @@ CREATE TABLE authors( author_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, parent_author_id INT, name VARCHAR(25) NOT NULL DEFAULT 'default_name', + firstName VARCHAR(25) NOT NULL DEFAULT 'default_name', updated_at datetime, created_at datetime, some_Date date, diff --git a/test/sql/pgsql.sql b/test/sql/pgsql.sql index cd1825ba..6cd7275e 100644 --- a/test/sql/pgsql.sql +++ b/test/sql/pgsql.sql @@ -2,6 +2,7 @@ CREATE TABLE authors( author_id SERIAL PRIMARY KEY, parent_author_id INT, name VARCHAR(25) NOT NULL DEFAULT 'default_name', + firstName VARCHAR(25) NOT NULL DEFAULT 'default_name', updated_at timestamp, created_at timestamp, "some_Date" date, diff --git a/test/sql/sqlite.sql b/test/sql/sqlite.sql index 365dcd3d..6a0649b7 100644 --- a/test/sql/sqlite.sql +++ b/test/sql/sqlite.sql @@ -2,6 +2,7 @@ CREATE TABLE authors( author_id INTEGER NOT NULL PRIMARY KEY, parent_author_id INT, name VARCHAR (25) NOT NULL DEFAULT default_name, -- don't touch those spaces + firstName VARCHAR (25) NOT NULL DEFAULT default_name, updated_at datetime, created_at datetime, some_Date date, From 80ed0523f53ddeeb9aea56a70d26218b28189011 Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Fri, 1 Sep 2023 23:21:34 -0400 Subject: [PATCH 7/9] Switched from firstName field to existing mixedCaseField to pass Postgres regression --- test/ActiveRecordFindTest.php | 28 ++++++++++++++-------------- test/ActiveRecordTest.php | 12 ++++++------ test/fixtures/authors.csv | 2 +- test/sql/mysql.sql | 5 ++--- test/sql/pgsql.sql | 5 ++--- test/sql/sqlite.sql | 5 ++--- 6 files changed, 27 insertions(+), 30 deletions(-) diff --git a/test/ActiveRecordFindTest.php b/test/ActiveRecordFindTest.php index ef29ec89..51b2e119 100644 --- a/test/ActiveRecordFindTest.php +++ b/test/ActiveRecordFindTest.php @@ -28,19 +28,19 @@ public function test_find_returns_single_model() { $author = Author::find(["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find(["firstName"=>"Bill"]); + $author = Author::find(["mixedCaseField"=>"Bill"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find(["FIRSTNAME"=>"Bill"]); + $author = Author::find(["MIXEDCASEFIELD"=>"Bill"]); $this->assertInstanceOf(Model::class, $author ); $author = Author::find_by_name("Bill Clinton"); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find_by_firstName("Bill"); + $author = Author::find_by_mixedCaseField("Bill"); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find_by_FIRSTNAME("Bill"); + $author = Author::find_by_MIXEDCASEFIELD("Bill"); $this->assertInstanceOf(Model::class, $author ); $author = Author::first(); @@ -49,10 +49,10 @@ public function test_find_returns_single_model() { $author = Author::find("first", ["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find("first", ["firstName"=>"Bill"]); + $author = Author::find("first", ["mixedCaseField"=>"Bill"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find("first", ["FIRSTNAME"=>"Bill"]); + $author = Author::find("first", ["MIXEDCASEFIELD"=>"Bill"]); $this->assertInstanceOf(Model::class, $author ); $author = Author::last(); @@ -61,10 +61,10 @@ public function test_find_returns_single_model() { $author = Author::find("last", ["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find("last", ["firstName"=>"Bill"]); + $author = Author::find("last", ["mixedCaseField"=>"Bill"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find("last", ["FIRSTNAME"=>"Bill"]); + $author = Author::find("last", ["MIXEDCASEFIELD"=>"Bill"]); $this->assertInstanceOf(Model::class, $author ); } @@ -79,19 +79,19 @@ public function test_find_returns_array_of_models() $authors = Author::find("all", ["name" => "Bill Clinton"]); $this->assertIsArray($authors); - $authors = Author::find("all", ["firstName" => "Bill"]); + $authors = Author::find("all", ["mixedCaseField" => "Bill"]); $this->assertIsArray($authors); - $authors = Author::find("all", ["FIRSTNAME" => "Bill"]); + $authors = Author::find("all", ["MIXEDCASEFIELD" => "Bill"]); $this->assertIsArray($authors); $authors = Author::find_all_by_name("Bill Clinton"); $this->assertIsArray($authors); - $authors = Author::find_all_by_firstName("Bill"); + $authors = Author::find_all_by_mixedCaseField("Bill"); $this->assertIsArray($authors); - $authors = Author::find_all_by_FIRSTNAME("Bill"); + $authors = Author::find_all_by_MIXEDCASEFIELD("Bill"); $this->assertIsArray($authors); $authors = Author::find(1,2,3); @@ -103,10 +103,10 @@ public function test_find_returns_array_of_models() $authors = Author::find(["conditions"=> ["name" => "Bill Clinton"]]); $this->assertIsArray($authors); - $authors = Author::find(["conditions"=> ["firstName" => "Bill"]]); + $authors = Author::find(["conditions"=> ["mixedCaseField" => "Bill"]]); $this->assertIsArray($authors); - $authors = Author::find(["conditions"=> ["FIRSTNAME" => "Bill"]]); + $authors = Author::find(["conditions"=> ["MIXEDCASEFIELD" => "Bill"]]); $this->assertIsArray($authors); $authors = Author::find(['conditions'=>["author_id = ?", 3]]); diff --git a/test/ActiveRecordTest.php b/test/ActiveRecordTest.php index 42e6a78e..7e6f4805 100644 --- a/test/ActiveRecordTest.php +++ b/test/ActiveRecordTest.php @@ -446,14 +446,14 @@ public function test_setter() public function test_case_insensitivity() { $author = new Author(); - $author->firstName = 'Peter'; - $this->assertEquals('Peter', $author->firstname); + $author->mixedCaseField = 'Peter'; + $this->assertEquals('Peter', $author->mixedcasefield); - $author->FIRSTNAME = 'Paul'; - $this->assertEquals('Paul', $author->firstName); + $author->MIXEDCASEFIELD = 'Paul'; + $this->assertEquals('Paul', $author->mixedCaseFIELD); - $author->FiRsTNAmE = 'Simon'; - $this->assertEquals('Simon', $author->firstNAME); + $author->mixedcasefield = 'Simon'; + $this->assertEquals('Simon', $author->MIXEDCASEFIELD); } public function test_setter_with_same_name_as_an_attribute() diff --git a/test/fixtures/authors.csv b/test/fixtures/authors.csv index 24cbc54e..ea124cf2 100644 --- a/test/fixtures/authors.csv +++ b/test/fixtures/authors.csv @@ -1,4 +1,4 @@ -author_id,parent_author_id,name,firstName +author_id,parent_author_id,name,mixedCaseField 1,3,"Tito","Tito" 2,2,"George W. Bush","George W." 3,1,"Bill Clinton","Bill" diff --git a/test/sql/mysql.sql b/test/sql/mysql.sql index 19df34db..5d9d7023 100644 --- a/test/sql/mysql.sql +++ b/test/sql/mysql.sql @@ -2,15 +2,14 @@ CREATE TABLE authors( author_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, parent_author_id INT, name VARCHAR(25) NOT NULL DEFAULT 'default_name', - firstName VARCHAR(25) NOT NULL DEFAULT 'default_name', + mixedCaseField varchar(50), updated_at datetime, created_at datetime, some_Date date, some_time time, some_text text, some_enum enum('a','b','c'), - encrypted_password varchar(50), - mixedCaseField varchar(50) + encrypted_password varchar(50) ) ENGINE=InnoDB; CREATE TABLE honest_lawyers ( diff --git a/test/sql/pgsql.sql b/test/sql/pgsql.sql index 6cd7275e..3a8d25dc 100644 --- a/test/sql/pgsql.sql +++ b/test/sql/pgsql.sql @@ -2,14 +2,13 @@ CREATE TABLE authors( author_id SERIAL PRIMARY KEY, parent_author_id INT, name VARCHAR(25) NOT NULL DEFAULT 'default_name', - firstName VARCHAR(25) NOT NULL DEFAULT 'default_name', + "mixedCaseField" varchar(50), updated_at timestamp, created_at timestamp, "some_Date" date, some_time time, some_text text, - encrypted_password varchar(50), - "mixedCaseField" varchar(50) + encrypted_password varchar(50) ); CREATE TABLE honest_lawyers ( diff --git a/test/sql/sqlite.sql b/test/sql/sqlite.sql index 6a0649b7..02a7e4fa 100644 --- a/test/sql/sqlite.sql +++ b/test/sql/sqlite.sql @@ -2,14 +2,13 @@ CREATE TABLE authors( author_id INTEGER NOT NULL PRIMARY KEY, parent_author_id INT, name VARCHAR (25) NOT NULL DEFAULT default_name, -- don't touch those spaces - firstName VARCHAR (25) NOT NULL DEFAULT default_name, + mixedCaseField varchar(50), updated_at datetime, created_at datetime, some_Date date, some_time time, some_text text, - encrypted_password varchar(50), - mixedCaseField varchar(50) + encrypted_password varchar(50) ); CREATE TABLE honest_lawyers( From b8ecfc0a906060c97344592e4ac443a8047bfe3b Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Fri, 1 Sep 2023 23:41:47 -0400 Subject: [PATCH 8/9] - List things in alphabetical order - Correct link to contributing.md --- README.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b6d7ec02..bdd59a93 100644 --- a/README.md +++ b/README.md @@ -44,22 +44,21 @@ Of course, there are some differences which will be obvious to the user if they ## Supported Databases ## - MySQL -- SQLite - PostgreSQL +- SQLite ## Features ## -- Finder methods -- Dynamic finder methods -- Writer methods -- Relationships -- Validations - Callbacks +- Model Caching +- Database adapter plugins +- Finder methods, static and dynamic +- Relationships - Serializations (json/xml) - Transactions -- Support for multiple adapters +- Validations +- Writer methods - Miscellaneous options such as: aliased/protected/accessible attributes -- Model Caching ## Installation ## @@ -150,4 +149,4 @@ echo $post->title; # 'New real title' ## Contributing ## -Please refer to [CONTRIBUTING.md](https://github.com/jpfuentes2/php-activerecord/blob/master/CONTRIBUTING.md) for information on how to contribute to PHP ActiveRecord. +Please refer to [CONTRIBUTING.md](https://github.com/php-activerecord/activerecord/blob/master/CONTRIBUTING.md) for information on how to contribute to PHP ActiveRecord. From 23d00a73297700660a94d62e272f1972d7feca6d Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Sat, 2 Sep 2023 14:35:27 -0400 Subject: [PATCH 9/9] Revert updates to test suite as ActiveRecordTest::test_case_insensitivity() is enough. --- test/ActiveRecordFindTest.php | 41 ----------------------------------- test/ActiveRecordTest.php | 14 ++++++------ test/SerializationTest.php | 2 ++ test/models/Author.php | 1 + test/models/Book.php | 8 +++---- test/models/Venue.php | 4 ++++ 6 files changed, 18 insertions(+), 52 deletions(-) diff --git a/test/ActiveRecordFindTest.php b/test/ActiveRecordFindTest.php index 51b2e119..bde65669 100644 --- a/test/ActiveRecordFindTest.php +++ b/test/ActiveRecordFindTest.php @@ -28,44 +28,21 @@ public function test_find_returns_single_model() { $author = Author::find(["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find(["mixedCaseField"=>"Bill"]); - $this->assertInstanceOf(Model::class, $author ); - - $author = Author::find(["MIXEDCASEFIELD"=>"Bill"]); - $this->assertInstanceOf(Model::class, $author ); $author = Author::find_by_name("Bill Clinton"); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find_by_mixedCaseField("Bill"); - $this->assertInstanceOf(Model::class, $author ); - - $author = Author::find_by_MIXEDCASEFIELD("Bill"); - $this->assertInstanceOf(Model::class, $author ); - $author = Author::first(); $this->assertInstanceOf(Model::class, $author ); $author = Author::find("first", ["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); - $author = Author::find("first", ["mixedCaseField"=>"Bill"]); - $this->assertInstanceOf(Model::class, $author ); - - $author = Author::find("first", ["MIXEDCASEFIELD"=>"Bill"]); - $this->assertInstanceOf(Model::class, $author ); - $author = Author::last(); $this->assertInstanceOf(Model::class, $author ); $author = Author::find("last", ["name"=>"Bill Clinton"]); $this->assertInstanceOf(Model::class, $author ); - - $author = Author::find("last", ["mixedCaseField"=>"Bill"]); - $this->assertInstanceOf(Model::class, $author ); - - $author = Author::find("last", ["MIXEDCASEFIELD"=>"Bill"]); - $this->assertInstanceOf(Model::class, $author ); } public function test_find_returns_array_of_models() @@ -79,21 +56,9 @@ public function test_find_returns_array_of_models() $authors = Author::find("all", ["name" => "Bill Clinton"]); $this->assertIsArray($authors); - $authors = Author::find("all", ["mixedCaseField" => "Bill"]); - $this->assertIsArray($authors); - - $authors = Author::find("all", ["MIXEDCASEFIELD" => "Bill"]); - $this->assertIsArray($authors); - $authors = Author::find_all_by_name("Bill Clinton"); $this->assertIsArray($authors); - $authors = Author::find_all_by_mixedCaseField("Bill"); - $this->assertIsArray($authors); - - $authors = Author::find_all_by_MIXEDCASEFIELD("Bill"); - $this->assertIsArray($authors); - $authors = Author::find(1,2,3); $this->assertIsArray($authors); @@ -103,12 +68,6 @@ public function test_find_returns_array_of_models() $authors = Author::find(["conditions"=> ["name" => "Bill Clinton"]]); $this->assertIsArray($authors); - $authors = Author::find(["conditions"=> ["mixedCaseField" => "Bill"]]); - $this->assertIsArray($authors); - - $authors = Author::find(["conditions"=> ["MIXEDCASEFIELD" => "Bill"]]); - $this->assertIsArray($authors); - $authors = Author::find(['conditions'=>["author_id = ?", 3]]); $this->assertIsArray($authors); } diff --git a/test/ActiveRecordTest.php b/test/ActiveRecordTest.php index 7e6f4805..3922f80e 100644 --- a/test/ActiveRecordTest.php +++ b/test/ActiveRecordTest.php @@ -399,8 +399,8 @@ public function test_delegate_returns_null_if_relationship_does_not_exist() public function test_delegate_set_attribute() { $event = Event::first(); - $event->state = 'Mexico'; - $this->assertEquals('Mexico', $event->venue->state); + $event->state = 'MEXICO'; + $this->assertEquals('MEXICO', $event->venue->state); } public function test_delegate_getter_gh_98() @@ -408,8 +408,8 @@ public function test_delegate_getter_gh_98() Venue::$use_custom_get_state_getter = true; $event = Event::first(); - $this->assertEquals('NY', $event->venue->state); - $this->assertEquals('NY', $event->state); + $this->assertEquals('ny', $event->venue->state); + $this->assertEquals('ny', $event->state); Venue::$use_custom_get_state_getter = false; } @@ -419,8 +419,8 @@ public function test_delegate_setter_gh_98() Venue::$use_custom_set_state_setter = true; $event = Event::first(); - $event->state = 'Mexico'; - $this->assertEquals('Mexico#', $event->venue->state); + $event->state = 'MEXICO'; + $this->assertEquals('MEXICO#', $event->venue->state); Venue::$use_custom_set_state_setter = false; } @@ -460,7 +460,7 @@ public function test_setter_with_same_name_as_an_attribute() { $author = new Author(); $author->name = 'bob'; - $this->assertEquals('bob', $author->name); + $this->assertEquals('BOB', $author->name); } public function test_getter() diff --git a/test/SerializationTest.php b/test/SerializationTest.php index fcd3f00c..1012c405 100644 --- a/test/SerializationTest.php +++ b/test/SerializationTest.php @@ -36,6 +36,8 @@ public function test_only_should_only_apply_to_attributes() { $this->assertArrayHasKey('author', $this->_a(['only' => 'name', 'include' => 'author'])); $this->assertArrayHasKey('name', $this->_a(['only' => 'name', 'include' => 'author'])); + $this->assertArrayHasKey('book_id', $this->_a(['only' => 'book_id', 'methods' => 'upper_name'])); + $this->assertArrayHasKey('upper_name', $this->_a(['only' => 'book_id', 'methods' => 'upper_name'])); } public function test_only_overrides_except() diff --git a/test/models/Author.php b/test/models/Author.php index 9bfcbc3e..00bce6dd 100644 --- a/test/models/Author.php +++ b/test/models/Author.php @@ -22,6 +22,7 @@ public function set_password($plaintext) public function set_name($value) { + $value = strtoupper($value); $this->assign_attribute('name', $value); } diff --git a/test/models/Book.php b/test/models/Book.php index e87b5939..8203d7a5 100644 --- a/test/models/Book.php +++ b/test/models/Book.php @@ -9,22 +9,22 @@ class Book extends Model public static $has_one = []; public function upper_name() { - return strtoupper($this->name); // keep? + return strtoupper($this->name); } public function name() { - return strtolower($this->name); // keep + return strtolower($this->name); } public function get_publisher() { - return strtoupper($this->read_attribute('publisher')); // keep + return strtoupper($this->read_attribute('publisher')); } public function get_upper_name() { - return strtoupper($this->name); // keep + return strtoupper($this->name); } public function get_lower_name() diff --git a/test/models/Venue.php b/test/models/Venue.php index 70fa6f3a..20681a75 100644 --- a/test/models/Venue.php +++ b/test/models/Venue.php @@ -21,6 +21,10 @@ class Venue extends Model public function get_state() { + if (self::$use_custom_get_state_getter) { + return strtolower($this->read_attribute('state')); + } + return $this->read_attribute('state'); }