diff --git a/composer.json b/composer.json index 22f2251..ad91758 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ }, "require-dev": { "phpstan/phpstan": "^2.0", - "phpunit/phpunit": "^10.0", + "phpunit/phpunit": "^12.5", "respect/coding-standard": "^5.0", "squizlabs/php_codesniffer": "^4.0" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index da3a218..d4c8e3b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,17 +1,15 @@ - - - - - - - - tests - - - - - src/ - - + + + + tests + + + + + src + + diff --git a/tests/DbTest.php b/tests/DbTest.php index aa7e85d..399b620 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -4,7 +4,11 @@ namespace Respect\Relational; -class DbTest extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; + +#[CoversClass(Db::class)] +class DbTest extends TestCase { protected $object; @@ -27,7 +31,7 @@ protected function tearDown(): void unset($this->object); } - public function testBasicStatement() + public function testBasicStatement(): void { $this->assertEquals( 'unit', @@ -35,25 +39,25 @@ public function testBasicStatement() ); } - public function testPassingValues() + public function testPassingValues(): void { $line = $this->object->select('*')->from('unit')->where(array('testb' => 'abc'))->fetch(); $this->assertEquals(10, $line->testa); } - public function testFetchingAll() + public function testFetchingAll(): void { $all = $this->object->select('*')->from('unit')->fetchAll(); $this->assertEquals(3, count($all)); } - public function testFetchingClass() + public function testFetchingClass(): void { $line = $this->object->select('*')->from('unit')->fetch('Respect\Relational\testFetchingClass'); $this->assertInstanceOF('Respect\Relational\testFetchingClass', $line); } - public function testFetchingClassArgs() + public function testFetchingClassArgs(): void { $line = $this->object->select('*')->from('unit')->fetch('Respect\Relational\testFetchingClassArgs', array('foo')); @@ -61,7 +65,7 @@ public function testFetchingClassArgs() $this->assertEquals('foo', $line->testd); } - public function testFetchingCallback() + public function testFetchingCallback(): void { $line = $this->object->select('*')->from('unit')->fetch( function($row) { @@ -72,39 +76,39 @@ function($row) { $this->assertEquals('test', $line->acid); } - public function testFetchingInto() + public function testFetchingInto(): void { $x = new testFetchingInto; $line = $this->object->select('*')->from('unit')->where(array('testb' => 'abc'))->fetch($x); $this->assertEquals('abc', $x->testb); } - public function testRawSql() + public function testRawSql(): void { $all = $this->object->query('select * from unit')->fetchAll(); $this->assertEquals(3, count($all)); } - public function testFetchingArray() + public function testFetchingArray(): void { $line = $this->object->select('*')->from('unit')->where(array('testb' => 'abc'))->fetch(\PDO::FETCH_ASSOC); $this->assertTrue(is_array($line)); } - public function testFetchingArray2() + public function testFetchingArray2(): void { $line = $this->object->select('*')->from('unit')->where(array('testb' => 'abc'))->fetch(array()); $this->assertTrue(is_array($line)); } - public function testGetSql() + public function testGetSql(): void { $sql = $this->object->select('*')->from('unit')->where(array('testb' => 'abc'))->getSql(); $this->assertEquals('SELECT * FROM unit WHERE testb = ?', (string) $sql); $this->assertEquals(array('abc'), $sql->getParams()); } - public function testRawSqlWithParams() + public function testRawSqlWithParams(): void { $line = $this->object->query('SELECT * FROM unit WHERE testb = ?', array('abc'))->fetch(); $this->assertEquals(10, $line->testa); @@ -130,4 +134,4 @@ public function __construct($testd) $this->testd = $testd; } -} \ No newline at end of file +} diff --git a/tests/MapperTest.php b/tests/MapperTest.php index 37f44df..3946120 100644 --- a/tests/MapperTest.php +++ b/tests/MapperTest.php @@ -5,12 +5,15 @@ namespace Respect\Relational; use PDO; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; use Respect\Data\Collections\Filtered; use Respect\Data\Collections\Mix; use Respect\Data\Collections\Typed; use Respect\Data\Styles; -class MapperTest extends \PHPUnit\Framework\TestCase { +#[CoversClass(Mapper::class)] +class MapperTest extends TestCase { protected $conn, $mapper, $posts, $authors, $comments, $categories, $postsCategories, $issues; @@ -131,14 +134,14 @@ protected function setUp(): void { $this->conn = $conn; } - public function test_creating_with_db_instance() + public function test_creating_with_db_instance(): void { $db = new Db($this->conn); $mapper = new Mapper($db); $this->assertSame($db, $mapper->getDb()); } - public function test_get_defined_db_instance() + public function test_get_defined_db_instance(): void { $db = new Db($this->conn); $mapper = new Mapper($db); @@ -146,18 +149,15 @@ public function test_get_defined_db_instance() $this->assertSame($db, $mapper->getDb()); } - public function test_creating_with_invalid_args_should_throw_exception() + public function test_creating_with_invalid_args_should_throw_exception(): void { $this->expectException(\TypeError::class); $mapper = new Mapper('foo'); } - public function test_rolling_back_transaction() + public function test_rolling_back_transaction(): void { - $conn = $this->getMockBuilder('PDO') - ->setConstructorArgs(array('sqlite::memory:')) - ->onlyMethods(array('beginTransaction', 'rollback', 'prepare')) - ->getMock(); + $conn = $this->createMock(PDO::class); $conn->expects($this->any()) ->method('prepare') ->will($this->throwException(new \Exception)); @@ -174,16 +174,22 @@ public function test_rolling_back_transaction() } } - public function test_ignoring_last_insert_id_errors() + public function test_ignoring_last_insert_id_errors(): void { - $conn = $this->getMockBuilder('PDO') - ->setConstructorArgs(array('sqlite::memory:')) - ->onlyMethods(array('lastInsertId')) - ->getMock(); - $conn->exec('CREATE TABLE foo(id INTEGER PRIMARY KEY, name VARCHAR(255))'); - $conn->expects($this->any()) - ->method('lastInsertId') - ->will($this->throwException(new \PDOException)); + $conn = $this->createStub(PDO::class); + $conn->method('getAttribute') + ->willReturn('sqlite'); + $stmt = $this->createStub(\PDOStatement::class); + $stmt->method('execute') + ->willReturn(true); + $conn->method('prepare') + ->willReturn($stmt); + $conn->method('lastInsertId') + ->willThrowException(new \PDOException()); + $conn->method('beginTransaction') + ->willReturn(true); + $conn->method('commit') + ->willReturn(true); $mapper = new Mapper($conn); $obj = new \stdClass(); $obj->id = null; @@ -194,7 +200,7 @@ public function test_ignoring_last_insert_id_errors() $this->assertEquals('bar', $obj->name); } - public function test_removing_untracked_object() + public function test_removing_untracked_object(): void { $comment = new \stdClass(); $comment->id = 7; @@ -204,34 +210,34 @@ public function test_removing_untracked_object() $this->assertEmpty($this->mapper->comment[7]->fetch()); } - public function test_fetching_single_entity_from_collection_should_return_first_record_from_table() + public function test_fetching_single_entity_from_collection_should_return_first_record_from_table(): 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() + public function test_fetching_all_entites_from_collection_should_return_all_records(): 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() + public function test_extra_sql_on_single_fetch_should_be_applied_on_mapper_sql(): 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() + public function test_extra_sql_on_fetchAll_should_be_applied_on_mapper_sql(): 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() { + public function test_nested_collections_should_hydrate_results(): void { $mapper = $this->mapper; $comment = $mapper->comment->post[5]->fetch(); $this->assertEquals(7, $comment->id); @@ -243,7 +249,7 @@ public function test_nested_collections_should_hydrate_results() { $this->assertEquals(4, count(get_object_vars($comment->post_id))); } - public function testOneToN() { + public function testOneToN(): void { $mapper = $this->mapper; $comments = $mapper->comment->post($mapper->author)->fetchAll(); $comment = current($comments); @@ -260,7 +266,7 @@ public function testOneToN() { $this->assertEquals(2, count(get_object_vars($comment->post_id->author_id))); } - public function testNtoN() { + public function testNtoN(): void { $mapper = $this->mapper; $comments = $mapper->comment->post->post_category->category[2]->fetchAll(); $comment = current($comments); @@ -274,14 +280,14 @@ public function testNtoN() { $this->assertEquals(4, count(get_object_vars($comment->post_id))); } - public function testNtoNReverse() { + public function testNtoNReverse(): void { $mapper = $this->mapper; $cat = $mapper->category->post_category->post[5]->fetch(); $this->assertEquals(2, $cat->id); $this->assertEquals('Sample Category', $cat->name); } - public function testSimplePersist() { + public function testSimplePersist(): void { $mapper = $this->mapper; $entity = (object) array('id' => 4, 'name' => 'inserted', 'category_id' => null); $mapper->category->persist($entity); @@ -289,7 +295,7 @@ public function testSimplePersist() { $result = $this->conn->query('select * from category where id=4')->fetch(PDO::FETCH_OBJ); $this->assertEquals($entity, $result); } - public function testSimplePersistCollection() { + public function testSimplePersistCollection(): void { $mapper = $this->mapper; $entity = (object) array('id' => 4, 'name' => 'inserted', 'category_id' => null); $mapper->category->persist($entity); @@ -298,7 +304,7 @@ public function testSimplePersistCollection() { $this->assertEquals($entity, $result); } - public function testNestedPersistCollection() { + public function testNestedPersistCollection(): void { $postWithAuthor = (object) array( 'id' => null, 'title' => 'hi', @@ -315,7 +321,7 @@ public function testNestedPersistCollection() { $this->assertEquals('New', $author->name); $this->assertEquals('hi', $post->title); } - public function testNestedPersistCollectionShortcut() { + public function testNestedPersistCollectionShortcut(): void { $postWithAuthor = (object) array( 'id' => null, 'title' => 'hi', @@ -334,7 +340,7 @@ public function testNestedPersistCollectionShortcut() { $this->assertEquals('hi', $post->title); } - public function testNestedPersistCollectionWithChildrenShortcut() { + public function testNestedPersistCollectionWithChildrenShortcut(): void { $postWithAuthor = (object) array( 'id' => null, 'title' => 'hi', @@ -353,7 +359,7 @@ public function testNestedPersistCollectionWithChildrenShortcut() { $this->assertEquals('hi', $post->title); } - public function testSubCategory() { + public function testSubCategory(): void { $mapper = $this->mapper; $entity = (object) array('id' => 8, 'name' => 'inserted', 'category_id' => 2); $mapper->category->persist($entity); @@ -364,7 +370,7 @@ public function testSubCategory() { $this->assertEquals($result->name, $result2->name); $this->assertEquals($entity, $result); } - public function testSubCategoryCondition() { + public function testSubCategoryCondition(): void { $mapper = $this->mapper; $entity = (object) array('id' => 8, 'name' => 'inserted', 'category_id' => 2); $mapper->category->persist($entity); @@ -376,7 +382,7 @@ public function testSubCategoryCondition() { $this->assertEquals($entity, $result); } - public function testAutoIncrementPersist() { + public function testAutoIncrementPersist(): void { $mapper = $this->mapper; $entity = (object) array('id' => null, 'name' => 'inserted', 'category_id' => null); $mapper->category->persist($entity); @@ -386,7 +392,7 @@ public function testAutoIncrementPersist() { $this->assertEquals(4, $result->id); } - public function testPassedIdentity() { + public function testPassedIdentity(): void { $mapper = $this->mapper; $post = new \stdClass(); @@ -413,7 +419,7 @@ public function testPassedIdentity() { $this->assertEquals('abc', $comment->text); } - public function testJoinedPersist() { + public function testJoinedPersist(): void { $mapper = $this->mapper; $entity = $mapper->comment[8]->fetch(); $entity->text = 'HeyHey'; @@ -424,7 +430,7 @@ public function testJoinedPersist() { } - public function testRemove() { + public function testRemove(): void { $mapper = $this->mapper; $c8 = $mapper->comment[8]->fetch(); $pre = $this->conn->query('select count(*) from comment')->fetchColumn(0); @@ -434,7 +440,7 @@ public function testRemove() { $this->assertEquals($total, $pre - 1); } - public function test_fetching_entity_typed() + public function test_fetching_entity_typed(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -442,7 +448,7 @@ public function test_fetching_entity_typed() $this->assertInstanceOf('\Respect\Relational\Comment', $comment); } - public function test_fetching_all_entity_typed() + public function test_fetching_all_entity_typed(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -450,7 +456,7 @@ public function test_fetching_all_entity_typed() $this->assertInstanceOf('\Respect\Relational\Comment', $comment[1]); } - public function test_fetching_all_entity_typed_nested() + public function test_fetching_all_entity_typed_nested(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -459,7 +465,7 @@ public function test_fetching_all_entity_typed_nested() $this->assertInstanceOf('\Respect\Relational\Post', $comment[0]->post_id); } - public function test_persisting_entity_typed() + public function test_persisting_entity_typed(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -471,7 +477,7 @@ public function test_persisting_entity_typed() $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed() + public function test_persisting_new_entity_typed(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -483,7 +489,7 @@ public function test_persisting_new_entity_typed() $this->assertEquals('HeyHey', $result); } - public function test_setters_and_getters_datetime_as_object() + public function test_setters_and_getters_datetime_as_object(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; @@ -499,7 +505,7 @@ public function test_setters_and_getters_datetime_as_object() $this->assertEquals(date('Y-m-d'), $result->getDatetime()->format('Y-m-d')); } - public function test_style() + public function test_style(): void { $this->assertInstanceOf('Respect\Data\Styles\Stylable', $this->mapper->getStyle()); $this->assertInstanceOf('Respect\Data\Styles\Standard', $this->mapper->getStyle()); @@ -515,14 +521,14 @@ public function test_style() } } - public function test_feching_a_single_filtered_collection_should_not_bring_filtered_children() { + public function test_feching_a_single_filtered_collection_should_not_bring_filtered_children(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); $author = $mapper->authorsWithPosts->fetch(); $this->assertEquals($this->authors[0], $author); } - public function test_persisting_a_previously_fetched_filtered_entity_back_into_its_collection() { + public function test_persisting_a_previously_fetched_filtered_entity_back_into_its_collection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); $author = $mapper->authorsWithPosts->fetch(); @@ -533,7 +539,7 @@ public function test_persisting_a_previously_fetched_filtered_entity_back_into_i $this->assertEquals('Author Changed', $result->name); } - public function test_persisting_a_previously_fetched_filtered_entity_back_into_a_foreign_compatible_collection() { + public function test_persisting_a_previously_fetched_filtered_entity_back_into_a_foreign_compatible_collection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); $author = $mapper->authorsWithPosts->fetch(); @@ -544,7 +550,7 @@ public function test_persisting_a_previously_fetched_filtered_entity_back_into_a $this->assertEquals('Author Changed', $result->name); } - public function test_persisting_a_newly_created_filtered_entity_into_its_collection() { + public function test_persisting_a_newly_created_filtered_entity_into_its_collection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); $author = new \stdClass; @@ -556,7 +562,7 @@ public function test_persisting_a_newly_created_filtered_entity_into_its_collect $this->assertEquals('Author Changed', $result->name); } - public function test_persisting_a_newly_created_filtered_entity_into_a_foreig_compatible_collection() { + public function test_persisting_a_newly_created_filtered_entity_into_a_foreig_compatible_collection(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); $author = new \stdClass; @@ -568,14 +574,14 @@ public function test_persisting_a_newly_created_filtered_entity_into_a_foreig_co $this->assertEquals('Author Changed', $result->name); } - public function test_feching_multiple_filtered_collections_should_not_bring_filtered_children() { + public function test_feching_multiple_filtered_collections_should_not_bring_filtered_children(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::post()->author(); $authors = $mapper->authorsWithPosts->fetchAll(); $this->assertEquals($this->authors, $authors); } - public function test_filtered_collections_should_hydrate_non_filtered_parts_as_usual() { + public function test_filtered_collections_should_hydrate_non_filtered_parts_as_usual(): void { $mapper = $this->mapper; $mapper->postsFromAuthorsWithComments = Filtered::comment()->post()->author(); $post = $mapper->postsFromAuthorsWithComments->fetch(); @@ -583,7 +589,7 @@ public function test_filtered_collections_should_hydrate_non_filtered_parts_as_u $this->assertEquals($this->authors[0], $post->author_id); } - public function test_filtered_collections_should_persist_hydrated_non_filtered_parts_as_usual() { + public function test_filtered_collections_should_persist_hydrated_non_filtered_parts_as_usual(): void { $mapper = $this->mapper; $mapper->postsFromAuthorsWithComments = Filtered::comment()->post()->author(); $post = $mapper->postsFromAuthorsWithComments->fetch(); @@ -599,7 +605,7 @@ public function test_filtered_collections_should_persist_hydrated_non_filtered_p $this->assertEquals('John', $result->name); } - public function test_multiple_filtered_collections_dont_persist() { + public function test_multiple_filtered_collections_dont_persist(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::comment()->post->stack(Filtered::author()); $post = $mapper->authorsWithPosts->fetch(); @@ -614,7 +620,7 @@ public function test_multiple_filtered_collections_dont_persist() { $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() { + public function test_multiple_filtered_collections_dont_persist_newly_create_objects(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::comment()->post->stack(Filtered::author()); $post = $mapper->authorsWithPosts->fetch(); @@ -631,7 +637,7 @@ public function test_multiple_filtered_collections_dont_persist_newly_create_obj $this->assertNotEquals('A', $result->name); } - public function test_multiple_filtered_collections_fetch_at_once_dont_persist() { + public function test_multiple_filtered_collections_fetch_at_once_dont_persist(): void { $mapper = $this->mapper; $mapper->authorsWithPosts = Filtered::comment()->post->stack(Filtered::author()); $post = $mapper->authorsWithPosts->fetchAll(); @@ -648,7 +654,7 @@ public function test_multiple_filtered_collections_fetch_at_once_dont_persist() $this->assertNotEquals('A', $result->name); } - public function test_reusing_registered_filtered_collections_keeps_their_filtering() { + public function test_reusing_registered_filtered_collections_keeps_their_filtering(): void { $mapper = $this->mapper; $mapper->commentFil = Filtered::comment(); $mapper->author = Filtered::author(); @@ -660,7 +666,7 @@ public function test_reusing_registered_filtered_collections_keeps_their_filteri $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() { + public function test_reusing_registered_filtered_collections_keeps_their_filtering_on_fetchAll(): void { $mapper = $this->mapper; $mapper->commentFil = Filtered::comment(); $mapper->author = Filtered::author(); @@ -673,7 +679,7 @@ public function test_reusing_registered_filtered_collections_keeps_their_filteri $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() { + public function test_registered_filtered_collections_by_column_keeps_their_filtering(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('title')->post(); $post = $mapper->post->fetch(); @@ -684,7 +690,7 @@ public function test_registered_filtered_collections_by_column_keeps_their_filte $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() { + public function test_registered_filtered_collections_by_column_keeps_their_filtering_on_fetchAll(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('title')->post(); $post = $mapper->post->fetchAll(); @@ -696,7 +702,7 @@ public function test_registered_filtered_collections_by_column_keeps_their_filte $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() { + public function test_registered_filtered_wildcard_collections_keeps_their_filtering(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('*')->post(); $post = $mapper->post->fetch(); @@ -707,7 +713,7 @@ public function test_registered_filtered_wildcard_collections_keeps_their_filter $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() { + public function test_registered_filtered_wildcard_collections_keeps_their_filtering_on_fetchAll(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('*')->post(); $post = $mapper->post->fetchAll(); @@ -719,7 +725,7 @@ public function test_registered_filtered_wildcard_collections_keeps_their_filter $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() { + public function test_fetching_registered_filtered_collections_alongside_normal(): void { $mapper = $this->mapper; $mapper->post = Filtered::by('*')->post()->author(); $post = $mapper->post->fetchAll(); @@ -732,7 +738,7 @@ public function test_fetching_registered_filtered_collections_alongside_normal() $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() { + public function test_mixins_bring_results_from_two_tables(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(array('comment' => array('text')))->post()->author(); $post = $mapper->postComment->fetch(); @@ -740,7 +746,7 @@ public function test_mixins_bring_results_from_two_tables() { $this->assertEquals((object) array('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() { + public function test_mixins_persists_results_on_two_tables(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(array('comment' => array('text')))->post()->author(); $post = $mapper->postComment->fetch(); @@ -756,7 +762,7 @@ public function test_mixins_persists_results_on_two_tables() { $this->assertEquals('Comment Changed', $result->text); } - public function test_mixins_persists_newly_created_entities_on_two_tables() { + public function test_mixins_persists_newly_created_entities_on_two_tables(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(array('comment' => array('text')))->post()->author(); $post = (object) array('text' => 'Comment X', 'title' => 'Post X', 'id' => null); @@ -770,7 +776,7 @@ public function test_mixins_persists_newly_created_entities_on_two_tables() { $this->assertEquals('Comment X', $result->text); } - public function test_mixins_all() { + public function test_mixins_all(): void { $mapper = $this->mapper; $mapper->postComment = Mix::with(array('comment' => array('text')))->post()->author(); $post = $mapper->postComment->fetchAll(); @@ -787,7 +793,7 @@ public function test_mixins_all() { $this->assertEquals('Comment Changed', $result->text); } - public function test_typed() { + public function test_typed(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; $mapper->typedIssues = Typed::by('type')->issues(); @@ -802,7 +808,7 @@ public function test_typed() { $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() { + public function test_typed_single(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\\'; $mapper->typedIssues = Typed::by('type')->issues(); @@ -816,7 +822,7 @@ public function test_typed_single() { $this->assertEquals('Title Changed', $result->title); } - public function test_persist_new_with_arrayobject() + public function test_persist_new_with_arrayobject(): void { $mapper = $this->mapper; $arrayEntity = array('id' => 10, 'name' => 'array_object_category', 'category_id' => null); @@ -828,7 +834,7 @@ public function test_persist_new_with_arrayobject() } // -------------------------------------------------------------- - public function testFetchingEntityWithoutPublicPropertiesTyped() + public function testFetchingEntityWithoutPublicPropertiesTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\OtherEntity\\'; @@ -836,7 +842,7 @@ public function testFetchingEntityWithoutPublicPropertiesTyped() $this->assertInstanceOf('\Respect\Relational\OtherEntity\Post', $post); } - public function testFetchingAllEntityWithoutPublicPropertiesTyped() + public function testFetchingAllEntityWithoutPublicPropertiesTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\OtherEntity\\'; @@ -844,7 +850,7 @@ public function testFetchingAllEntityWithoutPublicPropertiesTyped() $this->assertInstanceOf('\Respect\Relational\OtherEntity\Post', $posts[0]); } - public function testFetchingAllEntityWithoutPublicPropertiesTypedNested() + public function testFetchingAllEntityWithoutPublicPropertiesTypedNested(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\OtherEntity\\'; @@ -853,7 +859,7 @@ public function testFetchingAllEntityWithoutPublicPropertiesTypedNested() $this->assertInstanceOf('\Respect\Relational\OtherEntity\Author', $posts[0]->getAuthor()); } - public function testPersistingEntityWithoutPublicPropertiesTyped() + public function testPersistingEntityWithoutPublicPropertiesTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\OtherEntity\\'; @@ -867,7 +873,7 @@ public function testPersistingEntityWithoutPublicPropertiesTyped() $this->assertEquals('HeyHey', $result); } - public function testPersistingNewEntityWithoutPublicPropertiesTyped() + public function testPersistingNewEntityWithoutPublicPropertiesTyped(): void { $mapper = $this->mapper; $mapper->entityNamespace = '\Respect\Relational\OtherEntity\\'; @@ -886,7 +892,7 @@ public function testPersistingNewEntityWithoutPublicPropertiesTyped() $this->assertEquals('My new Post Text', $result); } - public function testShouldExecuteEntityConstructorByDefault() + public function testShouldExecuteEntityConstructorByDefault(): void { $mapper = $this->mapper; $mapper->entityNamespace = 'Respect\\Relational\\OtherEntity\\'; @@ -899,7 +905,7 @@ public function testShouldExecuteEntityConstructorByDefault() } } - public function testShouldNotExecuteEntityConstructorWhenDisabled() + public function testShouldNotExecuteEntityConstructorWhenDisabled(): void { $mapper = $this->mapper; $mapper->entityNamespace = 'Respect\\Relational\\OtherEntity\\'; diff --git a/tests/SqlTest.php b/tests/SqlTest.php index b495c90..847d662 100644 --- a/tests/SqlTest.php +++ b/tests/SqlTest.php @@ -4,7 +4,12 @@ namespace Respect\Relational; -class SqlTest extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; + +#[CoversClass(Sql::class)] +class SqlTest extends TestCase { protected $object; @@ -14,7 +19,7 @@ protected function setUp(): void $this->object = new Sql; } - public function testCastingObjectToStringReturnsQuery() + public function testCastingObjectToStringReturnsQuery(): void { $sql = $this->object->select('*')->from('table'); $query = "SELECT * FROM table"; @@ -23,55 +28,55 @@ public function testCastingObjectToStringReturnsQuery() $this->assertSame($query, "$sql"); } - public function testSimpleSelect() + public function testSimpleSelect(): void { $sql = (string) $this->object->select('*')->from('table'); $this->assertEquals("SELECT * FROM table", $sql); } - public function testSelectMagicGetDistinctFromHell() + public function testSelectMagicGetDistinctFromHell(): void { $sql = (string) $this->object->selectDistinct('*')->from('table'); $this->assertEquals("SELECT DISTINCT * FROM table", $sql); } - public function testSelectColumns() + public function testSelectColumns(): void { $sql = (string) $this->object->select('column', 'other_column')->from('table'); $this->assertEquals("SELECT column, other_column FROM table", $sql); } - public function testSelectTables() + public function testSelectTables(): void { $sql = (string) $this->object->select('*')->from('table', 'other_table'); $this->assertEquals("SELECT * FROM table, other_table", $sql); } - public function testSelectInnerJoin() + 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); } - public function testSelectInnerJoinArr() + public function testSelectInnerJoinArr(): void { $sql = (string) $this->object->select('*')->from('table')->innerJoin('other_table')->on(array('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() + public function testSelectWhere(): void { $sql = (string) $this->object->select('*')->from('table')->where('column=123'); $this->assertEquals("SELECT * FROM table WHERE column=123", $sql); } - public function testSelectWhereBetween() + public function testSelectWhereBetween(): void { $sql = (string) $this->object->select('*')->from('table')->where('column')->between(1, 2); $this->assertEquals("SELECT * FROM table WHERE column BETWEEN 1 AND 2", $sql); } - public function testSelectWhereIn() + public function testSelectWhereIn(): void { $data = array('key' => '123', 'other_key' => '456'); $sql = (string) $this->object->select('*')->from('table')->where('column')->in($data); @@ -79,7 +84,7 @@ public function testSelectWhereIn() $this->assertEquals(array_values($data), $this->object->getParams()); } - public function testSelectWhereArray() + public function testSelectWhereArray(): void { $data = array('column' => '123', 'other_column' => '456'); $sql = (string) $this->object->select('*')->from('table')->where($data); @@ -87,7 +92,7 @@ public function testSelectWhereArray() $this->assertEquals(array_values($data), $this->object->getParams()); } - public function testSelectWhereArrayEmptyAnd() + public function testSelectWhereArrayEmptyAnd(): void { $data = array('column' => '123', 'other_column' => '456'); $sql = (string) $this->object->select('*')->from('table')->where($data)->and(); @@ -95,7 +100,7 @@ public function testSelectWhereArrayEmptyAnd() $this->assertEquals(array_values($data), $this->object->getParams()); } - public function testSelectWhereOr() + public function testSelectWhereOr(): void { $data = array('column' => '123'); $data2 = array('other_column' => '456'); @@ -104,7 +109,7 @@ public function testSelectWhereOr() $this->assertEquals(array_values(array_merge($data, $data2)), $this->object->getParams()); } - public function testSelectWhereArrayQualifiedNames() + public function testSelectWhereArrayQualifiedNames(): void { $data = array('a.column' => '123', 'b.other_column' => '456'); $sql = (string) $this->object->select('*')->from('table a', 'other_table b')->where($data); @@ -112,13 +117,13 @@ public function testSelectWhereArrayQualifiedNames() $this->assertEquals(array_values($data), $this->object->getParams()); } - public function testSelectGroupBy() + public function testSelectGroupBy(): void { $sql = (string) $this->object->select('*')->from('table')->groupBy('column', 'other_column'); $this->assertEquals("SELECT * FROM table GROUP BY column, other_column", $sql); } - public function testSelectGroupByHaving() + public function testSelectGroupByHaving(): void { $condition = array('other_column' => 456, 'yet_another_column' => 567); $sql = (string) $this->object->select('*')->from('table')->groupBy('column', 'other_column')->having($condition); @@ -126,7 +131,7 @@ public function testSelectGroupByHaving() $this->assertEquals(array_values($condition), $this->object->getParams()); } - public function testSimpleUpdate() + public function testSimpleUpdate(): void { $data = array('column' => 123, 'column_2' => 234); $condition = array('other_column' => 456, 'yet_another_column' => 567); @@ -135,7 +140,7 @@ public function testSimpleUpdate() $this->assertEquals(array_values(array_merge($data, $condition)), $this->object->getParams()); } - public function testSimpleInsert() + public function testSimpleInsert(): void { $data = array('column' => 123, 'column_2' => 234); $sql = (string) $this->object->insertInto('table', $data)->values($data); @@ -143,7 +148,7 @@ public function testSimpleInsert() $this->assertEquals(array_values($data), $this->object->getParams()); } - public function testSimpleDelete() + public function testSimpleDelete(): void { $condition = array('other_column' => 456, 'yet_another_column' => 567); $sql = (string) $this->object->deleteFrom('table')->where($condition); @@ -151,7 +156,7 @@ public function testSimpleDelete() $this->assertEquals(array_values($condition), $this->object->getParams()); } - public function testCreateTable() + public function testCreateTable(): void { $columns = array( 'column INT', @@ -162,7 +167,7 @@ public function testCreateTable() $this->assertEquals("CREATE TABLE table (column INT, other_column VARCHAR(255), yet_another_column TEXT)", $sql); } - public function testAlterTable() + public function testAlterTable(): void { $columns = array( 'ADD column INT', @@ -173,19 +178,19 @@ public function testAlterTable() $this->assertEquals("ALTER TABLE table ADD column INT, ADD other_column VARCHAR(255), ADD yet_another_column TEXT", $sql); } - public function testGrant() + public function testGrant(): void { $sql = (string) $this->object->grant('SELECT', 'UPDATE')->on('table')->to('user', 'other_user'); $this->assertEquals("GRANT SELECT, UPDATE ON table TO user, other_user", $sql); } - public function testRevoke() + public function testRevoke(): void { $sql = (string) $this->object->revoke('SELECT', 'UPDATE')->on('table')->to('user', 'other_user'); $this->assertEquals("REVOKE SELECT, UPDATE ON table TO user, other_user", $sql); } - public function testComplexFunctions() + public function testComplexFunctions(): void { $condition = array("AES_DECRYPT('pass', 'salt')" => 123); $sql = (string) $this->object->select('column', 'COUNT(column)', 'other_column')->from('table')->where($condition); @@ -196,7 +201,7 @@ public function testComplexFunctions() /** * @ticket 13 */ - public function testAggregateFunctions() + public function testAggregateFunctions(): void { $where = array('abc' => 10); $having = array('SUM(abc) >=' => '10', 'AVG(def) =' => 15); @@ -205,14 +210,14 @@ public function testAggregateFunctions() $this->assertEquals(array_values(array_merge($where, $having)), $this->object->getParams()); } - public function testStaticBuilderCall() + public function testStaticBuilderCall(): void { $this->assertEquals( 'ORDER BY updated_at DESC', (string) Sql::orderBy('updated_at')->desc() ); } - public function testLastParameterWithoutParts() + public function testLastParameterWithoutParts(): void { $this->assertEquals( 'ORDER BY updated_at DESC', @@ -220,7 +225,7 @@ public function testLastParameterWithoutParts() ); } - public static function provider_sql_operators() + public static function provider_sql_operators(): array { // $operator, $expectedWhere return array( @@ -238,9 +243,9 @@ public static function provider_sql_operators() /** * @ticket 13 - * @dataProvider provider_sql_operators */ - public function test_sql_operators($operator, $expected=null) + #[DataProvider('provider_sql_operators')] + public function test_sql_operators($operator, $expected=null): void { $expected = $expected ?: ' ?'; $where = array('id '.$operator => 10); @@ -248,7 +253,7 @@ public function test_sql_operators($operator, $expected=null) $this->assertEquals('SELECT * FROM table WHERE id '.$operator.$expected, $sql); } - public function testSetQueryWithParams() + public function testSetQueryWithParams(): void { $query = 'SELECT * FROM table WHERE a > ? AND b = ?'; $params = array(1, 'foo'); @@ -262,7 +267,7 @@ public function testSetQueryWithParams() $this->assertEmpty($this->object->getParams()); } - public function testSetQueryWithParamsViaConstructor() + public function testSetQueryWithParamsViaConstructor(): void { $query = 'SELECT * FROM table WHERE a > ? AND b = ?'; $params = array(1, 'foo'); @@ -272,7 +277,7 @@ public function testSetQueryWithParamsViaConstructor() $this->assertEquals($params, $sql->getParams()); } - public function testAppendQueryWithParams() + public function testAppendQueryWithParams(): void { $query = 'SELECT * FROM table WHERE a > ? AND b = ?'; $this->object->setQuery('SELECT * FROM table WHERE a > ? AND b = ?', array(1, 'foo')); @@ -282,7 +287,7 @@ public function testAppendQueryWithParams() $this->assertEquals(array(1, 'foo', 2), $this->object->getParams()); } - public function testSelectWhereWithRepeatedReferences() + public function testSelectWhereWithRepeatedReferences(): void { $data1 = array('a >' => 1, 'b' => 'foo'); $data2 = array('a >' => 4); @@ -293,7 +298,7 @@ public function testSelectWhereWithRepeatedReferences() $this->assertEquals(array(1, 'foo', 4, 'bar'), $this->object->getParams()); } - public function testSelectWhereWithConditionsGroupedByUnderscores() + public function testSelectWhereWithConditionsGroupedByUnderscores(): void { $data = array(array('a' => 1), array('b' => 2), array('c' => 3), array('d' => 4)); @@ -312,7 +317,7 @@ public function testSelectWhereWithConditionsGroupedByUnderscores() $this->assertEquals(array(1, 2, 3, 4), $this->object->getParams()); } - public function testSelectWhereWithConditionsGroupedBySubqueries() + public function testSelectWhereWithConditionsGroupedBySubqueries(): void { $data = array(array('a' => 1), array('b' => 2), array('c' => 3), array('d' => 4)); @@ -331,7 +336,7 @@ public function testSelectWhereWithConditionsGroupedBySubqueries() $this->assertEquals(array(1, 2, 3, 4), $this->object->getParams()); } - public function testSelectWhereWithSubquery() + public function testSelectWhereWithSubquery(): void { $subquery = Sql::select('column1')->from('t2')->where(array('column2' => 2)); $sql = (string) $this->object->select('column1')->from('t1')->where(array('column1' => $subquery, 'column2' => 'foo')); @@ -340,7 +345,7 @@ public function testSelectWhereWithSubquery() $this->assertEquals(array(2, 'foo'), $this->object->getParams()); } - public function testSelectWhereWithNestedSubqueries() + public function testSelectWhereWithNestedSubqueries(): void { $subquery1 = Sql::select('column1')->from('t3')->where(array('column3' => 3)); $subquery2 = Sql::select('column1')->from('t2')->where(array('column2' => $subquery1, 'column3' => 'foo')); @@ -350,14 +355,14 @@ public function testSelectWhereWithNestedSubqueries() $this->assertEquals(array(3, 'foo'), $this->object->getParams()); } - public function testSelectUsingAliasedColumns() + public function testSelectUsingAliasedColumns(): void { $sql = (string) $this->object->select('f1', array('alias' => 'f2'), 'f3', array('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() + public function testSelectWithColumnAsSubquery(): void { $subquery = Sql::select('f1')->from('t2')->where(array('f2' => 2)); $sql = (string) $this->object->select('f1', array('subalias' => $subquery))->from('t1')->where(array('f2' => 'foo')); @@ -366,7 +371,7 @@ public function testSelectWithColumnAsSubquery() $this->assertEquals(array(2, 'foo'), $this->object->getParams()); } - public function testInsertWithValueFunctions() + public function testInsertWithValueFunctions(): void { $data = array('column' => 123, 'column_2' => 234); $sql = (string) $this->object->insertInto('table', $data, 'date')->values($data, 'NOW()'); @@ -374,7 +379,7 @@ public function testInsertWithValueFunctions() $this->assertEquals(array_values($data), $this->object->getParams()); } - public function testInsertWithSelectSubquery() + public function testInsertWithSelectSubquery(): void { $data = array('f3' => 3, 'f4' => 4); $subquery = Sql::select('f1', 'f2')->from('t2')->where($data); diff --git a/tests/Styles/AbstractStyleTest.php b/tests/Styles/AbstractStyleTest.php index 70dde6f..8519a1a 100644 --- a/tests/Styles/AbstractStyleTest.php +++ b/tests/Styles/AbstractStyleTest.php @@ -4,69 +4,69 @@ namespace Respect\Data\Styles; -class AbstractStyleTest extends \PHPUnit\Framework\TestCase -{ +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; - /** - * @var Respect\Data\Styles\AbstractStyle - */ - private $style; +#[CoversClass(AbstractStyle::class)] +class AbstractStyleTest extends TestCase +{ + private AbstractStyle $style; - public static function singularPluralProvider() + /** @return array */ + public static function singularPluralProvider(): array { - return array( - array('post', 'posts'), - array('comment', 'comments'), - array('category', 'categories'), - array('tag', 'tags'), - array('entity', 'entities'), - ); + return [ + ['post', 'posts'], + ['comment', 'comments'], + ['category', 'categories'], + ['tag', 'tags'], + ['entity', 'entities'], + ]; } - public static function camelCaseToSeparatorProvider() + /** @return array */ + public static function camelCaseToSeparatorProvider(): array { - return array( - array('-', 'HenriqueMoody', 'Henrique-Moody'), - array(' ', 'AlexandreGaigalas', 'Alexandre Gaigalas'), - array('_', 'AugustoPascutti', 'Augusto_Pascutti'), - ); + return [ + ['-', 'HenriqueMoody', 'Henrique-Moody'], + [' ', 'AlexandreGaigalas', 'Alexandre Gaigalas'], + ['_', 'AugustoPascutti', 'Augusto_Pascutti'], + ]; } - protected function setUp(): void { - $this->style = $this->getMockForAbstractClass('\Respect\Data\Styles\AbstractStyle'); - } - - protected function tearDown(): void - { - $this->style = null; + $this->style = new class extends AbstractStyle { + public function styledProperty(string $name): string { return $name; } + public function realName(string $name): string { return $name; } + public function realProperty(string $name): string { return $name; } + public function styledName(string $name): string { return $name; } + public function identifier(string $name): string { return 'id'; } + public function remoteIdentifier(string $name): string { return $name . '_id'; } + public function composed(string $left, string $right): string { return "{$left}_{$right}"; } + public function isRemoteIdentifier(string $name): bool { return false; } + public function remoteFromIdentifier(string $name): ?string { return null; } + }; } - /** - * @dataProvider singularPluralProvider - */ - public function test_plural_to_singular_and_vice_versa($singular, $plural) + #[DataProvider('singularPluralProvider')] + public function test_plural_to_singular_and_vice_versa(string $singular, string $plural): void { $pluralToSingular = new \ReflectionMethod($this->style, 'pluralToSingular'); - $this->assertEquals($singular, $pluralToSingular->invoke($this->style, $plural)); + $this->assertEquals($singular, $pluralToSingular->invoke($this->style, $plural)); $singularToPlural = new \ReflectionMethod($this->style, 'singularToPlural'); - $this->assertEquals($plural, $singularToPlural->invoke($this->style, $singular)); + $this->assertEquals($plural, $singularToPlural->invoke($this->style, $singular)); } - /** - * @dataProvider camelCaseToSeparatorProvider - */ - public function test_camel_case_to_separator_and_vice_versa($separator, $camelCase, $separated) + #[DataProvider('camelCaseToSeparatorProvider')] + public function test_camel_case_to_separator_and_vice_versa(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)); } - } - diff --git a/tests/Styles/CakePHPTest.php b/tests/Styles/CakePHPTest.php index 437511e..e7871b1 100644 --- a/tests/Styles/CakePHPTest.php +++ b/tests/Styles/CakePHPTest.php @@ -9,8 +9,12 @@ Respect\Relational\Sql, Respect\Data\Styles\CakePHP, Respect\Relational\Mapper; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; -class CakePHPTest extends \PHPUnit\Framework\TestCase +#[CoversClass(CakePHP::class)] +class CakePHPTest extends TestCase { /** @@ -22,7 +26,7 @@ class CakePHPTest extends \PHPUnit\Framework\TestCase * @var Respect\Relational\Mapper */ private $mapper; - + /** * @var PDO */ @@ -164,20 +168,16 @@ protected function tearDown(): void $this->style = null; } - /** - * @dataProvider tableEntityProvider - */ - public function test_table_and_entities_methods($table, $entity) + #[DataProvider('tableEntityProvider')] + public function test_table_and_entities_methods($table, $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); $this->assertEquals('id', $this->style->identifier($table)); } - /** - * @dataProvider columnsPropertyProvider - */ - public function test_columns_and_properties_methods($column) + #[DataProvider('columnsPropertyProvider')] + public function test_columns_and_properties_methods($column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -185,43 +185,39 @@ public function test_columns_and_properties_methods($column) $this->assertNull($this->style->remoteFromIdentifier($column)); } - /** - * @dataProvider manyToMantTableProvider - */ - public function test_table_from_left_right_table($left, $right, $table) + #[DataProvider('manyToMantTableProvider')] + public function test_table_from_left_right_table($left, $right, $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } - /** - * @dataProvider foreignProvider - */ - public function test_foreign($table, $foreign) + #[DataProvider('foreignProvider')] + public function test_foreign($table, $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() + public function test_fetching_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment); } - public function test_fetching_all_entity_typed() + public function test_fetching_all_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comments->fetchAll(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment[1]); - + $categories = $mapper->post_categories->categories->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\PostCategory', $categories); $this->assertInstanceOf(__NAMESPACE__ . '\Category', $categories->category_id); } - public function test_fetching_all_entity_typed_nested() + public function test_fetching_all_entity_typed_nested(): void { $mapper = $this->mapper; $comment = $mapper->comments->posts->authors->fetchAll(); @@ -230,7 +226,7 @@ public function test_fetching_all_entity_typed_nested() $this->assertInstanceOf(__NAMESPACE__ . '\Author', $comment[0]->post_id->author_id); } - public function test_persisting_entity_typed() + public function test_persisting_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); @@ -242,7 +238,7 @@ public function test_persisting_entity_typed() $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed() + public function test_persisting_new_entity_typed(): void { $mapper = $this->mapper; $comment = new Comment(); @@ -253,7 +249,7 @@ public function test_persisting_new_entity_typed() $this->assertEquals('HeyHey', $result); } - public static function tableEntityProvider() + public static function tableEntityProvider(): array { return array( array('posts', 'Post'), @@ -264,7 +260,7 @@ public static function tableEntityProvider() ); } - public static function manyToMantTableProvider() + public static function manyToMantTableProvider(): array { return array( array('post', 'category', 'post_categories'), @@ -273,7 +269,7 @@ public static function manyToMantTableProvider() ); } - public static function columnsPropertyProvider() + public static function columnsPropertyProvider(): array { return array( array('id'), @@ -284,7 +280,7 @@ public static function columnsPropertyProvider() ); } - public static function foreignProvider() + public static function foreignProvider(): array { return array( array('posts', 'post_id'), diff --git a/tests/Styles/NorthWindTest.php b/tests/Styles/NorthWindTest.php index 2b0fc30..afc3c6b 100644 --- a/tests/Styles/NorthWindTest.php +++ b/tests/Styles/NorthWindTest.php @@ -9,8 +9,12 @@ Respect\Relational\Sql, Respect\Data\Styles\NorthWind, Respect\Relational\Mapper; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; -class NorthWindTest extends \PHPUnit\Framework\TestCase +#[CoversClass(NorthWind::class)] +class NorthWindTest extends TestCase { /** @@ -22,7 +26,7 @@ class NorthWindTest extends \PHPUnit\Framework\TestCase * @var Respect\Relational\Mapper */ private $mapper; - + /** * @var PDO */ @@ -158,7 +162,7 @@ protected function setUp(): void $this->mapper->entityNamespace = __NAMESPACE__ . '\\'; } - public static function tableEntityProvider() + public static function tableEntityProvider(): array { return array( array('Posts', 'Posts'), @@ -169,7 +173,7 @@ public static function tableEntityProvider() ); } - public static function manyToMantTableProvider() + public static function manyToMantTableProvider(): array { return array( array('Posts', 'Categories', 'PostCategories'), @@ -178,7 +182,7 @@ public static function manyToMantTableProvider() ); } - public static function columnsPropertyProvider() + public static function columnsPropertyProvider(): array { return array( array('Text'), @@ -188,8 +192,8 @@ public static function columnsPropertyProvider() array('Udated'), ); } - - public static function keyProvider() + + public static function keyProvider(): array { return array( array('Posts', 'PostID'), @@ -199,19 +203,15 @@ public static function keyProvider() ); } - /** - * @dataProvider tableEntityProvider - */ - public function test_table_and_entities_methods($table, $entity) + #[DataProvider('tableEntityProvider')] + public function test_table_and_entities_methods($table, $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) + #[DataProvider('columnsPropertyProvider')] + public function test_columns_and_properties_methods($column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -219,18 +219,14 @@ public function test_columns_and_properties_methods($column) $this->assertNull($this->style->remoteFromIdentifier($column)); } - /** - * @dataProvider manyToMantTableProvider - */ - public function test_table_from_left_right_table($left, $right, $table) + #[DataProvider('manyToMantTableProvider')] + public function test_table_from_left_right_table($left, $right, $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } - - /** - * @dataProvider keyProvider - */ - public function test_keys($table, $foreign) + + #[DataProvider('keyProvider')] + public function test_keys($table, $foreign): void { $this->assertTrue($this->style->isRemoteIdentifier($foreign)); $this->assertEquals($table, $this->style->remoteFromIdentifier($foreign)); @@ -238,25 +234,25 @@ public function test_keys($table, $foreign) $this->assertEquals($foreign, $this->style->remoteIdentifier($table)); } - public function test_fetching_entity_typed() + public function test_fetching_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->Comments[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comments', $comment); } - public function test_fetching_all_entity_typed() + public function test_fetching_all_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->Comments->fetchAll(); $this->assertInstanceOf(__NAMESPACE__ . '\Comments', $comment[1]); - + $categories = $mapper->PostCategories->Categories->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\PostCategories', $categories); $this->assertInstanceOf(__NAMESPACE__ . '\Categories', $categories->CategoryID); } - public function test_fetching_all_entity_typed_nested() + public function test_fetching_all_entity_typed_nested(): void { $mapper = $this->mapper; $comment = $mapper->Comments->Posts->Authors->fetchAll(); @@ -265,7 +261,7 @@ public function test_fetching_all_entity_typed_nested() $this->assertInstanceOf(__NAMESPACE__ . '\Authors', $comment[0]->PostID->AuthorID); } - public function test_persisting_entity_typed() + public function test_persisting_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->Comments[8]->fetch(); @@ -277,7 +273,7 @@ public function test_persisting_entity_typed() $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed() + public function test_persisting_new_entity_typed(): void { $mapper = $this->mapper; $comment = new Comments(); @@ -316,4 +312,3 @@ class PostCategories { public $PostCategoryID, $PostID, $CategoryID; } - diff --git a/tests/Styles/PluralTest.php b/tests/Styles/PluralTest.php index bc057bf..7b82d6a 100644 --- a/tests/Styles/PluralTest.php +++ b/tests/Styles/PluralTest.php @@ -9,8 +9,12 @@ Respect\Relational\Sql, Respect\Data\Styles\Plural, Respect\Relational\Mapper; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; -class PluralTest extends \PHPUnit\Framework\TestCase +#[CoversClass(Plural::class)] +class PluralTest extends TestCase { /** * @var Respect\Data\Styles\Plural @@ -158,20 +162,16 @@ protected function tearDown(): void $this->style = null; } - /** - * @dataProvider tableEntityProvider - */ - public function test_table_and_entities_methods($table, $entity) + #[DataProvider('tableEntityProvider')] + public function test_table_and_entities_methods($table, $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); $this->assertEquals('id', $this->style->identifier($table)); } - /** - * @dataProvider columnsPropertyProvider - */ - public function test_columns_and_properties_methods($column) + #[DataProvider('columnsPropertyProvider')] + public function test_columns_and_properties_methods($column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -179,32 +179,28 @@ public function test_columns_and_properties_methods($column) $this->assertNull($this->style->remoteFromIdentifier($column)); } - /** - * @dataProvider manyToMantTableProvider - */ - public function test_table_from_left_right_table($left, $right, $table) + #[DataProvider('manyToMantTableProvider')] + public function test_table_from_left_right_table($left, $right, $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } - /** - * @dataProvider foreignProvider - */ - public function test_foreign($table, $foreign) + #[DataProvider('foreignProvider')] + public function test_foreign($table, $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() + public function test_fetching_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment); } - public function test_fetching_all_entity_typed() + public function test_fetching_all_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comments->fetchAll(); @@ -215,7 +211,7 @@ public function test_fetching_all_entity_typed() $this->assertInstanceOf(__NAMESPACE__ . '\Category', $categories->category_id); } - public function test_fetching_all_entity_typed_nested() + public function test_fetching_all_entity_typed_nested(): void { $mapper = $this->mapper; $comment = $mapper->comments->posts->authors->fetchAll(); @@ -224,7 +220,7 @@ public function test_fetching_all_entity_typed_nested() $this->assertInstanceOf(__NAMESPACE__ . '\Author', $comment[0]->post_id->author_id); } - public function test_persisting_entity_typed() + public function test_persisting_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comments[8]->fetch(); @@ -236,7 +232,7 @@ public function test_persisting_entity_typed() $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed() + public function test_persisting_new_entity_typed(): void { $mapper = $this->mapper; $comment = new Comment(); @@ -247,7 +243,7 @@ public function test_persisting_new_entity_typed() $this->assertEquals('HeyHey', $result); } - public static function tableEntityProvider() + public static function tableEntityProvider(): array { return array( array('posts', 'Post'), @@ -258,7 +254,7 @@ public static function tableEntityProvider() ); } - public static function manyToMantTableProvider() + public static function manyToMantTableProvider(): array { return array( array('post', 'category', 'posts_categories'), @@ -267,7 +263,7 @@ public static function manyToMantTableProvider() ); } - public static function columnsPropertyProvider() + public static function columnsPropertyProvider(): array { return array( array('id'), @@ -278,7 +274,7 @@ public static function columnsPropertyProvider() ); } - public static function foreignProvider() + public static function foreignProvider(): array { return array( array('posts', 'post_id'), diff --git a/tests/Styles/SakilaTest.php b/tests/Styles/SakilaTest.php index 51059af..c2c5721 100644 --- a/tests/Styles/SakilaTest.php +++ b/tests/Styles/SakilaTest.php @@ -9,8 +9,12 @@ Respect\Relational\Sql, Respect\Data\Styles\Sakila, Respect\Relational\Mapper; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; -class SakilaTest extends \PHPUnit\Framework\TestCase +#[CoversClass(Sakila::class)] +class SakilaTest extends TestCase { /** @@ -159,7 +163,7 @@ protected function setUp(): void $this->mapper->entityNamespace = __NAMESPACE__ . '\\'; } - public static function tableEntityProvider() + public static function tableEntityProvider(): array { return array( array('post', 'Post'), @@ -170,7 +174,7 @@ public static function tableEntityProvider() ); } - public static function manyToMantTableProvider() + public static function manyToMantTableProvider(): array { return array( array('post', 'category', 'post_category'), @@ -179,7 +183,7 @@ public static function manyToMantTableProvider() ); } - public static function columnsPropertyProvider() + public static function columnsPropertyProvider(): array { return array( array('id'), @@ -190,7 +194,7 @@ public static function columnsPropertyProvider() ); } - public static function keyProvider() + public static function keyProvider(): array { return array( array('post', 'post_id'), @@ -200,19 +204,15 @@ public static function keyProvider() ); } - /** - * @dataProvider tableEntityProvider - */ - public function test_table_and_entities_methods($table, $entity) + #[DataProvider('tableEntityProvider')] + public function test_table_and_entities_methods($table, $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) + #[DataProvider('columnsPropertyProvider')] + public function test_columns_and_properties_methods($column): void { $this->assertEquals($column, $this->style->styledProperty($column)); $this->assertEquals($column, $this->style->realProperty($column)); @@ -220,18 +220,14 @@ public function test_columns_and_properties_methods($column) $this->assertNull($this->style->remoteFromIdentifier($column)); } - /** - * @dataProvider manyToMantTableProvider - */ - public function test_table_from_left_right_table($left, $right, $table) + #[DataProvider('manyToMantTableProvider')] + public function test_table_from_left_right_table($left, $right, $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } - /** - * @dataProvider keyProvider - */ - public function test_foreign($table, $key) + #[DataProvider('keyProvider')] + public function test_foreign($table, $key): void { $this->assertTrue($this->style->isRemoteIdentifier($key)); $this->assertEquals($table, $this->style->remoteFromIdentifier($key)); @@ -239,25 +235,25 @@ public function test_foreign($table, $key) $this->assertEquals($key, $this->style->remoteIdentifier($table)); } - public function test_fetching_entity_typed() + public function test_fetching_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comment[8]->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment); } - public function test_fetching_all_entity_typed() + public function test_fetching_all_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comment->fetchAll(); $this->assertInstanceOf(__NAMESPACE__ . '\Comment', $comment[1]); - + $categories = $mapper->post_category->category->fetch(); $this->assertInstanceOf(__NAMESPACE__ . '\PostCategory', $categories); $this->assertInstanceOf(__NAMESPACE__ . '\Category', $categories->category_id); } - public function test_fetching_all_entity_typed_nested() + public function test_fetching_all_entity_typed_nested(): void { $mapper = $this->mapper; $comment = $mapper->comment->post->author->fetchAll(); @@ -266,7 +262,7 @@ public function test_fetching_all_entity_typed_nested() $this->assertInstanceOf(__NAMESPACE__ . '\Author', $comment[0]->post_id->author_id); } - public function test_persisting_entity_typed() + public function test_persisting_entity_typed(): void { $mapper = $this->mapper; $comment = $mapper->comment[8]->fetch(); @@ -278,7 +274,7 @@ public function test_persisting_entity_typed() $this->assertEquals('HeyHey', $result); } - public function test_persisting_new_entity_typed() + public function test_persisting_new_entity_typed(): void { $mapper = $this->mapper; $comment = new Comment(); diff --git a/tests/Styles/StandardTest.php b/tests/Styles/StandardTest.php index e62d882..7cd6e8c 100644 --- a/tests/Styles/StandardTest.php +++ b/tests/Styles/StandardTest.php @@ -4,7 +4,12 @@ namespace Respect\Data\Styles; -class StandardTest extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; + +#[CoversClass(Standard::class)] +class StandardTest extends TestCase { /** @@ -13,7 +18,7 @@ class StandardTest extends \PHPUnit\Framework\TestCase private $style; - public static function tableEntityProvider() + public static function tableEntityProvider(): array { return array( array('post', 'Post'), @@ -24,7 +29,7 @@ public static function tableEntityProvider() ); } - public static function manyToMantTableProvider() + public static function manyToMantTableProvider(): array { return array( array('post', 'category', 'post_category'), @@ -33,7 +38,7 @@ public static function manyToMantTableProvider() ); } - public static function columnsPropertyProvider() + public static function columnsPropertyProvider(): array { return array( array('id'), @@ -43,8 +48,8 @@ public static function columnsPropertyProvider() array('created'), ); } - - public static function foreignProvider() + + public static function foreignProvider(): array { return array( array('post', 'post_id'), @@ -65,20 +70,16 @@ protected function tearDown(): void $this->style = null; } - /** - * @dataProvider tableEntityProvider - */ - public function test_table_and_entities_methods($table, $entity) + #[DataProvider('tableEntityProvider')] + public function test_table_and_entities_methods($table, $entity): void { $this->assertEquals($entity, $this->style->styledName($table)); $this->assertEquals($table, $this->style->realName($entity)); $this->assertEquals('id', $this->style->identifier($table)); } - /** - * @dataProvider columnsPropertyProvider - */ - public function test_columns_and_properties_methods($name) + #[DataProvider('columnsPropertyProvider')] + public function test_columns_and_properties_methods($name): void { $this->assertEquals($name, $this->style->styledProperty($name)); $this->assertEquals($name, $this->style->realProperty($name)); @@ -86,18 +87,14 @@ public function test_columns_and_properties_methods($name) $this->assertNull($this->style->remoteFromIdentifier($name)); } - /** - * @dataProvider manyToMantTableProvider - */ - public function test_table_from_left_right_table($left, $right, $table) + #[DataProvider('manyToMantTableProvider')] + public function test_table_from_left_right_table($left, $right, $table): void { $this->assertEquals($table, $this->style->composed($left, $right)); } - - /** - * @dataProvider foreignProvider - */ - public function test_foreign($table, $foreign) + + #[DataProvider('foreignProvider')] + public function test_foreign($table, $foreign): void { $this->assertTrue($this->style->isRemoteIdentifier($foreign)); $this->assertEquals($table, $this->style->remoteFromIdentifier($foreign)); @@ -105,4 +102,3 @@ public function test_foreign($table, $foreign) } } -