QueryBuilderTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  4. use Illuminate\Database\MultipleRecordsFoundException;
  5. use Illuminate\Database\RecordsNotFoundException;
  6. use Illuminate\Database\Schema\Blueprint;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Schema;
  10. class QueryBuilderTest extends DatabaseTestCase
  11. {
  12. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  13. {
  14. Schema::create('posts', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->string('title');
  17. $table->text('content');
  18. $table->timestamp('created_at');
  19. });
  20. DB::table('posts')->insert([
  21. ['title' => 'Foo Post', 'content' => 'Lorem Ipsum.', 'created_at' => new Carbon('2017-11-12 13:14:15')],
  22. ['title' => 'Bar Post', 'content' => 'Lorem Ipsum.', 'created_at' => new Carbon('2018-01-02 03:04:05')],
  23. ]);
  24. }
  25. public function testSole()
  26. {
  27. $expected = ['id' => '1', 'title' => 'Foo Post'];
  28. $this->assertEquals($expected, (array) DB::table('posts')->where('title', 'Foo Post')->select('id', 'title')->sole());
  29. }
  30. public function testSoleFailsForMultipleRecords()
  31. {
  32. DB::table('posts')->insert([
  33. ['title' => 'Foo Post', 'content' => 'Lorem Ipsum.', 'created_at' => new Carbon('2017-11-12 13:14:15')],
  34. ]);
  35. $this->expectException(MultipleRecordsFoundException::class);
  36. DB::table('posts')->where('title', 'Foo Post')->sole();
  37. }
  38. public function testSoleFailsIfNoRecords()
  39. {
  40. $this->expectException(RecordsNotFoundException::class);
  41. DB::table('posts')->where('title', 'Baz Post')->sole();
  42. }
  43. public function testSelect()
  44. {
  45. $expected = ['id' => '1', 'title' => 'Foo Post'];
  46. $this->assertEquals($expected, (array) DB::table('posts')->select('id', 'title')->first());
  47. $this->assertEquals($expected, (array) DB::table('posts')->select(['id', 'title'])->first());
  48. }
  49. public function testSelectReplacesExistingSelects()
  50. {
  51. $this->assertEquals(
  52. ['id' => '1', 'title' => 'Foo Post'],
  53. (array) DB::table('posts')->select('content')->select(['id', 'title'])->first()
  54. );
  55. }
  56. public function testSelectWithSubQuery()
  57. {
  58. $this->assertEquals(
  59. ['id' => '1', 'title' => 'Foo Post', 'foo' => 'Lorem Ipsum.'],
  60. (array) DB::table('posts')->select(['id', 'title', 'foo' => function ($query) {
  61. $query->select('content');
  62. }])->first()
  63. );
  64. }
  65. public function testAddSelect()
  66. {
  67. $expected = ['id' => '1', 'title' => 'Foo Post', 'content' => 'Lorem Ipsum.'];
  68. $this->assertEquals($expected, (array) DB::table('posts')->select('id')->addSelect('title', 'content')->first());
  69. $this->assertEquals($expected, (array) DB::table('posts')->select('id')->addSelect(['title', 'content'])->first());
  70. $this->assertEquals($expected, (array) DB::table('posts')->addSelect(['id', 'title', 'content'])->first());
  71. }
  72. public function testAddSelectWithSubQuery()
  73. {
  74. $this->assertEquals(
  75. ['id' => '1', 'title' => 'Foo Post', 'foo' => 'Lorem Ipsum.'],
  76. (array) DB::table('posts')->addSelect(['id', 'title', 'foo' => function ($query) {
  77. $query->select('content');
  78. }])->first()
  79. );
  80. }
  81. public function testFromWithAlias()
  82. {
  83. $this->assertCount(2, DB::table('posts', 'alias')->select('alias.*')->get());
  84. }
  85. public function testFromWithSubQuery()
  86. {
  87. $this->assertSame(
  88. 'Fake Post',
  89. DB::table(function ($query) {
  90. $query->selectRaw("'Fake Post' as title");
  91. }, 'posts')->first()->title
  92. );
  93. }
  94. public function testWhereValueSubQuery()
  95. {
  96. $subQuery = function ($query) {
  97. $query->selectRaw("'Sub query value'");
  98. };
  99. $this->assertTrue(DB::table('posts')->where($subQuery, 'Sub query value')->exists());
  100. $this->assertFalse(DB::table('posts')->where($subQuery, 'Does not match')->exists());
  101. $this->assertTrue(DB::table('posts')->where($subQuery, '!=', 'Does not match')->exists());
  102. }
  103. public function testWhereValueSubQueryBuilder()
  104. {
  105. $subQuery = DB::table('posts')->selectRaw("'Sub query value'")->limit(1);
  106. $this->assertTrue(DB::table('posts')->where($subQuery, 'Sub query value')->exists());
  107. $this->assertFalse(DB::table('posts')->where($subQuery, 'Does not match')->exists());
  108. $this->assertTrue(DB::table('posts')->where($subQuery, '!=', 'Does not match')->exists());
  109. }
  110. public function testWhereDate()
  111. {
  112. $this->assertSame(1, DB::table('posts')->whereDate('created_at', '2018-01-02')->count());
  113. $this->assertSame(1, DB::table('posts')->whereDate('created_at', new Carbon('2018-01-02'))->count());
  114. }
  115. public function testOrWhereDate()
  116. {
  117. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDate('created_at', '2018-01-02')->count());
  118. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDate('created_at', new Carbon('2018-01-02'))->count());
  119. }
  120. public function testWhereDay()
  121. {
  122. $this->assertSame(1, DB::table('posts')->whereDay('created_at', '02')->count());
  123. $this->assertSame(1, DB::table('posts')->whereDay('created_at', 2)->count());
  124. $this->assertSame(1, DB::table('posts')->whereDay('created_at', new Carbon('2018-01-02'))->count());
  125. }
  126. public function testOrWhereDay()
  127. {
  128. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDay('created_at', '02')->count());
  129. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDay('created_at', 2)->count());
  130. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDay('created_at', new Carbon('2018-01-02'))->count());
  131. }
  132. public function testWhereMonth()
  133. {
  134. $this->assertSame(1, DB::table('posts')->whereMonth('created_at', '01')->count());
  135. $this->assertSame(1, DB::table('posts')->whereMonth('created_at', 1)->count());
  136. $this->assertSame(1, DB::table('posts')->whereMonth('created_at', new Carbon('2018-01-02'))->count());
  137. }
  138. public function testOrWhereMonth()
  139. {
  140. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereMonth('created_at', '01')->count());
  141. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereMonth('created_at', 1)->count());
  142. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereMonth('created_at', new Carbon('2018-01-02'))->count());
  143. }
  144. public function testWhereYear()
  145. {
  146. $this->assertSame(1, DB::table('posts')->whereYear('created_at', '2018')->count());
  147. $this->assertSame(1, DB::table('posts')->whereYear('created_at', 2018)->count());
  148. $this->assertSame(1, DB::table('posts')->whereYear('created_at', new Carbon('2018-01-02'))->count());
  149. }
  150. public function testOrWhereYear()
  151. {
  152. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereYear('created_at', '2018')->count());
  153. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereYear('created_at', 2018)->count());
  154. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereYear('created_at', new Carbon('2018-01-02'))->count());
  155. }
  156. public function testWhereTime()
  157. {
  158. $this->assertSame(1, DB::table('posts')->whereTime('created_at', '03:04:05')->count());
  159. $this->assertSame(1, DB::table('posts')->whereTime('created_at', new Carbon('2018-01-02 03:04:05'))->count());
  160. }
  161. public function testOrWhereTime()
  162. {
  163. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereTime('created_at', '03:04:05')->count());
  164. $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereTime('created_at', new Carbon('2018-01-02 03:04:05'))->count());
  165. }
  166. public function testPaginateWithSpecificColumns()
  167. {
  168. $result = DB::table('posts')->paginate(5, ['title', 'content']);
  169. $this->assertInstanceOf(LengthAwarePaginator::class, $result);
  170. $this->assertEquals($result->items(), [
  171. (object) ['title' => 'Foo Post', 'content' => 'Lorem Ipsum.'],
  172. (object) ['title' => 'Bar Post', 'content' => 'Lorem Ipsum.'],
  173. ]);
  174. }
  175. public function testChunkMap()
  176. {
  177. DB::enableQueryLog();
  178. $results = DB::table('posts')->orderBy('id')->chunkMap(function ($post) {
  179. return $post->title;
  180. }, 1);
  181. $this->assertCount(2, $results);
  182. $this->assertSame('Foo Post', $results[0]);
  183. $this->assertSame('Bar Post', $results[1]);
  184. $this->assertCount(3, DB::getQueryLog());
  185. }
  186. }