diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 37fe9c9..aecc899 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -21,43 +21,8 @@ - - - tests/ - - - - - tests/ - - - tests/ - - tests/ - - - - tests/ - - - tests/ - - - - - tests/ - - - tests/ - - - tests/ - - - tests/ - diff --git a/tests/DbTest.php b/tests/DbTest.php index cf2dfe8..53c0581 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -15,7 +15,7 @@ #[CoversClass(Db::class)] class DbTest extends TestCase { - protected $object; + protected Db $object; protected function setUp(): void { @@ -53,17 +53,17 @@ public function testFetchingAll(): void public function testFetchingClass(): void { - $line = $this->object->select('*')->from('unit')->fetch('Respect\Relational\testFetchingClass'); - $this->assertInstanceOF('Respect\Relational\testFetchingClass', $line); + $line = $this->object->select('*')->from('unit')->fetch('Respect\Relational\TestFetchingClass'); + $this->assertInstanceOF('Respect\Relational\TestFetchingClass', $line); } public function testFetchingClassArgs(): void { $line = $this->object->select('*')->from('unit')->fetch( - 'Respect\Relational\testFetchingClassArgs', + 'Respect\Relational\TestFetchingClassArgs', ['foo'], ); - $this->assertInstanceOF('Respect\Relational\testFetchingClassArgs', $line); + $this->assertInstanceOF('Respect\Relational\TestFetchingClassArgs', $line); $this->assertEquals('foo', $line->testd); } @@ -81,7 +81,7 @@ static function ($row) { public function testFetchingInto(): void { - $x = new testFetchingInto(); + $x = new TestFetchingInto(); $this->object->select('*')->from('unit')->where(['testb' => 'abc'])->fetch($x); $this->assertEquals('abc', $x->testb); } @@ -94,7 +94,8 @@ public function testRawSql(): void public function testFetchingArray(): void { - $line = $this->object->select('*')->from('unit')->where(['testb' => 'abc'])->fetch(PDO::FETCH_ASSOC); + $line = $this->object->select('*')->from('unit') + ->where(['testb' => 'abc'])->fetch(PDO::FETCH_ASSOC); $this->assertTrue(is_array($line)); } @@ -123,19 +124,27 @@ protected function tearDown(): void } } -class testFetchingClass +class TestFetchingClass { - public $testa, $testb, $testez; + public int|null $testa = null; + + public string|null $testb = null; + + public int|null $testez = null; } -class testFetchingInto +class TestFetchingInto { - public $testa, $testb, $testez; + public int|null $testa = null; + + public string|null $testb = null; + + public int|null $testez = null; } -class testFetchingClassArgs +class TestFetchingClassArgs { - public function __construct(public $testd) + public function __construct(public string|null $testd = null) { } } diff --git a/tests/MapperTest.php b/tests/MapperTest.php index fea367f..5512b15 100644 --- a/tests/MapperTest.php +++ b/tests/MapperTest.php @@ -31,7 +31,27 @@ #[CoversClass(Mapper::class)] class MapperTest extends TestCase { - protected $conn, $mapper, $posts, $authors, $comments, $categories, $postsCategories, $issues; + protected PDO $conn; + + protected Mapper $mapper; + + /** @var list */ + protected array $posts; + + /** @var list */ + protected array $authors; + + /** @var list */ + protected array $comments; + + /** @var list */ + protected array $categories; + + /** @var list */ + protected array $postsCategories; + + /** @var list */ + protected array $issues; protected function setUp(): void { @@ -157,14 +177,14 @@ protected function setUp(): void $this->conn = $conn; } - public function test_creating_with_db_instance(): void + public function testCreatingWithDbInstance(): void { $db = new Db($this->conn); $mapper = new Mapper($db); $this->assertSame($db, $mapper->getDb()); } - public function test_get_defined_db_instance(): void + public function testGetDefinedDbInstance(): void { $db = new Db($this->conn); $mapper = new Mapper($db); @@ -172,13 +192,13 @@ public function test_get_defined_db_instance(): void $this->assertSame($db, $mapper->getDb()); } - public function test_creating_with_invalid_args_should_throw_exception(): void + public function testCreatingWithInvalidArgsShouldThrowException(): void { $this->expectException(TypeError::class); new Mapper('foo'); } - public function test_rolling_back_transaction(): void + public function testRollingBackTransaction(): void { $conn = $this->createMock(PDO::class); $conn->expects($this->any()) @@ -197,7 +217,7 @@ public function test_rolling_back_transaction(): void } } - public function test_ignoring_last_insert_id_errors(): void + public function testIgnoringLastInsertIdErrors(): void { $conn = $this->createStub(PDO::class); $conn->method('getAttribute') @@ -223,7 +243,7 @@ public function test_ignoring_last_insert_id_errors(): void $this->assertEquals('bar', $obj->name); } - public function test_removing_untracked_object(): void + public function testRemovingUntrackedObject(): void { $comment = new stdClass(); $comment->id = 7; @@ -233,35 +253,35 @@ public function test_removing_untracked_object(): void $this->assertEmpty($this->mapper->comment[7]->fetch()); } - public function test_fetching_single_entity_from_collection_should_return_first_record_from_table(): void + public function testFetchingSingleEntityFromCollectionShouldReturnFirstRecordFromTable(): void { $expectedFirstComment = reset($this->comments); $fetchedFirstComment = $this->mapper->comment->fetch(); $this->assertEquals($expectedFirstComment, $fetchedFirstComment); } - public function test_fetching_all_entites_from_collection_should_return_all_records(): void + public function testFetchingAllEntitesFromCollectionShouldReturnAllRecords(): void { $expectedCategories = $this->categories; $fetchedCategories = $this->mapper->category->fetchAll(); $this->assertEquals($expectedCategories, $fetchedCategories); } - public function test_extra_sql_on_single_fetch_should_be_applied_on_mapper_sql(): void + public function testExtraSqlOnSingleFetchShouldBeAppliedOnMapperSql(): void { $expectedLast = end($this->comments); $fetchedLast = $this->mapper->comment->fetch(Sql::orderBy('id DESC')); $this->assertEquals($expectedLast, $fetchedLast); } - public function test_extra_sql_on_fetchAll_should_be_applied_on_mapper_sql(): void + public function testExtraSqlOnFetchAllShouldBeAppliedOnMapperSql(): void { $expectedComments = array_reverse($this->comments); $fetchedComments = $this->mapper->comment->fetchAll(Sql::orderBy('id DESC')); $this->assertEquals($expectedComments, $fetchedComments); } - public function test_nested_collections_should_hydrate_results(): void + public function testNestedCollectionsShouldHydrateResults(): void { $mapper = $this->mapper; $comment = $mapper->comment->post[5]->fetch(); @@ -307,7 +327,7 @@ public function testNtoN(): void $this->assertEquals(4, count(get_object_vars($comment->post_id))); } - public function testNtoNReverse(): void + public function testManyToManyReverse(): void { $mapper = $this->mapper; $cat = $mapper->category->post_category->post[5]->fetch(); @@ -321,7 +341,8 @@ public function testSimplePersist(): void $entity = (object) ['id' => 4, 'name' => 'inserted', 'category_id' => null]; $mapper->category->persist($entity); $mapper->flush(); - $result = $this->conn->query('select * from category where id=4')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select * from category where id=4') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals($entity, $result); } @@ -331,7 +352,8 @@ public function testSimplePersistCollection(): void $entity = (object) ['id' => 4, 'name' => 'inserted', 'category_id' => null]; $mapper->category->persist($entity); $mapper->flush(); - $result = $this->conn->query('select * from category where id=4')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select * from category where id=4') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals($entity, $result); } @@ -348,8 +370,12 @@ public function testNestedPersistCollection(): void ]; $this->mapper->post->author->persist($postWithAuthor); $this->mapper->flush(); - $author = $this->conn->query('select * from author order by id desc limit 1')->fetch(PDO::FETCH_OBJ); - $post = $this->conn->query('select * from post order by id desc limit 1')->fetch(PDO::FETCH_OBJ); + $author = $this->conn->query( + 'select * from author order by id desc limit 1', + )->fetch(PDO::FETCH_OBJ); + $post = $this->conn->query( + 'select * from post order by id desc limit 1', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals('New', $author->name); $this->assertEquals('hi', $post->title); } @@ -368,8 +394,12 @@ public function testNestedPersistCollectionShortcut(): void $this->mapper->postAuthor = $this->mapper->post->author; $this->mapper->postAuthor->persist($postWithAuthor); $this->mapper->flush(); - $author = $this->conn->query('select * from author order by id desc limit 1')->fetch(PDO::FETCH_OBJ); - $post = $this->conn->query('select * from post order by id desc limit 1')->fetch(PDO::FETCH_OBJ); + $author = $this->conn->query( + 'select * from author order by id desc limit 1', + )->fetch(PDO::FETCH_OBJ); + $post = $this->conn->query( + 'select * from post order by id desc limit 1', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals('New', $author->name); $this->assertEquals('hi', $post->title); } @@ -388,8 +418,12 @@ public function testNestedPersistCollectionWithChildrenShortcut(): void $this->mapper->postAuthor = $this->mapper->post($this->mapper->author); $this->mapper->postAuthor->persist($postWithAuthor); $this->mapper->flush(); - $author = $this->conn->query('select * from author order by id desc limit 1')->fetch(PDO::FETCH_OBJ); - $post = $this->conn->query('select * from post order by id desc limit 1')->fetch(PDO::FETCH_OBJ); + $author = $this->conn->query( + 'select * from author order by id desc limit 1', + )->fetch(PDO::FETCH_OBJ); + $post = $this->conn->query( + 'select * from post order by id desc limit 1', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals('New', $author->name); $this->assertEquals('hi', $post->title); } @@ -400,7 +434,8 @@ public function testSubCategory(): void $entity = (object) ['id' => 8, 'name' => 'inserted', 'category_id' => 2]; $mapper->category->persist($entity); $mapper->flush(); - $result = $this->conn->query('select * from category where id=8')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select * from category where id=8') + ->fetch(PDO::FETCH_OBJ); $result2 = $mapper->category[8]->category->fetch(); $this->assertEquals($result->id, $result2->id); $this->assertEquals($result->name, $result2->name); @@ -413,7 +448,8 @@ public function testSubCategoryCondition(): void $entity = (object) ['id' => 8, 'name' => 'inserted', 'category_id' => 2]; $mapper->category->persist($entity); $mapper->flush(); - $result = $this->conn->query('select * from category where id=8')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select * from category where id=8') + ->fetch(PDO::FETCH_OBJ); $result2 = $mapper->category(['id' => 8])->category->fetch(); $this->assertEquals($result->id, $result2->id); $this->assertEquals($result->name, $result2->name); @@ -426,7 +462,9 @@ public function testAutoIncrementPersist(): void $entity = (object) ['id' => null, 'name' => 'inserted', 'category_id' => null]; $mapper->category->persist($entity); $mapper->flush(); - $result = $this->conn->query('select * from category where name="inserted"')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query( + 'select * from category where name="inserted"', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals($entity, $result); $this->assertEquals(4, $result->id); } @@ -453,7 +491,8 @@ public function testPassedIdentity(): void ->query('select id from post where title = 12345') ->fetchColumn(0); - $comment = $this->conn->query('select * from comment where post_id = ' . $postId) + $comment = $this->conn + ->query('select * from comment where post_id = ' . $postId) ->fetchObject(); $this->assertEquals('abc', $comment->text); @@ -466,7 +505,8 @@ public function testJoinedPersist(): void $entity->text = 'HeyHey'; $mapper->comment->persist($entity); $mapper->flush(); - $result = $this->conn->query('select text from comment where id=8')->fetchColumn(0); + $result = $this->conn->query('select text from comment where id=8') + ->fetchColumn(0); $this->assertEquals('HeyHey', $result); } @@ -481,7 +521,7 @@ public function testRemove(): void $this->assertEquals($total, $pre - 1); } - public function test_fetching_entity_typed(): void + public function testFetchingEntityTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -489,7 +529,7 @@ public function test_fetching_entity_typed(): void $this->assertInstanceOf('\Respect\Relational\Comment', $comment); } - public function test_fetching_all_entity_typed(): void + public function testFetchingAllEntityTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -497,7 +537,7 @@ public function test_fetching_all_entity_typed(): void $this->assertInstanceOf('\Respect\Relational\Comment', $comment[1]); } - public function test_fetching_all_entity_typed_nested(): void + public function testFetchingAllEntityTypedNested(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -506,7 +546,7 @@ public function test_fetching_all_entity_typed_nested(): void $this->assertInstanceOf('\Respect\Relational\Post', $comment[0]->post_id); } - public function test_persisting_entity_typed(): void + public function testPersistingEntityTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -514,11 +554,12 @@ public function test_persisting_entity_typed(): void $comment->text = 'HeyHey'; $mapper->comment->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comment where id=8')->fetchColumn(0); + $result = $this->conn->query('select text from comment where id=8') + ->fetchColumn(0); $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed(): void + public function testPersistingNewEntityTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -526,11 +567,12 @@ public function test_persisting_new_entity_typed(): void $comment->text = 'HeyHey'; $mapper->comment->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comment where id=9')->fetchColumn(0); + $result = $this->conn->query('select text from comment where id=9') + ->fetchColumn(0); $this->assertEquals('HeyHey', $result); } - public function test_setters_and_getters_datetime_as_object(): void + public function testSettersAndGettersDatetimeAsObject(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -546,10 +588,16 @@ public function test_setters_and_getters_datetime_as_object(): void $this->assertEquals(date('Y-m-d'), $result->getDatetime()->format('Y-m-d')); } - public function test_style(): void + public function testStyle(): void { - $this->assertInstanceOf('Respect\Data\Styles\Stylable', $this->mapper->getStyle()); - $this->assertInstanceOf('Respect\Data\Styles\Standard', $this->mapper->getStyle()); + $this->assertInstanceOf( + 'Respect\Data\Styles\Stylable', + $this->mapper->getStyle(), + ); + $this->assertInstanceOf( + 'Respect\Data\Styles\Standard', + $this->mapper->getStyle(), + ); $styles = [ new Styles\CakePHP(), new Styles\NorthWind(), @@ -562,7 +610,7 @@ public function test_style(): void } } - public function test_feching_a_single_filtered_collection_should_not_bring_filtered_children(): void + public function testFetchingaSingleFilteredCollectionShouldNotBringFilteredChildren(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); @@ -570,7 +618,7 @@ public function test_feching_a_single_filtered_collection_should_not_bring_filte $this->assertEquals($this->authors[0], $author); } - public function test_persisting_a_previously_fetched_filtered_entity_back_into_its_collection(): void + public function testPersistingaPreviouslyFetchedFilteredEntityBackIntoItsCollection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); @@ -578,11 +626,12 @@ public function test_persisting_a_previously_fetched_filtered_entity_back_into_i $author->name = 'Author Changed'; $mapper->authorsWithPosts->persist($author); $mapper->flush(); - $result = $this->conn->query('select name from author where id=1')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select name from author where id=1') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Author Changed', $result->name); } - public function test_persisting_a_previously_fetched_filtered_entity_back_into_a_foreign_compatible_collection(): void + public function testPersistingaPreviouslyFetchedFilteredEntityBackIntoaForeignCompatibleCollection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); @@ -590,11 +639,12 @@ public function test_persisting_a_previously_fetched_filtered_entity_back_into_a $author->name = 'Author Changed'; $mapper->author->persist($author); $mapper->flush(); - $result = $this->conn->query('select name from author where id=1')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select name from author where id=1') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Author Changed', $result->name); } - public function test_persisting_a_newly_created_filtered_entity_into_its_collection(): void + public function testPersistingaNewlyCreatedFilteredEntityIntoItsCollection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); @@ -603,11 +653,13 @@ public function test_persisting_a_newly_created_filtered_entity_into_its_collect $author->name = 'Author Changed'; $mapper->authorsWithPosts->persist($author); $mapper->flush(); - $result = $this->conn->query('select name from author order by id desc')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query( + 'select name from author order by id desc', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals('Author Changed', $result->name); } - public function test_persisting_a_newly_created_filtered_entity_into_a_foreig_compatible_collection(): void + public function testPersistingaNewlyCreatedFilteredEntityIntoaForeignCompatibleCollection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); @@ -616,11 +668,13 @@ public function test_persisting_a_newly_created_filtered_entity_into_a_foreig_co $author->name = 'Author Changed'; $mapper->author->persist($author); $mapper->flush(); - $result = $this->conn->query('select name from author order by id desc')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query( + 'select name from author order by id desc', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals('Author Changed', $result->name); } - public function test_feching_multiple_filtered_collections_should_not_bring_filtered_children(): void + public function testFechingMultipleFilteredCollectionsShouldNotBringFilteredChildren(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); @@ -628,142 +682,182 @@ public function test_feching_multiple_filtered_collections_should_not_bring_filt $this->assertEquals($this->authors, $authors); } - public function test_filtered_collections_should_hydrate_non_filtered_parts_as_usual(): void + public function testFilteredCollectionsShouldHydrateNonFilteredPartsAsUsual(): void { $mapper = $this->mapper; $mapper->postsFromAuthorsWithComments = Filtered::comment()->post()->author(); $post = $mapper->postsFromAuthorsWithComments->fetch(); - $this->assertEquals((object) (['author_id' => $post->author_id] + (array) $this->posts[0]), $post); + $this->assertEquals( + (object) (['author_id' => $post->author_id] + (array) $this->posts[0]), + $post, + ); $this->assertEquals($this->authors[0], $post->author_id); } - public function test_filtered_collections_should_persist_hydrated_non_filtered_parts_as_usual(): void + public function testFilteredCollectionsShouldPersistHydratedNonFilteredPartsAsUsual(): void { $mapper = $this->mapper; $mapper->postsFromAuthorsWithComments = Filtered::comment()->post()->author(); $post = $mapper->postsFromAuthorsWithComments->fetch(); - $this->assertEquals((object) (['author_id' => $post->author_id] + (array) $this->posts[0]), $post); + $this->assertEquals( + (object) (['author_id' => $post->author_id] + (array) $this->posts[0]), + $post, + ); $this->assertEquals($this->authors[0], $post->author_id); $post->title = 'Title Changed'; $post->author_id->name = 'John'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); - $result = $this->conn->query('select name from author where id=1')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select name from author where id=1') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('John', $result->name); } - public function test_multiple_filtered_collections_dont_persist(): void + public function testMultipleFilteredCollectionsDontPersist(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::comment()->post->stack(Filtered::author()); $post = $mapper->authorsWithPosts->fetch(); - $this->assertEquals((object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], $post); + $this->assertEquals( + (object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], + $post, + ); $post->title = 'Title Changed'; $post->author_id = $mapper->author[1]->fetch(); $post->author_id->name = 'A'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); - $result = $this->conn->query('select name from author where id=1')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select name from author where id=1') + ->fetch(PDO::FETCH_OBJ); $this->assertNotEquals('A', $result->name); } - public function test_multiple_filtered_collections_dont_persist_newly_create_objects(): void + public function testMultipleFilteredCollectionsDontPersistNewlyCreateObjects(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::comment()->post->stack(Filtered::author()); $post = $mapper->authorsWithPosts->fetch(); - $this->assertEquals((object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], $post); + $this->assertEquals( + (object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], + $post, + ); $post->title = 'Title Changed'; $post->author_id = new stdClass(); $post->author_id->id = null; $post->author_id->name = 'A'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); - $result = $this->conn->query('select name from author order by id desc')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query( + 'select name from author order by id desc', + )->fetch(PDO::FETCH_OBJ); $this->assertNotEquals('A', $result->name); } - public function test_multiple_filtered_collections_fetch_at_once_dont_persist(): void + public function testMultipleFilteredCollectionsFetchAtOnceDontPersist(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::comment()->post->stack(Filtered::author()); $post = $mapper->authorsWithPosts->fetchAll(); $post = $post[0]; - $this->assertEquals((object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], $post); + $this->assertEquals( + (object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], + $post, + ); $post->title = 'Title Changed'; $post->author_id = $mapper->author[1]->fetch(); $post->author_id->name = 'A'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); - $result = $this->conn->query('select name from author where id=1')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select name from author where id=1') + ->fetch(PDO::FETCH_OBJ); $this->assertNotEquals('A', $result->name); } - public function test_reusing_registered_filtered_collections_keeps_their_filtering(): void + public function testReusingRegisteredFilteredCollectionsKeepsTheirFiltering(): void { $mapper = $this->mapper; $mapper->commentFil = Filtered::comment(); $mapper->author = Filtered::author(); $post = $mapper->commentFil->post->author->fetch(); - $this->assertEquals((object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], $post); + $this->assertEquals( + (object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], + $post, + ); $post->title = 'Title Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_reusing_registered_filtered_collections_keeps_their_filtering_on_fetchAll(): void + public function testReusingRegisteredFilteredCollectionsKeepsTheirFilteringOnFetchAll(): void { $mapper = $this->mapper; $mapper->commentFil = Filtered::comment(); $mapper->author = Filtered::author(); $post = $mapper->commentFil->post->author->fetchAll(); $post = $post[0]; - $this->assertEquals((object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], $post); + $this->assertEquals( + (object) ['id' => '5', 'author_id' => 1, 'text' => 'Post Text', 'title' => 'Post Title'], + $post, + ); $post->title = 'Title Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_registered_filtered_collections_by_column_keeps_their_filtering(): void + public function testRegisteredFilteredCollectionsByColumnKeepsTheirFiltering(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('title')->post(); $post = $mapper->post->fetch(); - $this->assertEquals((object) ['id' => '5', 'title' => 'Post Title'], $post); + $this->assertEquals( + (object) ['id' => '5', 'title' => 'Post Title'], + $post, + ); $post->title = 'Title Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_registered_filtered_collections_by_column_keeps_their_filtering_on_fetchAll(): void + public function testRegisteredFilteredCollectionsByColumnKeepsTheirFilteringOnFetchAll(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('title')->post(); $post = $mapper->post->fetchAll(); $post = $post[0]; - $this->assertEquals((object) ['id' => '5', 'title' => 'Post Title'], $post); + $this->assertEquals( + (object) ['id' => '5', 'title' => 'Post Title'], + $post, + ); $post->title = 'Title Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_registered_filtered_wildcard_collections_keeps_their_filtering(): void + public function testRegisteredFilteredWildcardCollectionsKeepsTheirFiltering(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('*')->post(); @@ -772,11 +866,12 @@ public function test_registered_filtered_wildcard_collections_keeps_their_filter $post->title = 'Title Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_registered_filtered_wildcard_collections_keeps_their_filtering_on_fetchAll(): void + public function testRegisteredFilteredWildcardCollectionsKeepsTheirFilteringOnFetchAll(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('*')->post(); @@ -786,52 +881,86 @@ public function test_registered_filtered_wildcard_collections_keeps_their_filter $post->title = 'Title Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_fetching_registered_filtered_collections_alongside_normal(): void + public function testFetchingRegisteredFilteredCollectionsAlongsideNormal(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('*')->post()->author(); $post = $mapper->post->fetchAll(); $post = $post[0]; - $this->assertEquals((object) ['id' => '5', 'author_id' => $post->author_id], $post); - $this->assertEquals((object) ['name' => 'Author 1', 'id' => 1], $post->author_id); + $this->assertEquals( + (object) ['id' => '5', 'author_id' => $post->author_id], + $post, + ); + $this->assertEquals( + (object) ['name' => 'Author 1', 'id' => 1], + $post->author_id, + ); $post->title = 'Title Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_mixins_bring_results_from_two_tables(): void + public function testMixinsBringResultsFromTwoTables(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(['comment' => ['text']])->post()->author(); $post = $mapper->postComment->fetch(); - $this->assertEquals((object) ['name' => 'Author 1', 'id' => 1], $post->author_id); - $this->assertEquals((object) ['id' => '5', 'author_id' => $post->author_id, 'text' => 'Comment Text', 'title' => 'Post Title', 'comment_id' => 7], $post); + $this->assertEquals( + (object) ['name' => 'Author 1', 'id' => 1], + $post->author_id, + ); + $this->assertEquals( + (object) [ + 'id' => '5', + 'author_id' => $post->author_id, + 'text' => 'Comment Text', + 'title' => 'Post Title', + 'comment_id' => 7, + ], + $post, + ); } - public function test_mixins_persists_results_on_two_tables(): void + public function testMixinsPersistsResultsOnTwoTables(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(['comment' => ['text']])->post()->author(); $post = $mapper->postComment->fetch(); - $this->assertEquals((object) ['name' => 'Author 1', 'id' => 1], $post->author_id); - $this->assertEquals((object) ['id' => '5', 'author_id' => $post->author_id, 'text' => 'Comment Text', 'title' => 'Post Title', 'comment_id' => 7], $post); + $this->assertEquals( + (object) ['name' => 'Author 1', 'id' => 1], + $post->author_id, + ); + $this->assertEquals( + (object) [ + 'id' => '5', + 'author_id' => $post->author_id, + 'text' => 'Comment Text', + 'title' => 'Post Title', + 'comment_id' => 7, + ], + $post, + ); $post->title = 'Title Changed'; $post->text = 'Comment Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); - $result = $this->conn->query('select text from comment where id=7')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select text from comment where id=7') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Comment Changed', $result->text); } - public function test_mixins_persists_newly_created_entities_on_two_tables(): void + public function testMixinsPersistsNewlyCreatedEntitiesOnTwoTables(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(['comment' => ['text']])->post()->author(); @@ -839,32 +968,50 @@ public function test_mixins_persists_newly_created_entities_on_two_tables(): voi $post->author_id = (object) ['name' => 'Author X', 'id' => null]; $mapper->postComment->persist($post); $mapper->flush(); - $result = $this->conn->query('select title, text from post order by id desc')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query( + 'select title, text from post order by id desc', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals('Post X', $result->title); $this->assertEquals('', $result->text); - $result = $this->conn->query('select text from comment order by id desc')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query( + 'select text from comment order by id desc', + )->fetch(PDO::FETCH_OBJ); $this->assertEquals('Comment X', $result->text); } - public function test_mixins_all(): void + public function testMixinsAll(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(['comment' => ['text']])->post()->author(); $post = $mapper->postComment->fetchAll(); $post = $post[0]; - $this->assertEquals((object) ['name' => 'Author 1', 'id' => 1], $post->author_id); - $this->assertEquals((object) ['id' => '5', 'author_id' => $post->author_id, 'text' => 'Comment Text', 'title' => 'Post Title', 'comment_id' => 7], $post); + $this->assertEquals( + (object) ['name' => 'Author 1', 'id' => 1], + $post->author_id, + ); + $this->assertEquals( + (object) [ + 'id' => '5', + 'author_id' => $post->author_id, + 'text' => 'Comment Text', + 'title' => 'Post Title', + 'comment_id' => 7, + ], + $post, + ); $post->title = 'Title Changed'; $post->text = 'Comment Changed'; $mapper->postsFromAuthorsWithComments->persist($post); $mapper->flush(); - $result = $this->conn->query('select title from post where id=5')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from post where id=5') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); - $result = $this->conn->query('select text from comment where id=7')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select text from comment where id=7') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Comment Changed', $result->text); } - public function test_typed(): void + public function testTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -877,11 +1024,12 @@ public function test_typed(): void $issues[0]->title = 'Title Changed'; $mapper->typedIssues->persist($issues[0]); $mapper->flush(); - $result = $this->conn->query('select title from issues where id=1')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from issues where id=1') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_typed_single(): void + public function testTypedSingle(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -892,18 +1040,20 @@ public function test_typed_single(): void $issue->title = 'Title Changed'; $mapper->typedIssues->persist($issue); $mapper->flush(); - $result = $this->conn->query('select title from issues where id=1')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select title from issues where id=1') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('Title Changed', $result->title); } - public function test_persist_new_with_arrayobject(): void + public function testPersistNewWithArrayobject(): void { $mapper = $this->mapper; $arrayEntity = ['id' => 10, 'name' => 'array_object_category', 'category_id' => null]; $entity = (object) $arrayEntity; $mapper->category->persist($entity); $mapper->flush(); - $result = $this->conn->query('select * from category where id=10')->fetch(PDO::FETCH_OBJ); + $result = $this->conn->query('select * from category where id=10') + ->fetch(PDO::FETCH_OBJ); $this->assertEquals('array_object_category', $result->name); } @@ -929,7 +1079,10 @@ public function testFetchingAllEntityWithoutPublicPropertiesTypedNested(): void $mapper->entityNamespace = '\Respect\Relational\OtherEntity\\'; $posts = $mapper->post->author->fetchAll(); $this->assertInstanceOf('\Respect\Relational\OtherEntity\Post', $posts[0]); - $this->assertInstanceOf('\Respect\Relational\OtherEntity\Author', $posts[0]->getAuthor()); + $this->assertInstanceOf( + '\Respect\Relational\OtherEntity\Author', + $posts[0]->getAuthor(), + ); } public function testPersistingEntityWithoutPublicPropertiesTyped(): void @@ -942,7 +1095,8 @@ public function testPersistingEntityWithoutPublicPropertiesTyped(): void $mapper->post->persist($post); $mapper->flush(); - $result = $this->conn->query('select text from post where id=5')->fetchColumn(0); + $result = $this->conn->query('select text from post where id=5') + ->fetchColumn(0); $this->assertEquals('HeyHey', $result); } @@ -961,7 +1115,8 @@ public function testPersistingNewEntityWithoutPublicPropertiesTyped(): void $post->setText('My new Post Text'); $mapper->post->persist($post); $mapper->flush(); - $result = $this->conn->query('select text from post where id=6')->fetchColumn(0); + $result = $this->conn->query('select text from post where id=6') + ->fetchColumn(0); $this->assertEquals('My new Post Text', $result); } @@ -984,36 +1139,52 @@ public function testShouldNotExecuteEntityConstructorWhenDisabled(): void $mapper->entityNamespace = 'Respect\\Relational\\OtherEntity\\'; $mapper->disableEntityConstructor = true; - $this->assertInstanceOf('Respect\\Relational\\OtherEntity\\Comment', $mapper->comment->fetch()); + $this->assertInstanceOf( + 'Respect\\Relational\\OtherEntity\\Comment', + $mapper->comment->fetch(), + ); } } class Postcomment { - public $id = null; + public int|null $id = null; } class Bug { - public $id = null, $title, $type; + public int|null $id = null; + + public string|null $title = null; + + public string|null $type = null; } + class Improvement { - public $id = null, $title, $type; + public int|null $id = null; + + public string|null $title = null; + + public string|null $type = null; } class Comment { - public $id = null, $post_id = null, $text = null; + public mixed $id = null; - private $datetime; + public mixed $post_id = null; + + public string|null $text = null; + + private string|null $datetime = null; public function setDatetime(Datetime $datetime): void { $this->datetime = $datetime->format('Y-m-d H:i:s'); } - public function getDatetime() + public function getDatetime(): Datetime { return new Datetime($this->datetime); } @@ -1021,17 +1192,23 @@ public function getDatetime() class Post { - public $id = null, $author_id = null, $text = null, $title = null; + public mixed $id = null; + + public mixed $author_id = null; + + public string|null $text = null; + + public string|null $title = null; /** @Relational\isNotColumn -> annotation because generate a sql error case column not exists in db. */ - private $datetime = ''; + private string $datetime = ''; public function setDatetime(Datetime $datetime): void { $this->datetime = $datetime->format('Y-m-d H:i:s'); } - public function getDatetime() + public function getDatetime(): Datetime { return new Datetime($this->datetime); } @@ -1043,34 +1220,40 @@ public function getDatetime() class Post { - private $id, $author_id, $title, $text; + private mixed $id = null; - public function getTitle() + private mixed $author_id = null; + + private mixed $title = null; + + private mixed $text = null; + + public function getTitle(): mixed { return $this->title; } - public function setTitle($title): void + public function setTitle(mixed $title): void { $this->title = $title; } - public function getId() + public function getId(): mixed { return $this->id; } - public function getAuthor() + public function getAuthor(): mixed { return $this->author_id; } - public function getText() + public function getText(): mixed { return $this->text; } - public function setId($id): void + public function setId(mixed $id): void { $this->id = $id; } @@ -1080,7 +1263,7 @@ public function setAuthor(Author $author): void $this->author_id = $author; } - public function setText($text): void + public function setText(mixed $text): void { $this->text = $text; } @@ -1088,24 +1271,26 @@ public function setText($text): void class Author { - private $id, $name; + private mixed $id = null; + + private mixed $name = null; - public function getId() + public function getId(): mixed { return $this->id; } - public function getName() + public function getName(): mixed { return $this->name; } - public function setId($id): void + public function setId(mixed $id): void { $this->id = $id; } - public function setName($name): void + public function setName(mixed $name): void { $this->name = $name; } @@ -1114,13 +1299,13 @@ public function setName($name): void class Comment { - public $id = null; + public int|null $id = null; - public $post_id = null; + public int|null $post_id = null; - public $text = null; + public string|null $text = null; - public $datetime = null; + public string|null $datetime = null; public function __construct() { diff --git a/tests/SqlTest.php b/tests/SqlTest.php index bd3b4a7..8dfed0d 100644 --- a/tests/SqlTest.php +++ b/tests/SqlTest.php @@ -14,7 +14,7 @@ #[CoversClass(Sql::class)] class SqlTest extends TestCase { - protected $object; + protected Sql $object; protected function setUp(): void { @@ -56,14 +56,24 @@ public function testSelectTables(): void public function testSelectInnerJoin(): void { - $sql = (string) $this->object->select('*')->from('table')->innerJoin('other_table')->on('table.column = other_table.other_column'); - $this->assertEquals('SELECT * FROM table INNER JOIN other_table ON table.column = other_table.other_column', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->innerJoin('other_table') + ->on('table.column = other_table.other_column'); + $this->assertEquals( + 'SELECT * FROM table INNER JOIN other_table ON table.column = other_table.other_column', + $sql, + ); } public function testSelectInnerJoinArr(): void { - $sql = (string) $this->object->select('*')->from('table')->innerJoin('other_table')->on(['table.column' => 'other_table.other_column']); - $this->assertEquals('SELECT * FROM table INNER JOIN other_table ON table.column = other_table.other_column', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->innerJoin('other_table') + ->on(['table.column' => 'other_table.other_column']); + $this->assertEquals( + 'SELECT * FROM table INNER JOIN other_table ON table.column = other_table.other_column', + $sql, + ); } public function testSelectWhere(): void @@ -90,7 +100,10 @@ public function testSelectWhereArray(): void { $data = ['column' => '123', 'other_column' => '456']; $sql = (string) $this->object->select('*')->from('table')->where($data); - $this->assertEquals('SELECT * FROM table WHERE column = ? AND other_column = ?', $sql); + $this->assertEquals( + 'SELECT * FROM table WHERE column = ? AND other_column = ?', + $sql, + ); $this->assertEquals(array_values($data), $this->object->getParams()); } @@ -98,7 +111,10 @@ public function testSelectWhereArrayEmptyAnd(): void { $data = ['column' => '123', 'other_column' => '456']; $sql = (string) $this->object->select('*')->from('table')->where($data)->and(); - $this->assertEquals('SELECT * FROM table WHERE column = ? AND other_column = ?', $sql); + $this->assertEquals( + 'SELECT * FROM table WHERE column = ? AND other_column = ?', + $sql, + ); $this->assertEquals(array_values($data), $this->object->getParams()); } @@ -107,15 +123,24 @@ public function testSelectWhereOr(): void $data = ['column' => '123']; $data2 = ['other_column' => '456']; $sql = (string) $this->object->select('*')->from('table')->where($data)->or($data2); - $this->assertEquals('SELECT * FROM table WHERE column = ? OR other_column = ?', $sql); - $this->assertEquals(array_values(array_merge($data, $data2)), $this->object->getParams()); + $this->assertEquals( + 'SELECT * FROM table WHERE column = ? OR other_column = ?', + $sql, + ); + $this->assertEquals( + array_values(array_merge($data, $data2)), + $this->object->getParams(), + ); } public function testSelectWhereArrayQualifiedNames(): void { $data = ['a.column' => '123', 'b.other_column' => '456']; $sql = (string) $this->object->select('*')->from('table a', 'other_table b')->where($data); - $this->assertEquals('SELECT * FROM table a, other_table b WHERE a.column = ? AND b.other_column = ?', $sql); + $this->assertEquals( + 'SELECT * FROM table a, other_table b WHERE a.column = ? AND b.other_column = ?', + $sql, + ); $this->assertEquals(array_values($data), $this->object->getParams()); } @@ -128,8 +153,13 @@ public function testSelectGroupBy(): void public function testSelectGroupByHaving(): void { $condition = ['other_column' => 456, 'yet_another_column' => 567]; - $sql = (string) $this->object->select('*')->from('table')->groupBy('column', 'other_column')->having($condition); - $this->assertEquals('SELECT * FROM table GROUP BY column, other_column HAVING other_column = ? AND yet_another_column = ?', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->groupBy('column', 'other_column')->having($condition); + $this->assertEquals( + 'SELECT * FROM table GROUP BY column, other_column' + . ' HAVING other_column = ? AND yet_another_column = ?', + $sql, + ); $this->assertEquals(array_values($condition), $this->object->getParams()); } @@ -138,8 +168,15 @@ public function testSimpleUpdate(): void $data = ['column' => 123, 'column_2' => 234]; $condition = ['other_column' => 456, 'yet_another_column' => 567]; $sql = (string) $this->object->update('table')->set($data)->where($condition); - $this->assertEquals('UPDATE table SET column = ?, column_2 = ? WHERE other_column = ? AND yet_another_column = ?', $sql); - $this->assertEquals(array_values(array_merge($data, $condition)), $this->object->getParams()); + $this->assertEquals( + 'UPDATE table SET column = ?, column_2 = ?' + . ' WHERE other_column = ? AND yet_another_column = ?', + $sql, + ); + $this->assertEquals( + array_values(array_merge($data, $condition)), + $this->object->getParams(), + ); } public function testSimpleInsert(): void @@ -154,7 +191,10 @@ public function testSimpleDelete(): void { $condition = ['other_column' => 456, 'yet_another_column' => 567]; $sql = (string) $this->object->deleteFrom('table')->where($condition); - $this->assertEquals('DELETE FROM table WHERE other_column = ? AND yet_another_column = ?', $sql); + $this->assertEquals( + 'DELETE FROM table WHERE other_column = ? AND yet_another_column = ?', + $sql, + ); $this->assertEquals(array_values($condition), $this->object->getParams()); } @@ -166,7 +206,10 @@ public function testCreateTable(): void 'yet_another_column TEXT', ]; $sql = (string) $this->object->createTable('table', $columns); - $this->assertEquals('CREATE TABLE table (column INT, other_column VARCHAR(255), yet_another_column TEXT)', $sql); + $this->assertEquals( + 'CREATE TABLE table (column INT, other_column VARCHAR(255), yet_another_column TEXT)', + $sql, + ); } public function testAlterTable(): void @@ -177,7 +220,10 @@ public function testAlterTable(): void 'ADD yet_another_column TEXT', ]; $sql = (string) $this->object->alterTable('table', $columns); - $this->assertEquals('ALTER TABLE table ADD column INT, ADD other_column VARCHAR(255), ADD yet_another_column TEXT', $sql); + $this->assertEquals( + 'ALTER TABLE table ADD column INT, ADD other_column VARCHAR(255), ADD yet_another_column TEXT', + $sql, + ); } public function testGrant(): void @@ -195,8 +241,12 @@ public function testRevoke(): void public function testComplexFunctions(): void { $condition = ["AES_DECRYPT('pass', 'salt')" => 123]; - $sql = (string) $this->object->select('column', 'COUNT(column)', 'other_column')->from('table')->where($condition); - $this->assertEquals("SELECT column, COUNT(column), other_column FROM table WHERE AES_DECRYPT('pass', 'salt') = ?", $sql); + $sql = (string) $this->object->select('column', 'COUNT(column)', 'other_column') + ->from('table')->where($condition); + $this->assertEquals( + "SELECT column, COUNT(column), other_column FROM table WHERE AES_DECRYPT('pass', 'salt') = ?", + $sql, + ); $this->assertEquals(array_values($condition), $this->object->getParams()); } @@ -205,9 +255,17 @@ public function testAggregateFunctions(): void { $where = ['abc' => 10]; $having = ['SUM(abc) >=' => '10', 'AVG(def) =' => 15]; - $sql = (string) $this->object->select('column', 'MAX(def)')->from('table')->where($where)->groupBy('abc', 'def')->having($having); - $this->assertEquals('SELECT column, MAX(def) FROM table WHERE abc = ? GROUP BY abc, def HAVING SUM(abc) >= ? AND AVG(def) = ?', $sql); - $this->assertEquals(array_values(array_merge($where, $having)), $this->object->getParams()); + $sql = (string) $this->object->select('column', 'MAX(def)')->from('table') + ->where($where)->groupBy('abc', 'def')->having($having); + $this->assertEquals( + 'SELECT column, MAX(def) FROM table WHERE abc = ?' + . ' GROUP BY abc, def HAVING SUM(abc) >= ? AND AVG(def) = ?', + $sql, + ); + $this->assertEquals( + array_values(array_merge($where, $having)), + $this->object->getParams(), + ); } public function testStaticBuilderCall(): void @@ -226,7 +284,8 @@ public function testLastParameterWithoutParts(): void ); } - public static function provider_sql_operators(): array + /** @return array> */ + public static function providerSqlOperators(): array { // operator, expectedWhere return [ @@ -243,8 +302,8 @@ public static function provider_sql_operators(): array } /** @ticket 13 */ - #[DataProvider('provider_sql_operators')] - public function test_sql_operators($operator, $expected = null): void + #[DataProvider('providerSqlOperators')] + public function testSqlOperators(string $operator, string|null $expected = null): void { $expected = $expected ?: ' ?'; $where = ['id ' . $operator => 10]; @@ -279,7 +338,10 @@ public function testSetQueryWithParamsViaConstructor(): void public function testAppendQueryWithParams(): void { $query = 'SELECT * FROM table WHERE a > ? AND b = ?'; - $this->object->setQuery('SELECT * FROM table WHERE a > ? AND b = ?', [1, 'foo']); + $this->object->setQuery( + 'SELECT * FROM table WHERE a > ? AND b = ?', + [1, 'foo'], + ); $sql = (string) $this->object->appendQuery('AND c = ?', [2]); $this->assertEquals($query . ' AND c = ?', $sql); @@ -292,8 +354,12 @@ public function testSelectWhereWithRepeatedReferences(): void $data2 = ['a >' => 4]; $data3 = ['b' => 'bar']; - $sql = (string) $this->object->select('*')->from('table')->where($data1)->or($data2)->and($data3); - $this->assertEquals('SELECT * FROM table WHERE a > ? AND b = ? OR a > ? AND b = ?', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->where($data1)->or($data2)->and($data3); + $this->assertEquals( + 'SELECT * FROM table WHERE a > ? AND b = ? OR a > ? AND b = ?', + $sql, + ); $this->assertEquals([1, 'foo', 4, 'bar'], $this->object->getParams()); } @@ -301,18 +367,31 @@ public function testSelectWhereWithConditionsGroupedByUnderscores(): void { $data = [['a' => 1], ['b' => 2], ['c' => 3], ['d' => 4]]; - $sql = (string) $this->object->select('*')->from('table')->where($data[0])->and_($data[1])->or($data[2])->_(); - $this->assertEquals('SELECT * FROM table WHERE a = ? AND (b = ? OR c = ?)', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->where($data[0])->and_($data[1])->or($data[2])->_(); + $this->assertEquals( + 'SELECT * FROM table WHERE a = ? AND (b = ? OR c = ?)', + $sql, + ); $this->assertEquals([1, 2, 3], $this->object->getParams()); $this->object->setQuery('', []); - $sql = (string) $this->object->select('*')->from('table')->where_($data[0])->or($data[1])->_()->and_($data[2])->or($data[3])->_(); - $this->assertEquals('SELECT * FROM table WHERE (a = ? OR b = ?) AND (c = ? OR d = ?)', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->where_($data[0])->or($data[1])->_() + ->and_($data[2])->or($data[3])->_(); + $this->assertEquals( + 'SELECT * FROM table WHERE (a = ? OR b = ?) AND (c = ? OR d = ?)', + $sql, + ); $this->assertEquals([1, 2, 3, 4], $this->object->getParams()); $this->object->setQuery('', []); - $sql = (string) $this->object->select('*')->from('table')->where($data[0])->and_($data[1])->or_($data[2])->and($data[3])->_()->_(); - $this->assertEquals('SELECT * FROM table WHERE a = ? AND (b = ? OR (c = ? AND d = ?))', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->where($data[0])->and_($data[1])->or_($data[2])->and($data[3])->_()->_(); + $this->assertEquals( + 'SELECT * FROM table WHERE a = ? AND (b = ? OR (c = ? AND d = ?))', + $sql, + ); $this->assertEquals([1, 2, 3, 4], $this->object->getParams()); } @@ -320,53 +399,88 @@ public function testSelectWhereWithConditionsGroupedBySubqueries(): void { $data = [['a' => 1], ['b' => 2], ['c' => 3], ['d' => 4]]; - $sql = (string) $this->object->select('*')->from('table')->where($data[0], Sql::cond($data[1])->or($data[2])); - $this->assertEquals('SELECT * FROM table WHERE a = ? AND (b = ? OR c = ?)', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->where($data[0], Sql::cond($data[1])->or($data[2])); + $this->assertEquals( + 'SELECT * FROM table WHERE a = ? AND (b = ? OR c = ?)', + $sql, + ); $this->assertEquals([1, 2, 3], $this->object->getParams()); $this->object->setQuery('', []); - $sql = (string) $this->object->select('*')->from('table')->where(Sql::cond($data[0])->or($data[1]), Sql::cond($data[2])->or($data[3])); - $this->assertEquals('SELECT * FROM table WHERE (a = ? OR b = ?) AND (c = ? OR d = ?)', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->where(Sql::cond($data[0])->or($data[1]), Sql::cond($data[2])->or($data[3])); + $this->assertEquals( + 'SELECT * FROM table WHERE (a = ? OR b = ?) AND (c = ? OR d = ?)', + $sql, + ); $this->assertEquals([1, 2, 3, 4], $this->object->getParams()); $this->object->setQuery('', []); - $sql = (string) $this->object->select('*')->from('table')->where($data[0], Sql::cond($data[1])->or(Sql::cond($data[2], $data[3]))); - $this->assertEquals('SELECT * FROM table WHERE a = ? AND (b = ? OR (c = ? AND d = ?))', $sql); + $sql = (string) $this->object->select('*')->from('table') + ->where($data[0], Sql::cond($data[1])->or(Sql::cond($data[2], $data[3]))); + $this->assertEquals( + 'SELECT * FROM table WHERE a = ? AND (b = ? OR (c = ? AND d = ?))', + $sql, + ); $this->assertEquals([1, 2, 3, 4], $this->object->getParams()); } public function testSelectWhereWithSubquery(): void { $subquery = Sql::select('column1')->from('t2')->where(['column2' => 2]); - $sql = (string) $this->object->select('column1')->from('t1')->where(['column1' => $subquery, 'column2' => 'foo']); + $sql = (string) $this->object->select('column1')->from('t1') + ->where(['column1' => $subquery, 'column2' => 'foo']); - $this->assertEquals('SELECT column1 FROM t1 WHERE column1 = (SELECT column1 FROM t2 WHERE column2 = ?) AND column2 = ?', $sql); + $this->assertEquals( + 'SELECT column1 FROM t1 WHERE column1 = (SELECT column1 FROM t2 WHERE column2 = ?)' + . ' AND column2 = ?', + $sql, + ); $this->assertEquals([2, 'foo'], $this->object->getParams()); } public function testSelectWhereWithNestedSubqueries(): void { $subquery1 = Sql::select('column1')->from('t3')->where(['column3' => 3]); - $subquery2 = Sql::select('column1')->from('t2')->where(['column2' => $subquery1, 'column3' => 'foo']); - $sql = (string) $this->object->select('column1')->from('t1')->where(['column1' => $subquery2]); + $subquery2 = Sql::select('column1')->from('t2') + ->where(['column2' => $subquery1, 'column3' => 'foo']); + $sql = (string) $this->object->select('column1')->from('t1') + ->where(['column1' => $subquery2]); - $this->assertEquals('SELECT column1 FROM t1 WHERE column1 = (SELECT column1 FROM t2 WHERE column2 = (SELECT column1 FROM t3 WHERE column3 = ?) AND column3 = ?)', $sql); + $this->assertEquals( + 'SELECT column1 FROM t1 WHERE column1 = (SELECT column1 FROM t2' + . ' WHERE column2 = (SELECT column1 FROM t3 WHERE column3 = ?) AND column3 = ?)', + $sql, + ); $this->assertEquals([3, 'foo'], $this->object->getParams()); } public function testSelectUsingAliasedColumns(): void { - $sql = (string) $this->object->select('f1', ['alias' => 'f2'], 'f3', ['another_alias' => 'f4'])->from('table'); - $this->assertEquals('SELECT f1, f2 AS alias, f3, f4 AS another_alias FROM table', $sql); + $sql = (string) $this->object->select( + 'f1', + ['alias' => 'f2'], + 'f3', + ['another_alias' => 'f4'], + )->from('table'); + $this->assertEquals( + 'SELECT f1, f2 AS alias, f3, f4 AS another_alias FROM table', + $sql, + ); $this->assertEmpty($this->object->getParams()); } public function testSelectWithColumnAsSubquery(): void { $subquery = Sql::select('f1')->from('t2')->where(['f2' => 2]); - $sql = (string) $this->object->select('f1', ['subalias' => $subquery])->from('t1')->where(['f2' => 'foo']); + $sql = (string) $this->object->select('f1', ['subalias' => $subquery]) + ->from('t1')->where(['f2' => 'foo']); - $this->assertEquals('SELECT f1, (SELECT f1 FROM t2 WHERE f2 = ?) AS subalias FROM t1 WHERE f2 = ?', $sql); + $this->assertEquals( + 'SELECT f1, (SELECT f1 FROM t2 WHERE f2 = ?) AS subalias FROM t1 WHERE f2 = ?', + $sql, + ); $this->assertEquals([2, 'foo'], $this->object->getParams()); } @@ -374,7 +488,10 @@ public function testInsertWithValueFunctions(): void { $data = ['column' => 123, 'column_2' => 234]; $sql = (string) $this->object->insertInto('table', $data, 'date')->values($data, 'NOW()'); - $this->assertEquals('INSERT INTO table (column, column_2, date) VALUES (?, ?, NOW())', $sql); + $this->assertEquals( + 'INSERT INTO table (column, column_2, date) VALUES (?, ?, NOW())', + $sql, + ); $this->assertEquals(array_values($data), $this->object->getParams()); } @@ -384,7 +501,10 @@ public function testInsertWithSelectSubquery(): void $subquery = Sql::select('f1', 'f2')->from('t2')->where($data); $sql = (string) $this->object->insertInto('t1', ['f1', 'f2'])->appendQuery($subquery); - $this->assertEquals('INSERT INTO t1 (f1, f2) SELECT f1, f2 FROM t2 WHERE f3 = ? AND f4 = ?', $sql); + $this->assertEquals( + 'INSERT INTO t1 (f1, f2) SELECT f1, f2 FROM t2 WHERE f3 = ? AND f4 = ?', + $sql, + ); $this->assertEquals(array_values($data), $this->object->getParams()); } } diff --git a/tests/Styles/AbstractStyleTest.php b/tests/Styles/AbstractStyleTest.php index f795352..d45c541 100644 --- a/tests/Styles/AbstractStyleTest.php +++ b/tests/Styles/AbstractStyleTest.php @@ -87,7 +87,7 @@ public static function camelCaseToSeparatorProvider(): array } #[DataProvider('singularPluralProvider')] - public function test_plural_to_singular_and_vice_versa(string $singular, string $plural): void + public function testPluralToSingularAndViceVersa(string $singular, string $plural): void { $pluralToSingular = new ReflectionMethod($this->style, 'pluralToSingular'); $this->assertEquals($singular, $pluralToSingular->invoke($this->style, $plural)); @@ -97,12 +97,21 @@ public function test_plural_to_singular_and_vice_versa(string $singular, string } #[DataProvider('camelCaseToSeparatorProvider')] - public function test_camel_case_to_separator_and_vice_versa(string $separator, string $camelCase, string $separated): void - { + public function testCamelCaseToSeparatorAndViceVersa( + string $separator, + string $camelCase, + string $separated, + ): void { $camelCaseToSeparatorMethod = new ReflectionMethod($this->style, 'camelCaseToSeparator'); - $this->assertEquals($separated, $camelCaseToSeparatorMethod->invoke($this->style, $camelCase, $separator)); + $this->assertEquals( + $separated, + $camelCaseToSeparatorMethod->invoke($this->style, $camelCase, $separator), + ); $separatorToCamelCaseMethod = new ReflectionMethod($this->style, 'separatorToCamelCase'); - $this->assertEquals($camelCase, $separatorToCamelCaseMethod->invoke($this->style, $separated, $separator)); + $this->assertEquals( + $camelCase, + $separatorToCamelCaseMethod->invoke($this->style, $separated, $separator), + ); } } diff --git a/tests/Styles/CakePHPTest.php b/tests/Styles/CakePHPTest.php index ee5f491..4b20acf 100644 --- a/tests/Styles/CakePHPTest.php +++ b/tests/Styles/CakePHPTest.php @@ -22,15 +22,20 @@ class CakePHPTest extends TestCase private PDO $conn; - private $posts; + /** @var list */ + private array $posts; - private $authors; + /** @var list */ + private array $authors; - private $comments; + /** @var list */ + private array $comments; - private $categories; + /** @var list */ + private array $categories; - private $postsCategories; + /** @var list */ + private array $postsCategories; protected function setUp(): void { @@ -162,7 +167,7 @@ protected function setUp(): void } #[DataProvider('tableEntityProvider')] - public function test_table_and_entities_methods($table, $entity): void + public function testTableAndEntitiesMethods(string $table, string $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); @@ -170,7 +175,7 @@ public function test_table_and_entities_methods($table, $entity): void } #[DataProvider('columnsPropertyProvider')] - public function test_columns_and_properties_methods($column): void + public function testColumnsAndPropertiesMethods(string $column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -179,27 +184,27 @@ public function test_columns_and_properties_methods($column): void } #[DataProvider('manyToMantTableProvider')] - public function test_table_from_left_right_table($left, $right, $table): void + public function testTableFromLeftRightTable(string $left, string $right, string $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } #[DataProvider('foreignProvider')] - public function test_foreign($table, $foreign): void + public function testForeign(string $table, string $foreign): void { $this->assertTrue($this->style->isRemoteIdentifier($foreign)); $this->assertEquals($table, $this->style->remoteFromIdentifier($foreign)); $this->assertEquals($foreign, $this->style->remoteIdentifier($table)); } - public function test_fetching_entity_typed(): void + public function testFetchingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment); } - public function test_fetching_all_entity_typed(): void + public function testFetchingAllEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comments->fetchAll(); @@ -210,16 +215,19 @@ public function test_fetching_all_entity_typed(): void $this->assertInstanceOf(__NAMESPACE__ . '\Category', $categories->category_id); } - public function test_fetching_all_entity_typed_nested(): void + public function testFetchingAllEntityTypedNested(): void { $mapper = $this->mapper; $comment = $mapper->comments->posts->authors->fetchAll(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment[0]); $this->assertInstanceOf(__NAMESPACE__ . '\Post', $comment[0]->post_id); - $this->assertInstanceOf(__NAMESPACE__ . '\Author', $comment[0]->post_id->author_id); + $this->assertInstanceOf( + __NAMESPACE__ . '\Author', + $comment[0]->post_id->author_id, + ); } - public function test_persisting_entity_typed(): void + public function testPersistingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); @@ -227,21 +235,26 @@ public function test_persisting_entity_typed(): void $comment->text = 'HeyHey'; $mapper->comments->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comments where id=8')->fetchColumn(0); + $result = $this->conn->query( + 'select text from comments where id=8', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed(): void + public function testPersistingNewEntityTyped(): void { $mapper = $this->mapper; $comment = new Comment(); $comment->text = 'HeyHey'; $mapper->comments->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comments where id=9')->fetchColumn(0); + $result = $this->conn->query( + 'select text from comments where id=9', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } + /** @return array> */ public static function tableEntityProvider(): array { return [ @@ -253,6 +266,7 @@ public static function tableEntityProvider(): array ]; } + /** @return array> */ public static function manyToMantTableProvider(): array { return [ @@ -262,6 +276,7 @@ public static function manyToMantTableProvider(): array ]; } + /** @return array> */ public static function columnsPropertyProvider(): array { return [ @@ -273,6 +288,7 @@ public static function columnsPropertyProvider(): array ]; } + /** @return array> */ public static function foreignProvider(): array { return [ @@ -286,21 +302,45 @@ public static function foreignProvider(): array class Post { - public $id, $title, $text, $author_id; + public mixed $id = null; + + public string|null $title = null; + + public string|null $text = null; + + public mixed $author_id = null; } + class Author { - public $id, $name; + public mixed $id = null; + + public string|null $name = null; } + class Comment { - public $id, $post_id, $text; + public mixed $id = null; + + public mixed $post_id = null; + + public string|null $text = null; } + class Category { - public $id, $name, $category_id; + public mixed $id = null; + + public string|null $name = null; + + public mixed $category_id = null; } + class PostCategory { - public $id, $post_id, $category_id; + public mixed $id = null; + + public mixed $post_id = null; + + public mixed $category_id = null; } diff --git a/tests/Styles/NorthWindTest.php b/tests/Styles/NorthWindTest.php index 99c52cd..f26f26b 100644 --- a/tests/Styles/NorthWindTest.php +++ b/tests/Styles/NorthWindTest.php @@ -22,15 +22,20 @@ class NorthWindTest extends TestCase private PDO $conn; - private $posts; + /** @var list */ + private array $posts; - private $authors; + /** @var list */ + private array $authors; - private $comments; + /** @var list */ + private array $comments; - private $categories; + /** @var list */ + private array $categories; - private $postsCategories; + /** @var list */ + private array $postsCategories; protected function setUp(): void { @@ -160,6 +165,7 @@ protected function setUp(): void $this->mapper->entityNamespace = __NAMESPACE__ . '\\'; } + /** @return array> */ public static function tableEntityProvider(): array { return [ @@ -171,6 +177,7 @@ public static function tableEntityProvider(): array ]; } + /** @return array> */ public static function manyToMantTableProvider(): array { return [ @@ -180,6 +187,7 @@ public static function manyToMantTableProvider(): array ]; } + /** @return array> */ public static function columnsPropertyProvider(): array { return [ @@ -191,6 +199,7 @@ public static function columnsPropertyProvider(): array ]; } + /** @return array> */ public static function keyProvider(): array { return [ @@ -202,14 +211,14 @@ public static function keyProvider(): array } #[DataProvider('tableEntityProvider')] - public function test_table_and_entities_methods($table, $entity): void + public function testTableAndEntitiesMethods(string $table, string $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); } #[DataProvider('columnsPropertyProvider')] - public function test_columns_and_properties_methods($column): void + public function testColumnsAndPropertiesMethods(string $column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -218,13 +227,13 @@ public function test_columns_and_properties_methods($column): void } #[DataProvider('manyToMantTableProvider')] - public function test_table_from_left_right_table($left, $right, $table): void + public function testTableFromLeftRightTable(string $left, string $right, string $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } #[DataProvider('keyProvider')] - public function test_keys($table, $foreign): void + public function testKeys(string $table, string $foreign): void { $this->assertTrue($this->style->isRemoteIdentifier($foreign)); $this->assertEquals($table, $this->style->remoteFromIdentifier($foreign)); @@ -232,14 +241,14 @@ public function test_keys($table, $foreign): void $this->assertEquals($foreign, $this->style->remoteIdentifier($table)); } - public function test_fetching_entity_typed(): void + public function testFetchingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->Comments[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comments', $comment); } - public function test_fetching_all_entity_typed(): void + public function testFetchingAllEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->Comments->fetchAll(); @@ -247,19 +256,25 @@ public function test_fetching_all_entity_typed(): void $categories = $mapper->PostCategories->Categories->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\PostCategories', $categories); - $this->assertInstanceOf(__NAMESPACE__ . '\Categories', $categories->CategoryID); + $this->assertInstanceOf( + __NAMESPACE__ . '\Categories', + $categories->CategoryID, + ); } - public function test_fetching_all_entity_typed_nested(): void + public function testFetchingAllEntityTypedNested(): void { $mapper = $this->mapper; $comment = $mapper->Comments->Posts->Authors->fetchAll(); $this->assertInstanceOf(__NAMESPACE__ . '\Comments', $comment[0]); $this->assertInstanceOf(__NAMESPACE__ . '\Posts', $comment[0]->PostID); - $this->assertInstanceOf(__NAMESPACE__ . '\Authors', $comment[0]->PostID->AuthorID); + $this->assertInstanceOf( + __NAMESPACE__ . '\Authors', + $comment[0]->PostID->AuthorID, + ); } - public function test_persisting_entity_typed(): void + public function testPersistingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->Comments[8]->fetch(); @@ -267,45 +282,67 @@ public function test_persisting_entity_typed(): void $comment->Text = 'HeyHey'; $mapper->Comments->persist($comment); $mapper->flush(); - $result = $this->conn->query('select Text from Comments where CommentID=8')->fetchColumn(0); + $result = $this->conn->query( + 'select Text from Comments where CommentID=8', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed(): void + public function testPersistingNewEntityTyped(): void { $mapper = $this->mapper; $comment = new Comments(); $comment->Text = 'HeyHey'; $mapper->Comments->persist($comment); $mapper->flush(); - $result = $this->conn->query('select Text from Comments where CommentID=9')->fetchColumn(0); + $result = $this->conn->query( + 'select Text from Comments where CommentID=9', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } } - - class Posts { - public $PostID, $Title, $Text, $AuthorID; + public mixed $PostID = null; + + public string|null $Title = null; + + public string|null $Text = null; + + public mixed $AuthorID = null; } class Authors { - public $AuthorID, $Name; + public mixed $AuthorID = null; + + public string|null $Name = null; } class Comments { - public $CommentID, $PostID, $Text; + public mixed $CommentID = null; + + public mixed $PostID = null; + + public string|null $Text = null; } class Categories { - public $CategoryID, $Name, $Description; + public mixed $CategoryID = null; + + public string|null $Name = null; + + public string|null $Description = null; } class PostCategories { - public $PostCategoryID, $PostID, $CategoryID; + public mixed $PostCategoryID = null; + + public mixed $PostID = null; + + public mixed $CategoryID = null; } diff --git a/tests/Styles/PluralTest.php b/tests/Styles/PluralTest.php index b484cb7..3787fcd 100644 --- a/tests/Styles/PluralTest.php +++ b/tests/Styles/PluralTest.php @@ -22,15 +22,20 @@ class PluralTest extends TestCase private PDO $conn; - private $posts; + /** @var list */ + private array $posts; - private $authors; + /** @var list */ + private array $authors; - private $comments; + /** @var list */ + private array $comments; - private $categories; + /** @var list */ + private array $categories; - private $postsCategories; + /** @var list */ + private array $postsCategories; protected function setUp(): void { @@ -157,7 +162,7 @@ protected function setUp(): void } #[DataProvider('tableEntityProvider')] - public function test_table_and_entities_methods($table, $entity): void + public function testTableAndEntitiesMethods(string $table, string $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); @@ -165,7 +170,7 @@ public function test_table_and_entities_methods($table, $entity): void } #[DataProvider('columnsPropertyProvider')] - public function test_columns_and_properties_methods($column): void + public function testColumnsAndPropertiesMethods(string $column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -174,27 +179,27 @@ public function test_columns_and_properties_methods($column): void } #[DataProvider('manyToMantTableProvider')] - public function test_table_from_left_right_table($left, $right, $table): void + public function testTableFromLeftRightTable(string $left, string $right, string $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } #[DataProvider('foreignProvider')] - public function test_foreign($table, $foreign): void + public function testForeign(string $table, string $foreign): void { $this->assertTrue($this->style->isRemoteIdentifier($foreign)); $this->assertEquals($table, $this->style->remoteFromIdentifier($foreign)); $this->assertEquals($foreign, $this->style->remoteIdentifier($table)); } - public function test_fetching_entity_typed(): void + public function testFetchingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment); } - public function test_fetching_all_entity_typed(): void + public function testFetchingAllEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comments->fetchAll(); @@ -202,19 +207,25 @@ public function test_fetching_all_entity_typed(): void $categories = $mapper->posts_categories->categories->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\PostCategory', $categories); - $this->assertInstanceOf(__NAMESPACE__ . '\Category', $categories->category_id); + $this->assertInstanceOf( + __NAMESPACE__ . '\Category', + $categories->category_id, + ); } - public function test_fetching_all_entity_typed_nested(): void + public function testFetchingAllEntityTypedNested(): void { $mapper = $this->mapper; $comment = $mapper->comments->posts->authors->fetchAll(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment[0]); $this->assertInstanceOf(__NAMESPACE__ . '\Post', $comment[0]->post_id); - $this->assertInstanceOf(__NAMESPACE__ . '\Author', $comment[0]->post_id->author_id); + $this->assertInstanceOf( + __NAMESPACE__ . '\Author', + $comment[0]->post_id->author_id, + ); } - public function test_persisting_entity_typed(): void + public function testPersistingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); @@ -222,21 +233,26 @@ public function test_persisting_entity_typed(): void $comment->text = 'HeyHey'; $mapper->comments->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comments where id=8')->fetchColumn(0); + $result = $this->conn->query( + 'select text from comments where id=8', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed(): void + public function testPersistingNewEntityTyped(): void { $mapper = $this->mapper; $comment = new Comment(); $comment->text = 'HeyHey'; $mapper->comments->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comments where id=9')->fetchColumn(0); + $result = $this->conn->query( + 'select text from comments where id=9', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } + /** @return array> */ public static function tableEntityProvider(): array { return [ @@ -248,6 +264,7 @@ public static function tableEntityProvider(): array ]; } + /** @return array> */ public static function manyToMantTableProvider(): array { return [ @@ -257,6 +274,7 @@ public static function manyToMantTableProvider(): array ]; } + /** @return array> */ public static function columnsPropertyProvider(): array { return [ @@ -268,6 +286,7 @@ public static function columnsPropertyProvider(): array ]; } + /** @return array> */ public static function foreignProvider(): array { return [ @@ -281,21 +300,43 @@ public static function foreignProvider(): array class Post { - public $id, $title, $text, $author_id; + public mixed $id = null; + + public string|null $title = null; + + public string|null $text = null; + + public mixed $author_id = null; } + class Author { - public $id, $name; + public mixed $id = null; + + public string|null $name = null; } + class Comment { - public $id, $post_id, $text; + public mixed $id = null; + + public mixed $post_id = null; + + public string|null $text = null; } + class Category { - public $id, $name; + public mixed $id = null; + + public string|null $name = null; } + class PostCategory { - public $id, $post_id, $category_id; + public mixed $id = null; + + public mixed $post_id = null; + + public mixed $category_id = null; } diff --git a/tests/Styles/SakilaTest.php b/tests/Styles/SakilaTest.php index c1a40bf..8297805 100644 --- a/tests/Styles/SakilaTest.php +++ b/tests/Styles/SakilaTest.php @@ -22,15 +22,20 @@ class SakilaTest extends TestCase private PDO $conn; - private $posts; + /** @var list */ + private array $posts; - private $authors; + /** @var list */ + private array $authors; - private $comments; + /** @var list */ + private array $comments; - private $categories; + /** @var list */ + private array $categories; - private $postsCategories; + /** @var list */ + private array $postsCategories; protected function setUp(): void { @@ -161,6 +166,7 @@ protected function setUp(): void $this->mapper->entityNamespace = __NAMESPACE__ . '\\'; } + /** @return array> */ public static function tableEntityProvider(): array { return [ @@ -172,6 +178,7 @@ public static function tableEntityProvider(): array ]; } + /** @return array> */ public static function manyToMantTableProvider(): array { return [ @@ -181,6 +188,7 @@ public static function manyToMantTableProvider(): array ]; } + /** @return array> */ public static function columnsPropertyProvider(): array { return [ @@ -192,6 +200,7 @@ public static function columnsPropertyProvider(): array ]; } + /** @return array> */ public static function keyProvider(): array { return [ @@ -203,14 +212,14 @@ public static function keyProvider(): array } #[DataProvider('tableEntityProvider')] - public function test_table_and_entities_methods($table, $entity): void + public function testTableAndEntitiesMethods(string $table, string $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); } #[DataProvider('columnsPropertyProvider')] - public function test_columns_and_properties_methods($column): void + public function testColumnsAndPropertiesMethods(string $column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -219,13 +228,13 @@ public function test_columns_and_properties_methods($column): void } #[DataProvider('manyToMantTableProvider')] - public function test_table_from_left_right_table($left, $right, $table): void + public function testTableFromLeftRightTable(string $left, string $right, string $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } #[DataProvider('keyProvider')] - public function test_foreign($table, $key): void + public function testForeign(string $table, string $key): void { $this->assertTrue($this->style->isRemoteIdentifier($key)); $this->assertEquals($table, $this->style->remoteFromIdentifier($key)); @@ -233,14 +242,14 @@ public function test_foreign($table, $key): void $this->assertEquals($key, $this->style->remoteIdentifier($table)); } - public function test_fetching_entity_typed(): void + public function testFetchingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comment[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment); } - public function test_fetching_all_entity_typed(): void + public function testFetchingAllEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comment->fetchAll(); @@ -248,19 +257,25 @@ public function test_fetching_all_entity_typed(): void $categories = $mapper->post_category->category->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\PostCategory', $categories); - $this->assertInstanceOf(__NAMESPACE__ . '\Category', $categories->category_id); + $this->assertInstanceOf( + __NAMESPACE__ . '\Category', + $categories->category_id, + ); } - public function test_fetching_all_entity_typed_nested(): void + public function testFetchingAllEntityTypedNested(): void { $mapper = $this->mapper; $comment = $mapper->comment->post->author->fetchAll(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment[0]); $this->assertInstanceOf(__NAMESPACE__ . '\Post', $comment[0]->post_id); - $this->assertInstanceOf(__NAMESPACE__ . '\Author', $comment[0]->post_id->author_id); + $this->assertInstanceOf( + __NAMESPACE__ . '\Author', + $comment[0]->post_id->author_id, + ); } - public function test_persisting_entity_typed(): void + public function testPersistingEntityTyped(): void { $mapper = $this->mapper; $comment = $mapper->comment[8]->fetch(); @@ -268,39 +283,69 @@ public function test_persisting_entity_typed(): void $comment->text = 'HeyHey'; $mapper->comment->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comment where comment_id=8')->fetchColumn(0); + $result = $this->conn->query( + 'select text from comment where comment_id=8', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed(): void + public function testPersistingNewEntityTyped(): void { $mapper = $this->mapper; $comment = new Comment(); $comment->text = 'HeyHey'; $mapper->comment->persist($comment); $mapper->flush(); - $result = $this->conn->query('select text from comment where comment_id=9')->fetchColumn(0); + $result = $this->conn->query( + 'select text from comment where comment_id=9', + )->fetchColumn(0); $this->assertEquals('HeyHey', $result); } } class Post { - public $post_id, $title, $text, $author_id; + public mixed $post_id = null; + + public string|null $title = null; + + public string|null $text = null; + + public mixed $author_id = null; } + class Author { - public $author_id, $name; + public mixed $author_id = null; + + public string|null $name = null; } + class Comment { - public $comment_id, $post_id, $text; + public mixed $comment_id = null; + + public mixed $post_id = null; + + public string|null $text = null; } + class Category { - public $category_id, $name, $content, $description; + public mixed $category_id = null; + + public string|null $name = null; + + public string|null $content = null; + + public string|null $description = null; } + class PostCategory { - public $post_category_id, $post_id, $category_id; + public mixed $post_category_id = null; + + public mixed $post_id = null; + + public mixed $category_id = null; } diff --git a/tests/Styles/StandardTest.php b/tests/Styles/StandardTest.php index 80e992e..68c6b56 100644 --- a/tests/Styles/StandardTest.php +++ b/tests/Styles/StandardTest.php @@ -18,6 +18,7 @@ protected function setUp(): void $this->style = new Standard(); } + /** @return array> */ public static function tableEntityProvider(): array { return [ @@ -29,6 +30,7 @@ public static function tableEntityProvider(): array ]; } + /** @return array> */ public static function manyToMantTableProvider(): array { return [ @@ -38,6 +40,7 @@ public static function manyToMantTableProvider(): array ]; } + /** @return array> */ public static function columnsPropertyProvider(): array { return [ @@ -49,6 +52,7 @@ public static function columnsPropertyProvider(): array ]; } + /** @return array> */ public static function foreignProvider(): array { return [ @@ -60,7 +64,7 @@ public static function foreignProvider(): array } #[DataProvider('tableEntityProvider')] - public function test_table_and_entities_methods($table, $entity): void + public function testTableAndEntitiesMethods(string $table, string $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); @@ -68,7 +72,7 @@ public function test_table_and_entities_methods($table, $entity): void } #[DataProvider('columnsPropertyProvider')] - public function test_columns_and_properties_methods($name): void + public function testColumnsAndPropertiesMethods(string $name): void { $this->assertEquals($name, $this->style->styledProperty($name)); $this->assertEquals($name, $this->style->realProperty($name)); @@ -77,13 +81,13 @@ public function test_columns_and_properties_methods($name): void } #[DataProvider('manyToMantTableProvider')] - public function test_table_from_left_right_table($left, $right, $table): void + public function testTableFromLeftRightTable(string $left, string $right, string $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } #[DataProvider('foreignProvider')] - public function test_foreign($table, $foreign): void + public function testForeign(string $table, string $foreign): void { $this->assertTrue($this->style->isRemoteIdentifier($foreign)); $this->assertEquals($table, $this->style->remoteFromIdentifier($foreign));