Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

[wip] Enable deleteList #156

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ test/build
.buildpath
.project
.settings/
.idea
.idea
test/coverage
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ EVENT_PATCH_LIST_PRE = 'patch-all.pre';
EVENT_PATCH_LIST_POST = 'patch-all.post';
EVENT_DELETE_PRE = 'delete.pre';
EVENT_DELETE_POST = 'delete.post';
EVENT_DELETE_LIST_PRE = 'delete-list.pre';
EVENT_DELETE_LIST_POST = 'delete-list.post';
```
```

Attach to events through the Shared Event Manager:
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
],
"require-dev": {
"zfcampus/zf-apigility-admin": "~1.0",
"zendframework/zend-test": ">=2.3",
"zendframework/zend-form": ">=2.3",
"zendframework/zend-serializer": ">=2.3",
"zendframework/zend-log": ">=2.3",
"zendframework/zend-test": ">=2.4",
"zendframework/zend-form": ">=2.4",
"zendframework/zend-serializer": ">=2.4",
"zendframework/zend-log": ">=2.4",
"phpunit/phpunit": "3.7.*",
"doctrine/doctrine-orm-module": "*",
"doctrine/doctrine-mongo-odm-module": "*",
Expand All @@ -30,7 +30,8 @@
"require": {
"php": ">=5.3",
"phpro/zf-doctrine-hydration-module": ">=0.1.5",
"zfcampus/zf-apigility": "~1.0"
"zfcampus/zf-apigility": "~1.0",
"zfcampus/zf-rest": ">=1.0.4"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
28 changes: 15 additions & 13 deletions src/Server/Event/DoctrineResourceEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
*/
class DoctrineResourceEvent extends Event
{
const EVENT_FETCH_POST = 'fetch.post';
const EVENT_FETCH_ALL_PRE = 'fetch-all.pre';
const EVENT_FETCH_ALL_POST = 'fetch-all.post';
const EVENT_CREATE_PRE = 'create.pre';
const EVENT_CREATE_POST = 'create.post';
const EVENT_UPDATE_PRE = 'update.pre';
const EVENT_UPDATE_POST = 'update.post';
const EVENT_PATCH_PRE = 'patch.pre';
const EVENT_PATCH_POST = 'patch.post';
const EVENT_PATCH_LIST_PRE = 'patch-list.pre';
const EVENT_PATCH_LIST_POST = 'patch-list.post';
const EVENT_DELETE_PRE = 'delete.pre';
const EVENT_DELETE_POST = 'delete.post';
const EVENT_FETCH_POST = 'fetch.post';
const EVENT_FETCH_ALL_PRE = 'fetch-all.pre';
const EVENT_FETCH_ALL_POST = 'fetch-all.post';
const EVENT_CREATE_PRE = 'create.pre';
const EVENT_CREATE_POST = 'create.post';
const EVENT_UPDATE_PRE = 'update.pre';
const EVENT_UPDATE_POST = 'update.post';
const EVENT_PATCH_PRE = 'patch.pre';
const EVENT_PATCH_POST = 'patch.post';
const EVENT_PATCH_LIST_PRE = 'patch-list.pre';
const EVENT_PATCH_LIST_POST = 'patch-list.post';
const EVENT_DELETE_PRE = 'delete.pre';
const EVENT_DELETE_POST = 'delete.post';
const EVENT_DELETE_LIST_PRE = 'delete-list.pre';
const EVENT_DELETE_LIST_POST = 'delete-list.post';

/**
* @var ResourceEvent
Expand Down
38 changes: 33 additions & 5 deletions src/Server/Resource/DoctrineResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public function delete($id)

$this->getObjectManager()->remove($entity);
$this->getObjectManager()->flush();

$results = $this->triggerDoctrineEvent(DoctrineResourceEvent::EVENT_DELETE_POST, $entity);
if ($results->last() instanceof ApiProblem) {
return $results->last();
Expand Down Expand Up @@ -390,15 +391,42 @@ public function patchList($data)
}

/**
* Delete a collection, or members of a collection
* Delete a list of entities
*
* @param mixed $data
* @return ApiProblem|mixed
* @codeCoverageIgnore
* @param mixed $data
* @return ApiProblem|mixed
*/
public function deleteList($data)
{
return new ApiProblem(405, 'The DELETE method has not been defined for collections');
$results = $this->triggerDoctrineEvent(DoctrineResourceEvent::EVENT_DELETE_LIST_PRE, $data);
if ($results->last() instanceof ApiProblem) {
// @codeCoverageIgnoreStart
return $results->last();
}
// @codeCoverageIgnoreEnd

$this->getObjectManager()->getConnection()->beginTransaction();
foreach ($data as $row) {
$result = $this->delete($row[$this->getEntityIdentifierName()]);

if ($result instanceof ApiProblem) {
// @codeCoverageIgnoreStart
$this->getObjectManager()->getConnection()->rollback();

return $result;
// @codeCoverageIgnoreEnd
}
}
$this->getObjectManager()->getConnection()->commit();

$results = $this->triggerDoctrineEvent(DoctrineResourceEvent::EVENT_DELETE_LIST_POST, true);
if ($results->last() instanceof ApiProblem) {
// @codeCoverageIgnoreStart
return $results->last();
}
// @codeCoverageIgnoreEnd

return true;
}

/**
Expand Down
99 changes: 80 additions & 19 deletions test/src/Server/ORM/CRUD/CRUDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function testCreate()
{
$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_POST);
Expand Down Expand Up @@ -112,7 +112,7 @@ public function testFetch()

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Accept' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_GET);
Expand All @@ -126,7 +126,7 @@ public function testFetch()
// Test fetch() of resource with non-primary key identifier
$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Accept' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_GET);
Expand Down Expand Up @@ -174,7 +174,7 @@ public function testFetchAll()

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Accept' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_GET);
Expand Down Expand Up @@ -223,8 +223,8 @@ public function testPatch()

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_PATCH);
Expand Down Expand Up @@ -284,8 +284,8 @@ public function testPatchList()

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_POST);
Expand Down Expand Up @@ -322,8 +322,8 @@ public function testPatchList()

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_PATCH);
Expand Down Expand Up @@ -390,8 +390,8 @@ public function testPut()

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_PUT);
Expand Down Expand Up @@ -458,7 +458,7 @@ public function testDelete()

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Accept' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_DELETE);
Expand All @@ -468,8 +468,8 @@ public function testDelete()
$this->assertEmpty($em->getRepository('ZFTestApigilityDb\Entity\Artist')->find($id));
$this->validateTriggeredEvents(
array(
DoctrineResourceEvent::EVENT_DELETE_PRE,
DoctrineResourceEvent::EVENT_DELETE_POST,
DoctrineResourceEvent::EVENT_DELETE_PRE,
DoctrineResourceEvent::EVENT_DELETE_POST,
)
);

Expand Down Expand Up @@ -515,7 +515,7 @@ function (DoctrineResourceEvent $e) {

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Accept' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_DELETE);
Expand All @@ -525,12 +525,73 @@ function (DoctrineResourceEvent $e) {
$this->validateTriggeredEvents(array());
}


public function testDeleteList()
{
$serviceManager = $this->getApplication()->getServiceManager();
$em = $serviceManager->get('doctrine.entitymanager.orm_default');
$deleteList = array();

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_POST);
$this->getRequest()->setContent('{"name": "ArtistOne","createdAt": "2011-12-18 13:17:17"}');
$this->dispatch('/test/artist');
$body = json_decode($this->getResponse()->getBody(), true);

$deleteList[] = array(
'id' => $body['id'],
);

$this->getRequest()->setMethod(Request::METHOD_POST);
$this->getRequest()->setContent('{"name": "ArtistTwo","createdAt": "2011-12-18 13:17:17"}');
$this->dispatch('/test/artist');
$body = json_decode($this->getResponse()->getBody(), true);

$deleteList[] = array(
'id' => $body['id'],
);

$this->getRequest()->setMethod(Request::METHOD_POST);
$this->getRequest()->setContent('{"name": "ArtistThree","createdAt": "2011-12-18 13:17:17"}');
$this->dispatch('/test/artist');
$body = json_decode($this->getResponse()->getBody(), true);

$deleteList[] = array(
'id' => $body['id'],
);

$em->clear();

$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_DELETE);
$this->getRequest()->setContent(json_encode($deleteList));
$this->dispatch('/test/artist');
$this->assertEquals(204, $this->getResponseStatusCode());

$this->validateTriggeredEventsContains(array(
DoctrineResourceEvent::EVENT_DELETE_PRE,
DoctrineResourceEvent::EVENT_DELETE_POST,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above two are not triggered by deleteList(), and are the source of the unit test failure. I've removed them locally for the merge review.

DoctrineResourceEvent::EVENT_DELETE_LIST_PRE,
DoctrineResourceEvent::EVENT_DELETE_LIST_POST,
));
}

public function testRpcController()
{
$this->getRequest()->getHeaders()->addHeaders(
array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Accept' => 'application/json',
'Content-type' => 'application/json',
)
);
$this->getRequest()->setMethod(Request::METHOD_POST);
Expand Down
2 changes: 2 additions & 0 deletions test/src/Server/ORM/Setup/ApigilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function testBuildOrmApi()
0 => 'GET',
1 => 'POST',
2 => 'PATCH',
3 => 'DELETE',
),
);

Expand All @@ -67,6 +68,7 @@ public function testBuildOrmApi()
0 => 'GET',
1 => 'POST',
2 => 'PATCH',
3 => 'DELETE',
),
);

Expand Down