action = new \App\Action\Meta\DatasetAction( new \Psr\Log\NullLogger(), $this->metaEntityManagerFactory ); } protected function getRequestForPut(array $parsedBody): Request { $environment = \Slim\Http\Environment::mock([ 'REQUEST_METHOD' => 'PUT', 'REQUEST_URI' => '/metadata/dataset/corot_targets', 'CONTENT_TYPE' => 'application/json', ]); return \Slim\Http\Request::createFromEnvironment($environment)->withParsedBody($parsedBody); } public function testDatasetIsNotFound(): void { $environment = \Slim\Http\Environment::mock([ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/metadata/dataset/undifined' ]); $request = \Slim\Http\Request::createFromEnvironment($environment); $response = ($this->action)( $request, new \Slim\Http\Response(), array('name' => 'undifined', 'instance' => 'default') ); $this->assertEquals(404, (int) $response->getStatusCode()); $this->assertSame((string) $response->getBody(), json_encode(array( 'error' => 'Invalid request', 'error_description' => 'Dataset with name undifined is not found' ))); } public function testGetDataset(): void { $environment = \Slim\Http\Environment::mock([ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/metadata/dataset/corot_targets' ]); $request = \Slim\Http\Request::createFromEnvironment($environment); $response = ($this->action)( $request, new \Slim\Http\Response(), array('name' => 'corot_targets', 'instance' => 'default') ); $this->assertEquals(200, (int) $response->getStatusCode()); $this->assertSame((string) $response->getBody(), json_encode(array( 'name' => 'corot_targets', 'table_ref' => 'v_corot_targets', 'label' => 'CoRoT Targets', 'description' => 'CoRoT Targets', 'display' => 10, 'count' => 100582, 'vo' => true, 'data_path' => '/tmp/data', 'selectable_row' => false, 'project_name' => 'corot', 'id_dataset_family' => 1 ))); } public function testLabelIsEmpty(): void { $this->fieldIsEmpty('label'); } public function testDescriptionIsEmpty(): void { $this->fieldIsEmpty('description'); } public function testIdDatasetFamilyIsEmpty(): void { $this->fieldIsEmpty('id_dataset_family'); } public function testDisplayIsEmpty(): void { $this->fieldIsEmpty('display'); } public function testCountIsEmpty(): void { $this->fieldIsEmpty('count'); } public function testVoIsEmpty(): void { $this->fieldIsEmpty('vo'); } public function testDataPathIsEmpty(): void { $this->fieldIsEmpty('data_path'); } public function testEditDataset(): void { $editedDataset = $this->getEditedDataset(); $request = $this->getRequestForPut($editedDataset); $response = ($this->action)( $request, new \Slim\Http\Response(), array('name' => 'corot_targets', 'instance' => 'default') ); $this->assertEquals(200, (int) $response->getStatusCode()); $this->assertSame((string) $response->getBody(), json_encode($editedDataset)); $this->assertSame(2, $this->getDatabaseTester()->getConnection()->getRowCount('dataset')); } public function testRemoveDataset(): void { $environment = \Slim\Http\Environment::mock([ 'REQUEST_METHOD' => 'DELETE', 'REQUEST_URI' => '/metadata/dataset/corot_targets' ]); $request = \Slim\Http\Request::createFromEnvironment($environment); $response = ($this->action)( $request, new \Slim\Http\Response(), array('name' => 'corot_targets', 'instance' => 'default') ); $this->assertEquals(200, (int) $response->getStatusCode()); $this->assertSame( (string) $response->getBody(), json_encode(array('message' => 'Dataset with name corot_targets is removed!')) ); $this->assertSame(1, $this->getDatabaseTester()->getConnection()->getRowCount('dataset')); } private function fieldIsEmpty($field): void { $editedDataset = $this->getEditedDataset(); unset($editedDataset[$field]); $request = $this->getRequestForPut($editedDataset); $response = ($this->action)( $request, new \Slim\Http\Response(), array('name' => 'corot_targets', 'instance' => 'default') ); $this->assertEquals(400, (int) $response->getStatusCode()); $this->assertSame((string) $response->getBody(), json_encode(array( 'error' => 'Invalid request', 'error_description' => 'Param ' . $field . ' needed to edit the dataset' ))); } private function getEditedDataset(): array { return array( 'name' => 'corot_targets', 'table_ref' => 'v_corot_targets', 'label' => 'Edited dataset', 'description' => 'Edited dataset description', 'display' => 50, 'count' => 100583, 'vo' => true, 'data_path' => '/tmp/data', 'selectable_row' => false, 'project_name' => 'corot', 'id_dataset_family' => 1 ); } }