Skip to content

Commit 25f24f4

Browse files
committed
add view renaming
1 parent 5e2d1af commit 25f24f4

File tree

3 files changed

+84
-17
lines changed

3 files changed

+84
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ In addition, the `save` method of `DocumentHandler` will now understand the `ove
4141
option, which will turn an insert into a replace operation in case the insert fails with a
4242
unique constraint violation error on the primary key.
4343

44-
The method `insert` was introduced in `DocumentHandler` as an alias for `save` for consistency
45-
with the server-side naming.
44+
The method `insert` was introduced in `DocumentHandler` as an alias for the existing `save`
45+
method to be consistent with the server-side method naming.
46+
47+
Basic support for arangosearch views was added in 3.4.0, via the `View` and `ViewHandler`
48+
classes.
4649

4750

4851
Release notes for the ArangoDB-PHP driver 3.3.x

lib/ArangoDBClient/ViewHandler.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
*/
2121
class ViewHandler extends Handler
2222
{
23+
/**
24+
* rename option
25+
*/
26+
const OPTION_RENAME = 'rename';
27+
2328
/**
2429
* Create a view
2530
*
@@ -32,7 +37,7 @@ class ViewHandler extends Handler
3237
* @return array
3338
* @since 3.4
3439
*/
35-
public function createView(View $view)
40+
public function create(View $view)
3641
{
3742
$params = [
3843
View::ENTRY_NAME => $view->getName(),
@@ -57,7 +62,7 @@ public function createView(View $view)
5762
* @throws \ArangoDBClient\ClientException
5863
* @since 3.4
5964
*/
60-
public function getView($view)
65+
public function get($view)
6166
{
6267
$url = UrlHelper::buildUrl(Urls::URL_VIEW, [$view]);
6368

@@ -87,7 +92,6 @@ public function properties($view)
8792
}
8893

8994
$url = UrlHelper::buildUrl(Urls::URL_VIEW, [$view, 'properties']);
90-
9195
$result = $this->getConnection()->get($url);
9296

9397
return $result->getJson();
@@ -128,7 +132,7 @@ public function setProperties($view, array $properties)
128132
* @return bool - always true, will throw if there is an error
129133
* @since 3.4
130134
*/
131-
public function dropView($view)
135+
public function drop($view)
132136
{
133137
if ($view instanceof View) {
134138
$view = $view->getName();
@@ -139,6 +143,31 @@ public function dropView($view)
139143

140144
return true;
141145
}
146+
147+
/**
148+
* Rename a view
149+
*
150+
* @throws Exception
151+
*
152+
* @param mixed $view - view name as a string or instance of View
153+
* @param string $name - new name for collection
154+
*
155+
* @return bool - always true, will throw if there is an error
156+
*/
157+
public function rename($view, $name)
158+
{
159+
if ($view instanceof View) {
160+
$view = $view->getName();
161+
}
162+
163+
$params = [View::ENTRY_NAME => $name];
164+
$this->getConnection()->put(
165+
UrlHelper::buildUrl(Urls::URL_VIEW, [$view, self::OPTION_RENAME]),
166+
$this->json_encode_wrapper($params)
167+
);
168+
169+
return true;
170+
}
142171
}
143172

144173
class_alias(ViewHandler::class, '\triagens\ArangoDb\ViewHandler');

tests/ViewTest.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testCreateViewObject()
5252
public function testCreateView()
5353
{
5454
$this->view = new View('View1' . '_' . static::$testsTimestamp, 'arangosearch');
55-
$result = $this->viewHandler->createView($this->view);
55+
$result = $this->viewHandler->create($this->view);
5656
static::assertEquals('View1' . '_' . static::$testsTimestamp, $result['name']);
5757
static::assertEquals('arangosearch', $result['type']);
5858
}
@@ -63,9 +63,9 @@ public function testCreateView()
6363
public function testGetView()
6464
{
6565
$this->view = new View('View1' . '_' . static::$testsTimestamp, 'arangosearch');
66-
$this->viewHandler->createView($this->view);
66+
$this->viewHandler->create($this->view);
6767

68-
$result = $this->viewHandler->getView('View1' . '_' . static::$testsTimestamp, 'arangosearch');
68+
$result = $this->viewHandler->get('View1' . '_' . static::$testsTimestamp, 'arangosearch');
6969
static::assertEquals('View1' . '_' . static::$testsTimestamp, $result->getName());
7070
static::assertEquals('arangosearch', $result->getType());
7171
}
@@ -76,7 +76,7 @@ public function testGetView()
7676
public function testGetNonExistingView()
7777
{
7878
try {
79-
$this->viewHandler->getView('View1' . '_' . static::$testsTimestamp, 'arangosearch');
79+
$this->viewHandler->get('View1' . '_' . static::$testsTimestamp, 'arangosearch');
8080
} catch (\Exception $exception) {
8181
}
8282
static::assertEquals(404, $exception->getCode());
@@ -88,7 +88,7 @@ public function testGetNonExistingView()
8888
public function testViewProperties()
8989
{
9090
$this->view = new View('View1' . '_' . static::$testsTimestamp, 'arangosearch');
91-
$result = $this->viewHandler->createView($this->view);
91+
$result = $this->viewHandler->create($this->view);
9292
static::assertEquals('View1' . '_' . static::$testsTimestamp, $result['name']);
9393
static::assertEquals('arangosearch', $result['type']);
9494

@@ -103,7 +103,7 @@ public function testViewProperties()
103103
public function testViewSetProperties()
104104
{
105105
$this->view = new View('View1' . '_' . static::$testsTimestamp, 'arangosearch');
106-
$result = $this->viewHandler->createView($this->view);
106+
$result = $this->viewHandler->create($this->view);
107107
static::assertEquals('View1' . '_' . static::$testsTimestamp, $result['name']);
108108
static::assertEquals('arangosearch', $result['type']);
109109

@@ -124,8 +124,8 @@ public function testViewSetProperties()
124124
public function testDropView()
125125
{
126126
$this->view = new View('View1' . '_' . static::$testsTimestamp, 'arangosearch');
127-
$this->viewHandler->createView($this->view);
128-
$result = $this->viewHandler->dropView('View1' . '_' . static::$testsTimestamp);
127+
$this->viewHandler->create($this->view);
128+
$result = $this->viewHandler->drop('View1' . '_' . static::$testsTimestamp);
129129
static::assertTrue($result);
130130
}
131131

@@ -135,7 +135,42 @@ public function testDropView()
135135
public function testDropNonExistingView()
136136
{
137137
try {
138-
$this->viewHandler->dropView('View1' . '_' . static::$testsTimestamp);
138+
$this->viewHandler->drop('View1' . '_' . static::$testsTimestamp);
139+
} catch (\Exception $exception) {
140+
}
141+
static::assertEquals(404, $exception->getCode());
142+
}
143+
144+
/**
145+
* Test rename view
146+
*/
147+
public function testRenameView()
148+
{
149+
if (isCluster($this->connection)) {
150+
// don't execute this test in a cluster
151+
$this->markTestSkipped("test is only meaningful in a single server");
152+
return;
153+
}
154+
$this->view = new View('View1' . '_' . static::$testsTimestamp, 'arangosearch');
155+
$this->viewHandler->create($this->view);
156+
$result = $this->viewHandler->rename('View1' . '_' . static::$testsTimestamp, 'View2' . '_' . static::$testsTimestamp);
157+
static::assertTrue($result);
158+
}
159+
160+
/**
161+
* Test rename a non-existing view
162+
*/
163+
public function testRenameNonExistingView()
164+
{
165+
if (isCluster($this->connection)) {
166+
// don't execute this test in a cluster
167+
$this->markTestSkipped("test is only meaningful in a single server");
168+
return;
169+
}
170+
$this->view = new View('View1' . '_' . static::$testsTimestamp, 'arangosearch');
171+
$this->viewHandler->create($this->view);
172+
try {
173+
$this->viewHandler->rename('View2' . '_' . static::$testsTimestamp, 'View1' . '_' . static::$testsTimestamp);
139174
} catch (\Exception $exception) {
140175
}
141176
static::assertEquals(404, $exception->getCode());
@@ -145,11 +180,11 @@ public function tearDown()
145180
{
146181
$this->viewHandler = new ViewHandler($this->connection);
147182
try {
148-
$this->viewHandler->dropView('View1' . '_' . static::$testsTimestamp);
183+
$this->viewHandler->drop('View1' . '_' . static::$testsTimestamp);
149184
} catch (Exception $e) {
150185
}
151186
try {
152-
$this->viewHandler->dropView('View2' . '_' . static::$testsTimestamp);
187+
$this->viewHandler->drop('View2' . '_' . static::$testsTimestamp);
153188
} catch (Exception $e) {
154189
}
155190
}

0 commit comments

Comments
 (0)