|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Query; |
| 4 | + |
| 5 | +use Codemonster\Database\Query\QueryBuilder; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Tests\Fakes\FakeConnection; |
| 8 | + |
| 9 | +class QueryBuilderCrudTest extends TestCase |
| 10 | +{ |
| 11 | + protected FakeConnection $connection; |
| 12 | + |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + $this->connection = new FakeConnection(); |
| 16 | + } |
| 17 | + |
| 18 | + /* ----------------------------------------------------------------- |
| 19 | + | INSERT |
| 20 | + | ----------------------------------------------------------------- |
| 21 | + */ |
| 22 | + |
| 23 | + public function testInsertGeneratesCorrectSqlAndBindings() |
| 24 | + { |
| 25 | + $builder = new QueryBuilder($this->connection, 'users'); |
| 26 | + |
| 27 | + $result = $builder->insert([ |
| 28 | + 'name' => 'Vasya', |
| 29 | + 'email' => 'k@example.com', |
| 30 | + ]); |
| 31 | + |
| 32 | + $this->assertTrue($result); |
| 33 | + $this->assertSame( |
| 34 | + 'INSERT INTO `users` (`name`, `email`) VALUES (?, ?)', |
| 35 | + $this->connection->lastSql |
| 36 | + ); |
| 37 | + $this->assertSame( |
| 38 | + ['Vasya', 'k@example.com'], |
| 39 | + $this->connection->lastBindings |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testInsertGetIdReturnsId() |
| 44 | + { |
| 45 | + $builder = new QueryBuilder($this->connection, 'ideas'); |
| 46 | + |
| 47 | + $this->connection->getPdo()->exec('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)'); |
| 48 | + |
| 49 | + $id = $builder->insertGetId(['title' => 'New idea']); |
| 50 | + |
| 51 | + $this->assertSame( |
| 52 | + 'INSERT INTO `ideas` (`title`) VALUES (?)', |
| 53 | + $this->connection->lastSql |
| 54 | + ); |
| 55 | + $this->assertSame(['New idea'], $this->connection->lastBindings); |
| 56 | + $this->assertTrue(is_int($id), 'insertGetId must return a number (ID)'); |
| 57 | + } |
| 58 | + |
| 59 | + /* ----------------------------------------------------------------- |
| 60 | + | UPDATE |
| 61 | + | ----------------------------------------------------------------- |
| 62 | + */ |
| 63 | + |
| 64 | + public function testUpdateGeneratesSqlAndBindings() |
| 65 | + { |
| 66 | + $builder = new QueryBuilder($this->connection, 'users'); |
| 67 | + |
| 68 | + $updated = $builder |
| 69 | + ->where('id', 10) |
| 70 | + ->update([ |
| 71 | + 'name' => 'Updated', |
| 72 | + 'active' => 0, |
| 73 | + ]); |
| 74 | + |
| 75 | + $this->assertSame(1, $updated); |
| 76 | + $this->assertSame( |
| 77 | + 'UPDATE `users` SET `name` = ?, `active` = ? WHERE `id` = ?', |
| 78 | + $this->connection->lastSql |
| 79 | + ); |
| 80 | + $this->assertSame( |
| 81 | + ['Updated', 0, 10], |
| 82 | + $this->connection->lastBindings |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /* ----------------------------------------------------------------- |
| 87 | + | DELETE |
| 88 | + | ----------------------------------------------------------------- |
| 89 | + */ |
| 90 | + |
| 91 | + public function testDeleteGeneratesSqlAndBindings() |
| 92 | + { |
| 93 | + $builder = new QueryBuilder($this->connection, 'logs'); |
| 94 | + |
| 95 | + $deleted = $builder |
| 96 | + ->where('id', 5) |
| 97 | + ->delete(); |
| 98 | + |
| 99 | + $this->assertSame(1, $deleted); |
| 100 | + $this->assertSame( |
| 101 | + 'DELETE FROM `logs` WHERE `id` = ?', |
| 102 | + $this->connection->lastSql |
| 103 | + ); |
| 104 | + $this->assertSame([5], $this->connection->lastBindings); |
| 105 | + } |
| 106 | +} |
0 commit comments