Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
anis
anis-server
Commits
a6c164f2
Commit
a6c164f2
authored
Dec 19, 2019
by
François Agneray
Browse files
DatasetListAction => done
parent
89dace27
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/Action/DatasetListAction.php
View file @
a6c164f2
...
...
@@ -18,9 +18,7 @@ use Slim\Exception\HttpBadRequestException;
use
Slim\Exception\HttpNotFoundException
;
use
Doctrine\ORM\EntityManagerInterface
;
use
App\Utils\DBALConnectionFactory
;
use
App\Entity\Database
;
use
App\Entity\Project
;
use
App\Entity\Instance
;
use
App\Entity\DatasetFamily
;
use
App\Entity\Dataset
;
use
App\Entity\Attribute
;
...
...
@@ -42,7 +40,7 @@ final class DatasetListAction extends AbstractAction
}
/**
* `GET` Returns a list of all datasets
listed in the metamodel database
* `GET` Returns a list of all datasets
for a given dataset family
* `POST` Add a new dataset
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
...
...
@@ -57,18 +55,18 @@ final class DatasetListAction extends AbstractAction
return
$response
->
withHeader
(
'Access-Control-Allow-Methods'
,
'GET, POST, OPTIONS'
);
}
$
instance
=
$this
->
em
->
find
(
'App\Entity\
Instance
'
,
$args
[
'
name
'
]);
$
datasetFamily
=
$this
->
em
->
find
(
'App\Entity\
DatasetFamily
'
,
$args
[
'
id
'
]);
// Returns HTTP 404 if the
instance
is not found
if
(
is_null
(
$
instance
))
{
// Returns HTTP 404 if the
dataset family
is not found
if
(
is_null
(
$
datasetFamily
))
{
throw
new
HttpNotFoundException
(
$request
,
'
Instance
with
name
'
.
$args
[
'
name
'
]
.
' is not found'
'
Dataset family
with
id
'
.
$args
[
'
id
'
]
.
' is not found'
);
}
if
(
$request
->
getMethod
()
===
GET
)
{
$datasets
=
$this
->
em
->
getRepository
(
'App\Entity\Dataset'
)
->
findBy
Instance
(
$instance
);
$datasets
=
$this
->
em
->
getRepository
(
'App\Entity\Dataset'
)
->
findBy
DatasetFamily
(
$datasetFamily
);
$payload
=
json_encode
(
$datasets
);
}
...
...
@@ -86,8 +84,7 @@ final class DatasetListAction extends AbstractAction
'vo'
,
'data_path'
,
'selectable_row'
,
'project_name'
,
'id_dataset_family'
'project_name'
);
foreach
(
$fields
as
$a
)
{
if
(
$this
->
isEmptyField
(
$a
,
$parsedBody
))
{
...
...
@@ -107,15 +104,7 @@ final class DatasetListAction extends AbstractAction
);
}
$family
=
$this
->
em
->
find
(
'App\Entity\DatasetFamily'
,
$parsedBody
[
'id_dataset_family'
]);
if
(
is_null
(
$family
))
{
throw
new
HttpBadRequestException
(
$request
,
'Dataset family with id '
.
$parsedBody
[
'id_dataset_family'
]
.
' is not found'
);
}
$dataset
=
$this
->
postDataset
(
$parsedBody
,
$project
,
$instance
,
$family
);
$dataset
=
$this
->
postDataset
(
$parsedBody
,
$project
,
$datasetFamily
);
$payload
=
json_encode
(
$dataset
);
$response
=
$response
->
withStatus
(
201
);
}
...
...
@@ -136,8 +125,7 @@ final class DatasetListAction extends AbstractAction
private
function
postDataset
(
array
$parsedBody
,
Project
$project
,
Instance
$instance
,
DatasetFamily
$family
DatasetFamily
$datasetFamily
):
Dataset
{
$dataset
=
new
Dataset
(
$parsedBody
[
'name'
]);
$dataset
->
setTableRef
(
$parsedBody
[
'table_ref'
]);
...
...
@@ -149,8 +137,7 @@ final class DatasetListAction extends AbstractAction
$dataset
->
setDataPath
(
$parsedBody
[
'data_path'
]);
$dataset
->
setSelectableRow
(
$parsedBody
[
'selectable_row'
]);
$dataset
->
setProject
(
$project
);
$dataset
->
setInstance
(
$instance
);
$dataset
->
setDatasetFamily
(
$family
);
$dataset
->
setDatasetFamily
(
$datasetFamily
);
$this
->
em
->
persist
(
$dataset
);
$this
->
postAttributes
(
$dataset
);
...
...
tests/Action/DatasetListActionTest.php
View file @
a6c164f2
...
...
@@ -47,12 +47,12 @@ final class DatasetListActionTest extends TestCase
$this
->
assertSame
(
$response
->
getHeaderLine
(
'Access-Control-Allow-Methods'
),
'GET, POST, OPTIONS'
);
}
public
function
test
Instance
NotFound
():
void
public
function
test
DatasetFamily
NotFound
():
void
{
$this
->
expectException
(
HttpNotFoundException
::
class
);
$this
->
expectExceptionMessage
(
'
Instance with name aspic
is not found'
);
$this
->
expectExceptionMessage
(
'
Dataset family with id 1
is not found'
);
$request
=
$this
->
getRequest
(
'GET'
);
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
name
'
=>
'aspic'
));
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
id
'
=>
1
));
$this
->
assertEquals
(
404
,
(
int
)
$response
->
getStatusCode
());
}
...
...
@@ -60,7 +60,7 @@ final class DatasetListActionTest extends TestCase
{
$datasets
=
$this
->
addDatasets
();
$request
=
$this
->
getRequest
(
'GET'
);
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
name
'
=>
'aspic'
));
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
id
'
=>
1
));
$this
->
assertSame
(
json_encode
(
$datasets
),
(
string
)
$response
->
getBody
()
...
...
@@ -69,45 +69,32 @@ final class DatasetListActionTest extends TestCase
public
function
testAddANewDatasetEmptyNameField
():
void
{
$this
->
add
Instance
();
$this
->
add
DatasetFamily
();
$this
->
expectException
(
HttpBadRequestException
::
class
);
$this
->
expectExceptionMessage
(
'Param name needed to add a new dataset'
);
$request
=
$this
->
getRequest
(
'POST'
)
->
withParsedBody
(
array
());
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
name
'
=>
'aspic'
));
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
id
'
=>
1
));
$this
->
assertEquals
(
400
,
(
int
)
$response
->
getStatusCode
());
}
public
function
testAddANewDatasetProjectNotFound
():
void
{
$this
->
add
Instance
();
$this
->
add
DatasetFamily
();
$this
->
expectException
(
HttpBadRequestException
::
class
);
$this
->
expectExceptionMessage
(
'Project with name anis_project is not found'
);
$fields
=
$this
->
getNewDatasetFields
();
$request
=
$this
->
getRequest
(
'POST'
)
->
withParsedBody
(
$fields
);
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'name'
=>
'aspic'
));
$this
->
assertEquals
(
400
,
(
int
)
$response
->
getStatusCode
());
}
public
function
testAddANewDatasetFamilyNotFound
():
void
{
$this
->
addProject
();
$this
->
addInstance
();
$this
->
expectException
(
HttpBadRequestException
::
class
);
$this
->
expectExceptionMessage
(
'Dataset family with id 1 is not found'
);
$fields
=
$this
->
getNewDatasetFields
();
$request
=
$this
->
getRequest
(
'POST'
)
->
withParsedBody
(
$fields
);
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'name'
=>
'aspic'
));
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'id'
=>
1
));
$this
->
assertEquals
(
400
,
(
int
)
$response
->
getStatusCode
());
}
public
function
testAddANewDataset
():
void
{
$this
->
addProject
();
$this
->
addInstance
();
$this
->
addDatasetFamily
();
$fields
=
$this
->
getNewDatasetFields
();
$request
=
$this
->
getRequest
(
'POST'
)
->
withParsedBody
(
$fields
);
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
name
'
=>
'aspic'
));
$response
=
(
$this
->
action
)(
$request
,
new
Response
(),
array
(
'
id
'
=>
1
));
$this
->
assertSame
(
json_encode
(
$fields
),
(
string
)
$response
->
getBody
()
...
...
@@ -140,7 +127,6 @@ final class DatasetListActionTest extends TestCase
'data_path'
=>
'/mnt/dataset1'
,
'selectable_row'
=>
false
,
'project_name'
=>
'anis_project'
,
'instance_name'
=>
'aspic'
,
'id_dataset_family'
=>
1
);
}
...
...
@@ -180,7 +166,9 @@ final class DatasetListActionTest extends TestCase
private
function
addDatasetFamily
():
DatasetFamily
{
$family
=
new
DatasetFamily
();
$instance
=
$this
->
addInstance
();
$family
=
new
DatasetFamily
(
$instance
);
$family
->
setLabel
(
'Default dataset'
);
$family
->
setDisplay
(
10
);
$this
->
entityManager
->
persist
(
$family
);
...
...
@@ -192,7 +180,6 @@ final class DatasetListActionTest extends TestCase
private
function
addDatasets
():
array
{
$project
=
$this
->
addProject
();
$instance
=
$this
->
addInstance
();
$family
=
$this
->
addDatasetFamily
();
$dataset1
=
new
Dataset
(
'dataset1'
);
...
...
@@ -205,7 +192,6 @@ final class DatasetListActionTest extends TestCase
$dataset1
->
setDataPath
(
'/mnt/dataset1'
);
$dataset1
->
setSelectableRow
(
false
);
$dataset1
->
setProject
(
$project
);
$dataset1
->
setInstance
(
$instance
);
$dataset1
->
setDatasetFamily
(
$family
);
$this
->
entityManager
->
persist
(
$dataset1
);
...
...
@@ -219,7 +205,6 @@ final class DatasetListActionTest extends TestCase
$dataset2
->
setDataPath
(
'/mnt/dataset2'
);
$dataset2
->
setSelectableRow
(
false
);
$dataset2
->
setProject
(
$project
);
$dataset2
->
setInstance
(
$instance
);
$dataset2
->
setDatasetFamily
(
$family
);
$this
->
entityManager
->
persist
(
$dataset2
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment