|
7 | 7 | namespace Commercetools\Core\CustomObject; |
8 | 8 |
|
9 | 9 | use Commercetools\Core\ApiTestCase; |
| 10 | +use Commercetools\Core\Error\ConcurrentModificationError; |
| 11 | +use Commercetools\Core\Model\CustomObject\CustomObject; |
10 | 12 | use Commercetools\Core\Model\CustomObject\CustomObjectDraft; |
11 | 13 | use Commercetools\Core\Request\CustomObjects\CustomObjectByKeyGetRequest; |
12 | 14 | use Commercetools\Core\Request\CustomObjects\CustomObjectCreateRequest; |
@@ -36,14 +38,92 @@ protected function createCustomObject(CustomObjectDraft $draft) |
36 | 38 | $response = $request->executeWithClient($this->getClient()); |
37 | 39 | $customObject = $request->mapResponse($response); |
38 | 40 |
|
39 | | - $this->cleanupRequests[] = CustomObjectDeleteRequest::ofIdAndVersion( |
| 41 | + $this->cleanupRequests[] = $this->deleteRequest = CustomObjectDeleteRequest::ofIdAndVersion( |
40 | 42 | $customObject->getId(), |
41 | 43 | $customObject->getVersion() |
42 | 44 | ); |
43 | 45 |
|
44 | 46 | return $customObject; |
45 | 47 | } |
46 | 48 |
|
| 49 | + public function testCustomObjectWithVersion() |
| 50 | + { |
| 51 | + $draft = $this->getDraft(); |
| 52 | + $customObject = $this->createCustomObject($draft); |
| 53 | + |
| 54 | + $request = CustomObjectCreateRequest::ofObject($customObject); |
| 55 | + $response = $request->executeWithClient($this->getClient()); |
| 56 | + $result = $request->mapResponse($response); |
| 57 | + $this->deleteRequest->setVersion($result->getVersion()); |
| 58 | + |
| 59 | + $this->assertNotSame($customObject->getVersion(), $result->getVersion()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testCustomObjectWithVersionConflict() |
| 63 | + { |
| 64 | + $draft = $this->getDraft(); |
| 65 | + $customObject = $this->createCustomObject($draft); |
| 66 | + |
| 67 | + $request = CustomObjectCreateRequest::ofObject($customObject); |
| 68 | + $response = $request->executeWithClient($this->getClient()); |
| 69 | + $result = $request->mapResponse($response); |
| 70 | + $this->deleteRequest->setVersion($result->getVersion()); |
| 71 | + |
| 72 | + $this->assertNotSame($customObject->getVersion(), $result->getVersion()); |
| 73 | + |
| 74 | + $request = CustomObjectCreateRequest::ofObject($customObject); |
| 75 | + $response = $request->executeWithClient($this->getClient()); |
| 76 | + |
| 77 | + $this->assertTrue($response->isError()); |
| 78 | + $this->assertInstanceOf( |
| 79 | + '\Commercetools\Core\Error\ConcurrentModificationError', |
| 80 | + $response->getErrors()->getByCode(ConcurrentModificationError::CODE) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + public function testCustomObjectDraftWithVersionConflict() |
| 85 | + { |
| 86 | + $draft = $this->getDraft(); |
| 87 | + $customObject = $this->createCustomObject($draft); |
| 88 | + |
| 89 | + $request = CustomObjectCreateRequest::ofObject($draft); |
| 90 | + $response = $request->executeWithClient($this->getClient()); |
| 91 | + $result = $request->mapResponse($response); |
| 92 | + $this->deleteRequest->setVersion($result->getVersion()); |
| 93 | + |
| 94 | + $this->assertNotSame($customObject->getVersion(), $result->getVersion()); |
| 95 | + |
| 96 | + $draft->setVersion($customObject->getVersion()); |
| 97 | + $request = CustomObjectCreateRequest::ofObject($draft); |
| 98 | + $response = $request->executeWithClient($this->getClient()); |
| 99 | + |
| 100 | + $this->assertTrue($response->isError()); |
| 101 | + $this->assertInstanceOf( |
| 102 | + '\Commercetools\Core\Error\ConcurrentModificationError', |
| 103 | + $response->getErrors()->getByCode(ConcurrentModificationError::CODE) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public function testValidTypes() |
| 108 | + { |
| 109 | + $this->assertInstanceOf( |
| 110 | + '\Commercetools\Core\Request\CustomObjects\CustomObjectCreateRequest', |
| 111 | + CustomObjectCreateRequest::ofObject(CustomObject::of()) |
| 112 | + ); |
| 113 | + $this->assertInstanceOf( |
| 114 | + '\Commercetools\Core\Request\CustomObjects\CustomObjectCreateRequest', |
| 115 | + CustomObjectCreateRequest::ofObject(CustomObjectDraft::of()) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @expectedException \InvalidArgumentException |
| 121 | + */ |
| 122 | + public function testInvalidType() |
| 123 | + { |
| 124 | + CustomObjectCreateRequest::ofObject(new \stdClass()); |
| 125 | + } |
| 126 | + |
47 | 127 | public function testQuery() |
48 | 128 | { |
49 | 129 | $draft = $this->getDraft(); |
|
0 commit comments