connection = ConnectionManager::get('test'); Configure::write('App.namespace', 'TestApp'); } /** * teardown * * @return void */ public function tearDown() { parent::tearDown(); TableRegistry::clear(); } /** * Test saving new records sets uuids * * @return void */ public function testSaveNew() { $entity = new Entity([ 'name' => 'shiny new', 'published' => true, ]); $table = TableRegistry::get('uuiditems'); $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 * * @return void */ public function testSaveNewSpecificId() { $id = Text::uuid(); $entity = new Entity([ 'id' => $id, 'name' => 'shiny and new', 'published' => true, ]); $table = TableRegistry::get('uuiditems'); $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 * * @return void */ public function testSaveUpdate() { $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569'; $entity = new Entity([ 'id' => $id, 'name' => 'shiny update', 'published' => true, ]); $table = TableRegistry::get('uuiditems'); $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. * * @return void */ public function testDelete() { $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569'; $table = TableRegistry::get('uuiditems'); $entity = $table->find('all')->where(['id' => $id])->first(); $this->assertTrue($table->delete($entity)); $query = $table->find('all')->where(['id' => $id]); $this->assertCount(0, $query->execute(), 'No rows left'); } /** * Tests that sql server does not error when an empty uuid is bound * * @return void */ public function testEmptyUuid() { $id = ''; $table = TableRegistry::get('uuiditems'); $entity = $table->find('all') ->where(['id' => $id]) ->first(); $this->assertNull($entity); } }