Scalr\Tests\Functional\Ui\Controller\Scaling\MetricsTest::testXSave PHP Method

testXSave() public method

public testXSave ( )
    public function testXSave()
    {
        //in first case try to create metric with invalid name and receive error
        $response = $this->request('/scaling/metrics/xSave', ['name' => 'Invalid-metric-Name!', 'filePath' => '', 'retrieveMethod' => 'read', 'calcFunction' => 'avg', 'algorithm' => 'Sensor'], 'POST');
        $this->assertArrayHasKey('errors', $response);
        if ($response['success'] && isset($response['metric'])) {
            $this->request('/scaling/metrics/xRemove', ['metrics' => json_encode([$response['metric']['id']])], 'POST');
            $this->assertTrue(false, 'Metric with invalid name creates success without validation errors.');
        }
        //then create valid metric
        $response = $this->request('/scaling/metrics/xSave', ['name' => $this->testMetricName, 'filePath' => '', 'retrieveMethod' => 'read', 'calcFunction' => 'avg', 'algorithm' => 'Sensor'], 'POST');
        $this->assertArrayHasKey('success', $response);
        $this->assertArrayHasKey('successMessage', $response);
        $this->assertArrayHasKey('metric', $response);
        $id = $response['metric']['id'];
        //then try to create metric with same name and receive error
        $response = $this->request('/scaling/metrics/xSave', ['name' => $this->testMetricName, 'filePath' => '', 'retrieveMethod' => 'execute', 'calcFunction' => 'sum', 'algorithm' => 'DateTime'], 'POST');
        $this->assertArrayHasKey('errors', $response);
        if ($response['success'] && isset($response['metric'])) {
            $this->request('/scaling/metrics/xRemove', ['metrics' => json_encode([$response['metric']['id']])], 'POST');
            $this->assertTrue(false, 'Metric with duplicate name creates success without validation errors.');
        }
        //and finally remove this metric
        $response = $this->request('/scaling/metrics/xRemove', ['metrics' => json_encode([$id])], 'POST');
        $this->assertArrayHasKey('success', $response);
        $this->assertArrayHasKey('successMessage', $response);
    }