123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- <?php
- namespace Medoo\Tests;
- use Medoo\Medoo;
- use InvalidArgumentException;
- /**
- * @coversDefaultClass \Medoo\Medoo
- */
- class SelectTest extends MedooTestCase
- {
- /**
- * @covers ::select()
- * @covers ::selectContext()
- * @covers ::isJoin()
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectAll($type)
- {
- $this->setType($type);
- $this->database->select("account", "*");
- $this->assertQuery(
- <<<EOD
- SELECT * FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::select()
- * @covers ::selectContext()
- * @dataProvider typesProvider
- */
- public function testSelectTableWithAlias($type)
- {
- $this->setType($type);
- $this->database->select("account (user)", "name");
- $this->assertQuery(
- <<<EOD
- SELECT "name"
- FROM "account" AS "user"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectSingleColumn($type)
- {
- $this->setType($type);
- $this->database->select("account", "name");
- $this->assertQuery(
- <<<EOD
- SELECT "name"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectColumns($type)
- {
- $this->setType($type);
- $this->database->select("account", ["name", "id"]);
- $this->assertQuery(
- <<<EOD
- SELECT "name","id"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectColumnsWithAlias($type)
- {
- $this->setType($type);
- $this->database->select("account", ["name(nickname)", "id"]);
- $this->assertQuery(
- <<<EOD
- SELECT "name" AS "nickname","id"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectColumnsWithType($type)
- {
- $this->setType($type);
- $this->database->select("account", ["name[String]", "data [JSON]"]);
- $this->assertQuery(
- <<<EOD
- SELECT "name","data"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectColumnsWithAliasAndType($type)
- {
- $this->setType($type);
- $this->database->select("account", ["name (nickname) [String]", "data [JSON]"]);
- $this->assertQuery(
- <<<EOD
- SELECT "name" AS "nickname","data"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectColumnsWithRaw($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "id [String]" => Medoo::raw("UUID()")
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT UUID() AS "id"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::select()
- * @covers ::selectContext()
- * @covers ::isJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithWhere($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "name",
- "id"
- ], [
- "ORDER" => "age"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "name","id"
- FROM "account"
- ORDER BY "age"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::select()
- * @covers ::selectContext()
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithLeftJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]post" => "user_id"
- ], [
- "account.name",
- "post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title"
- FROM "account"
- LEFT JOIN "post"
- USING ("user_id")
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithRightJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[<]post" => "user_id"
- ], [
- "account.name",
- "post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title"
- FROM "account"
- RIGHT JOIN "post"
- USING ("user_id")
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithFullJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[<>]post" => "user_id"
- ], [
- "account.name",
- "post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title"
- FROM "account"
- FULL JOIN "post"
- USING ("user_id")
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithInnerJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[><]post" => "user_id"
- ], [
- "account.name",
- "post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title"
- FROM "account"
- INNER JOIN "post"
- USING ("user_id")
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithSameKeysJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]photo" => ["user_id", "avatar_id"],
- ], [
- "account.name",
- "photo.link"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","photo"."link"
- FROM "account"
- LEFT JOIN "photo"
- USING ("user_id", "avatar_id")
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithKeyJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]post" => ["user_id" => "author_id"],
- ], [
- "account.name",
- "post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title"
- FROM "account"
- LEFT JOIN "post"
- ON "account"."user_id" = "post"."author_id"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithAliasJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]post (main_post)" => ["user_id" => "author_id"],
- ], [
- "account.name",
- "main_post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","main_post"."title"
- FROM "account"
- LEFT JOIN "post" AS "main_post"
- ON "account"."user_id" = "main_post"."author_id"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithReferJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]post" => ["user_id" => "author_id"],
- "[>]album" => ["post.author_id" => "user_id"],
- ], [
- "account.name",
- "post.title",
- "album.link"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title","album"."link"
- FROM "account"
- LEFT JOIN "post"
- ON "account"."user_id" = "post"."author_id"
- LEFT JOIN "album"
- ON "post"."author_id" = "album"."user_id"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithMultipleConditionJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]album" => ["author_id" => "user_id"],
- "[>]post" => [
- "user_id" => "author_id",
- "album.user_id" => "owner_id"
- ]
- ], [
- "account.name",
- "post.title",
- "album.link"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title","album"."link"
- FROM "account"
- LEFT JOIN "album"
- ON "account"."author_id" = "album"."user_id"
- LEFT JOIN "post"
- ON "account"."user_id" = "post"."author_id"
- AND "album"."user_id" = "post"."owner_id"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectWithAdditionalConditionJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]post" => [
- "user_id" => "author_id",
- "AND" => [
- "post.id[>]" => 10
- ]
- ]
- ], [
- "account.name",
- "post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title"
- FROM "account"
- LEFT JOIN "post"
- ON "account"."user_id" = "post"."author_id"
- AND "post"."id" > 10
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::isJoin()
- * @covers ::buildJoin()
- * @dataProvider typesProvider
- */
- public function testSelectRawJoin($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "[>]post" => Medoo::raw("ON <account.user_id> = <post.author_id>")
- ], [
- "account.name",
- "post.title"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "account"."name","post"."title"
- FROM "account"
- LEFT JOIN "post"
- ON "account"."user_id" = "post"."author_id"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectAllWithJoin($type)
- {
- $this->setType($type);
- $this->expectException(InvalidArgumentException::class);
- $this->database->select("account", [
- "[>]post" => "user_id"
- ], [
- "account.*"
- ]);
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectWithDataMapping($type)
- {
- $this->setType($type);
- $this->database->select("post", [
- "[>]account" => ["user_id"]
- ], [
- "post.content",
- "userData" => [
- "account.user_id",
- "account.email",
- "meta" => [
- "account.location",
- "account.gender"
- ]
- ]
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "post"."content","account"."user_id","account"."email","account"."location","account"."gender"
- FROM "post"
- LEFT JOIN "account"
- USING ("user_id")
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectWithIndexMapping($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "user_id" => [
- "name (nickname)",
- "location"
- ]
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "user_id","name" AS "nickname","location"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectWithDistinct($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "@location",
- "nickname"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT DISTINCT "location","nickname"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectWithDistinctDiffOrder($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "location",
- "@nickname"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT DISTINCT "nickname","location"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectWithUnicodeCharacter($type)
- {
- $this->setType($type);
- $this->database->select("considérer", [
- "name (名前)",
- "положение (ロケーション)"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "name" AS "名前","положение" AS "ロケーション"
- FROM "considérer"
- EOD,
- $this->database->queryString
- );
- }
- /**
- * @covers ::columnMap()
- * @covers ::columnPush()
- * @dataProvider typesProvider
- */
- public function testSelectWithHyphenCharacter($type)
- {
- $this->setType($type);
- $this->database->select("account", [
- "nick-name"
- ]);
- $this->assertQuery(
- <<<EOD
- SELECT "nick-name"
- FROM "account"
- EOD,
- $this->database->queryString
- );
- }
- }
|