From 08f893e68038f51ca6b028afef83cbe166ed77fc Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Fri, 1 Sep 2023 15:58:22 -0700 Subject: [PATCH] clean up tests and book class in particular --- test/ActiveRecordTest.php | 8 +++----- test/SerializationTest.php | 10 +++++----- test/fixtures/books.csv | 6 +++--- test/models/Book.php | 16 +++++----------- test/sql/mysql.sql | 1 + test/sql/pgsql.sql | 1 + test/sql/sqlite.sql | 1 + 7 files changed, 19 insertions(+), 24 deletions(-) diff --git a/test/ActiveRecordTest.php b/test/ActiveRecordTest.php index 9b17e070..c1c88249 100644 --- a/test/ActiveRecordTest.php +++ b/test/ActiveRecordTest.php @@ -453,16 +453,14 @@ public function test_setter_with_same_name_as_an_attribute() public function test_getter() { $book = Book::first(); - $this->assertEquals(strtoupper($book->name), $book->upper_name); + $this->assertEquals("ANCIENT ART OF MAIN TANKING", $book->upper_name); } 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); - Book::$use_custom_get_name_getter = false; + $book->publisher = 'Random House'; + $this->assertEquals('RANDOM HOUSE', $book->publisher); } public function test_setting_invalid_date_should_set_date_to_null() diff --git a/test/SerializationTest.php b/test/SerializationTest.php index 6fee91f7..1012c405 100644 --- a/test/SerializationTest.php +++ b/test/SerializationTest.php @@ -68,7 +68,7 @@ public function test_methods_takes_a_string() $this->assertEquals('ANCIENT ART OF MAIN TANKING', $a['upper_name']); } - // methods added last should we shuld have value of the method in our json + // methods added last should have value of the method in our json // rather than the regular attribute value public function test_methods_method_same_as_attribute() { @@ -173,13 +173,13 @@ public function test_only_method() public function test_to_csv() { $book = Book::find(1); - $this->assertEquals('1,1,2,"Ancient Art of Main Tanking",0,0', $book->to_csv()); + $this->assertEquals('1,1,2,"Ancient Art of Main Tanking","Random House",0,0', $book->to_csv()); } public function test_to_csv_only_header() { $book = Book::find(1); - $this->assertEquals('book_id,author_id,secondary_author_id,name,numeric_test,special', + $this->assertEquals('book_id,author_id,secondary_author_id,name,publisher,numeric_test,special', $book->to_csv(['only_header'=>true]) ); } @@ -205,7 +205,7 @@ public function test_to_csv_with_custom_delimiter() { $book = Book::find(1); CsvSerializer::$delimiter=';'; - $this->assertEquals('1;1;2;"Ancient Art of Main Tanking";0;0', $book->to_csv()); + $this->assertEquals('1;1;2;"Ancient Art of Main Tanking";"Random House";0;0', $book->to_csv()); } public function test_to_csv_with_custom_enclosure() @@ -213,6 +213,6 @@ public function test_to_csv_with_custom_enclosure() $book = Book::find(1); CsvSerializer::$delimiter=','; CsvSerializer::$enclosure="'"; - $this->assertEquals("1,1,2,'Ancient Art of Main Tanking',0,0", $book->to_csv()); + $this->assertEquals("1,1,2,'Ancient Art of Main Tanking','Random House',0,0", $book->to_csv()); } } diff --git a/test/fixtures/books.csv b/test/fixtures/books.csv index 525afb3c..c510b5d6 100644 --- a/test/fixtures/books.csv +++ b/test/fixtures/books.csv @@ -1,3 +1,3 @@ -book_id,author_id,secondary_author_id,name,special -1,1,2,"Ancient Art of Main Tanking",0 -2,2,2,"Another Book",0 \ No newline at end of file +book_id,author_id,secondary_author_id,name,publisher,special +1,1,2,"Ancient Art of Main Tanking","Random House",0 +2,2,2,"Another Book","Penguin",0 diff --git a/test/models/Book.php b/test/models/Book.php index 9dd132e4..e87b5939 100644 --- a/test/models/Book.php +++ b/test/models/Book.php @@ -7,30 +7,24 @@ class Book extends Model { public static array $belongs_to = ['author']; public static $has_one = []; - public static $use_custom_get_name_getter = false; - public function upper_name() { - return strtoupper($this->name); + return strtoupper($this->name); // keep? } public function name() { - return strtolower($this->name); + return strtolower($this->name); // keep } - public function get_name() + public function get_publisher() { - if (self::$use_custom_get_name_getter) { - return strtoupper($this->read_attribute('name')); - } - - return $this->read_attribute('name'); + return strtoupper($this->read_attribute('publisher')); // keep } public function get_upper_name() { - return strtoupper($this->name); + return strtoupper($this->name); // keep } public function get_lower_name() diff --git a/test/sql/mysql.sql b/test/sql/mysql.sql index 9ee679dd..493cf803 100644 --- a/test/sql/mysql.sql +++ b/test/sql/mysql.sql @@ -22,6 +22,7 @@ CREATE TABLE books( Author_Id INT, secondary_author_id INT, name VARCHAR(50), + publisher VARCHAR(50), numeric_test VARCHAR(10) DEFAULT '0', special NUMERIC(10,2) DEFAULT 0 ); diff --git a/test/sql/pgsql.sql b/test/sql/pgsql.sql index a21191a5..cd1825ba 100644 --- a/test/sql/pgsql.sql +++ b/test/sql/pgsql.sql @@ -21,6 +21,7 @@ CREATE TABLE books( author_id INT, secondary_author_id INT, name VARCHAR(50), + publisher VARCHAR(50), numeric_test VARCHAR(10) DEFAULT '0', special NUMERIC(10,2) DEFAULT 0.0 ); diff --git a/test/sql/sqlite.sql b/test/sql/sqlite.sql index f380bc10..365dcd3d 100644 --- a/test/sql/sqlite.sql +++ b/test/sql/sqlite.sql @@ -21,6 +21,7 @@ CREATE TABLE books( Author_Id INT, secondary_author_id INT, name VARCHAR(50), + publisher VARCHAR(50), numeric_test VARCHAR(10) DEFAULT '0', special NUMERIC(10,2) DEFAULT 0 );