connection = ConnectionManager::get('test'); static::setAppNamespace(); } /** * Provider for testing that string and binary uuids work the same * * @return array */ public function uuidTableProvider() { return [['uuiditems'], ['binary_uuiditems']]; } /** * Test saving new records sets uuids * * @dataProvider uuidTableProvider * @return void */ public function testSaveNew($tableName) { $entity = new Entity([ 'name' => 'shiny new', 'published' => true, ]); $table = $this->getTableLocator()->get($tableName); $this->assertSame($entity, $table->save($entity)); $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters'); $row = $table->find('all')->where(['id' => $entity->id])->first(); $row->id = strtolower($row->id); $this->assertEquals($entity->toArray(), $row->toArray()); } /** * Test saving new records allows manual uuids * * @dataProvider uuidTableProvider * @return void */ public function testSaveNewSpecificId($tableName) { $id = Text::uuid(); $entity = new Entity([ 'id' => $id, 'name' => 'shiny and new', 'published' => true, ]); $table = $this->getTableLocator()->get($tableName); $this->assertSame($entity, $table->save($entity)); $this->assertSame($id, $entity->id); $row = $table->find('all')->where(['id' => $id])->first(); $this->assertNotEmpty($row); $this->assertSame($id, strtolower($row->id)); $this->assertSame($entity->name, $row->name); } /** * Test saving existing records works * * @dataProvider uuidTableProvider * @return void */ public function testSaveUpdate($tableName) { $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569'; $entity = new Entity([ 'id' => $id, 'name' => 'shiny update', 'published' => true, ]); $table = $this->getTableLocator()->get($tableName); $this->assertSame($entity, $table->save($entity)); $this->assertEquals($id, $entity->id, 'Should be 36 characters'); $row = $table->find('all')->where(['id' => $entity->id])->first(); $row->id = strtolower($row->id); $this->assertEquals($entity->toArray(), $row->toArray()); } /** * Test delete with string pk. * * @dataProvider uuidTableProvider * @return void */ public function testGetById($tableName) { $table = $this->getTableLocator()->get($tableName); $entity = $table->find('all')->firstOrFail(); $result = $table->get($entity->id); $this->assertSame($result->id, $entity->id); } /** * Test delete with string pk. * * @dataProvider uuidTableProvider * @return void */ public function testDelete($tableName) { $table = $this->getTableLocator()->get($tableName); $entity = $table->find('all')->firstOrFail(); $this->assertTrue($table->delete($entity)); $query = $table->find('all')->where(['id' => $entity->id]); $this->assertEmpty($query->first(), 'No row left'); } /** * Tests that sql server does not error when an empty uuid is bound * * @dataProvider uuidTableProvider * @return void */ public function testEmptyUuid($tableName) { $id = ''; $table = $this->getTableLocator()->get($tableName); $entity = $table->find('all') ->where(['id' => $id]) ->first(); $this->assertNull($entity); } }