conn = new FakeMySQLPDO(); $this->conn->exec('CREATE TABLE host (id INTEGER PRIMARY KEY AUTOINCREMENT, hostname TEXT NOT NULL, disabled TEXT DEFAULT \'\')'); $this->conn->exec('CREATE TABLE settings (name TEXT PRIMARY KEY, value TEXT)'); }); it('inserts via sql_save and returns the new auto-increment id', function () { $id = sql_save(['hostname' => 'host1', 'disabled' => ''], 'host', 'id', true, $this->conn); expect((int) $id)->toBe(1); $row = $this->conn->query('SELECT hostname FROM host WHERE id = 1')->fetch(PDO::FETCH_ASSOC); expect($row['hostname'])->toBe('host1'); }); it('updates an existing row when sql_save receives an explicit id', function () { sql_save(['hostname' => 'host1', 'disabled' => ''], 'host', 'id', true, $this->conn); sql_save(['id' => 1, 'hostname' => 'renamed'], 'host', 'id', true, $this->conn); $row = $this->conn->query('SELECT hostname FROM host WHERE id = 1')->fetch(PDO::FETCH_ASSOC); expect($row['hostname'])->toBe('renamed'); }); it('REPLACE-INTO semantics keep a single row keyed by name', function () { // _db_replace inlines field values directly into the SQL string, so the // caller must pre-quote string values (sql_save does this via db_qstr). db_replace('settings', ['name' => "'opt'", 'value' => "'v1'"], 'name', $this->conn); db_replace('settings', ['name' => "'opt'", 'value' => "'v2'"], 'name', $this->conn); $rows = $this->conn->query('SELECT name, value FROM settings')->fetchAll(PDO::FETCH_ASSOC); expect(count($rows))->toBe(1) ->and($rows[0]['name'])->toBe('opt') ->and($rows[0]['value'])->toBe('v2'); });