Scalr\Tests\Model\AbstractEntityTest::testFunctionalSeveralUniqueKeys PHP Method

testFunctionalSeveralUniqueKeys() public method

SCALRCORE-951 we should avoid ON DUPLICATE KEY UPDATE clause on tables with multiple unique indexes
    public function testFunctionalSeveralUniqueKeys()
    {
        $db = \Scalr::getDb();
        //Removes previously created records if they exist
        $this->cleanupCloudLocations();
        $cl2Identifier = CloudLocation::calculateCloudLocationId(self::CL_PLATFORM, self::CL_NAME, self::CL_URL);
        $cl3Identifier = CloudLocation::calculateCloudLocationId(self::CL_PLATFORM, self::CL_NAME . '3', self::CL_URL);
        //Creates the first record
        $cl = $this->getCloudLocationEntity();
        $cl->save();
        unset($cl);
        //Checks if it has been saved properly
        $cl = CloudLocation::findPk(self::CL_LOC_ID);
        $this->assertInstanceOf('Scalr\\Model\\Entity\\CloudLocation', $cl);
        $this->assertEquals(self::CL_LOC_ID, $cl->cloudLocationId);
        $this->assertEquals(self::CL_URL, $cl->url);
        $this->assertEquals(self::CL_NAME, $cl->cloudLocation);
        $this->assertEquals(self::CL_PLATFORM, $cl->platform);
        $cl3 = $this->getCloudLocationEntity($cl3Identifier);
        $cl3->cloudLocation = self::CL_NAME . '3';
        $cl3->save();
        //Record with this unique key already exists
        $cl3->cloudLocation = self::CL_NAME;
        //Saving record with existing unique key
        $ex = false;
        try {
            $cl3->save();
            $this->cleanupCloudLocations();
        } catch (Exception $e) {
            //"Duplicate entry 'test--test' for key 'idx_unique'"
            $ex = true;
            $this->assertContains("Duplicate entry", $e->getMessage());
        }
        $this->assertTrue($ex, "Duplicate entry 'test--test' for key 'idx_unique' must be thrown here (3)");
        //Trying to create with the same unique key as $cl but different primary key
        $cl2 = $this->getCloudLocationEntity($cl2Identifier);
        //Checks they're different
        $this->assertNotEquals(self::CL_LOC_ID, $cl2Identifier);
        //unique key should cause error, and should not be just ignored
        $ex = false;
        try {
            $cl2->save();
            $this->cleanupCloudLocations();
        } catch (Exception $e) {
            $ex = true;
            $this->assertContains("Duplicate entry", $e->getMessage());
        }
        $this->assertTrue($ex, "Duplicate entry 'test--test' for key 'idx_unique' must be thrown here (2)");
        $this->assertNull(CloudLocation::findPk($cl2Identifier));
        $this->cleanupCloudLocations();
    }