|
11 | 11 | use Illuminate\Database\Events\TransactionBeginning;
|
12 | 12 | use Illuminate\Database\Events\TransactionCommitted;
|
13 | 13 | use Illuminate\Database\Events\TransactionRolledBack;
|
| 14 | +use Illuminate\Database\MultipleColumnsSelectedException; |
14 | 15 | use Illuminate\Database\Query\Builder as BaseBuilder;
|
15 | 16 | use Illuminate\Database\Query\Grammars\Grammar;
|
16 | 17 | use Illuminate\Database\Query\Processors\Processor;
|
@@ -56,6 +57,28 @@ public function testSelectOneCallsSelectAndReturnsSingleResult()
|
56 | 57 | $this->assertSame('foo', $connection->selectOne('foo', ['bar' => 'baz']));
|
57 | 58 | }
|
58 | 59 |
|
| 60 | + public function testScalarCallsSelectOneAndReturnsSingleResult() |
| 61 | + { |
| 62 | + $connection = $this->getMockConnection(['selectOne']); |
| 63 | + $connection->expects($this->once())->method('selectOne')->with('select count(*) from tbl')->willReturn((object) ['count(*)' => 5]); |
| 64 | + $this->assertSame(5, $connection->scalar('select count(*) from tbl')); |
| 65 | + } |
| 66 | + |
| 67 | + public function testScalarThrowsExceptionIfMultipleColumnsAreSelected() |
| 68 | + { |
| 69 | + $connection = $this->getMockConnection(['selectOne']); |
| 70 | + $connection->expects($this->once())->method('selectOne')->with('select a, b from tbl')->willReturn((object) ['a' => 'a', 'b' => 'b']); |
| 71 | + $this->expectException(MultipleColumnsSelectedException::class); |
| 72 | + $connection->scalar('select a, b from tbl'); |
| 73 | + } |
| 74 | + |
| 75 | + public function testScalarReturnsNullIfUnderlyingSelectReturnsNoRows() |
| 76 | + { |
| 77 | + $connection = $this->getMockConnection(['selectOne']); |
| 78 | + $connection->expects($this->once())->method('selectOne')->with('select foo from tbl where 0=1')->willReturn(null); |
| 79 | + $this->assertNull($connection->scalar('select foo from tbl where 0=1')); |
| 80 | + } |
| 81 | + |
59 | 82 | public function testSelectProperlyCallsPDO()
|
60 | 83 | {
|
61 | 84 | $pdo = $this->getMockBuilder(DatabaseConnectionTestMockPDO::class)->onlyMethods(['prepare'])->getMock();
|
|
0 commit comments