Skip to content
This repository

Clone2 #536

Merged
merged 3 commits into from over 2 years ago

4 participants

elinw Jools Louis Landry ianmacl
elinw

This adds a __clone method for handling deep cloning to JDatabaseQuery and JDatabaseQueryElement and tests for those methods.

elinw

Thanks Ian and Rob.

Jools
Collaborator

Unit testing complete. There were 0 failures and 0 errors from 1707 tests and 10645 assertions.
Checkstyle analysis reported 235 warnings and 1 errors.

Jools
Collaborator

Build triggered by changes to the head.

Unit testing complete. There were 0 failures and 0 errors from 1703 tests and 10653 assertions.
Checkstyle analysis reported 235 warnings and 0 errors.

Louis Landry LouisLandry merged commit 4b630a7 into from
Louis Landry LouisLandry closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 2 authors.

Nov 17, 2011
elinw Add missing __clone magic method to JDatabaseQuery and
JDatabaseQueryElement.
3202485
ianmacl Fixing tests
Finish __clone
da3eaa5
elinw Code style work. 0b34bce
This page is out of date. Refresh to see the latest.
38  libraries/joomla/database/databasequery.php
@@ -105,6 +105,25 @@ public function getElements()
105 105
 	{
106 106
 		return $this->elements;
107 107
 	}
  108
+
  109
+	/**
  110
+	 * Method to provide deep copy support to nested objects and arrays
  111
+	 * when cloning.
  112
+	 *
  113
+	 * @return  void
  114
+	 *
  115
+	 * @since   11.3
  116
+	 */
  117
+	public function __clone()
  118
+	{
  119
+		foreach ($this as $k => $v)
  120
+		{
  121
+			if (is_object($v) || is_array($v))
  122
+			{
  123
+				$this->{$k} = unserialize(serialize($v));
  124
+			}
  125
+		}
  126
+	}
108 127
 }
109 128
 
110 129
 /**
@@ -1142,4 +1161,23 @@ public function where($conditions, $glue = 'AND')
1142 1161
 
1143 1162
 		return $this;
1144 1163
 	}
  1164
+
  1165
+	/**
  1166
+	 * Method to provide deep copy support to nested objects and
  1167
+	 * arrays when cloning.
  1168
+	 *
  1169
+	 * @return  void
  1170
+	 *
  1171
+	 * @since   11.3
  1172
+	 */
  1173
+	public function __clone()
  1174
+	{
  1175
+		foreach ($this as $k => $v)
  1176
+		{
  1177
+			if (is_object($v) || is_array($v))
  1178
+			{
  1179
+				$this->{$k} = unserialize(serialize($v));
  1180
+			}
  1181
+		}
  1182
+	}
1145 1183
 }
39  tests/suite/joomla/database/JDatabaseQueryElementTest.php
@@ -224,20 +224,45 @@ public function testAppend($element, $append, $expected, $string)
224 224
 		$this->assertAttributeEquals(array($expectedElement), 'elements', $baseElement);
225 225
 	}
226 226
 
  227
+/**
  228
+	 * Tests the JDatabaseQueryElement::__clone method properly clones an array.
  229
+	 *
  230
+	 * @return  void
  231
+	 *
  232
+	 * @since   11.3
  233
+	 */
  234
+	public function test__clone_array()
  235
+	{
  236
+
  237
+		$baseElement = new JDatabaseQueryElement($name = null, $elements = null);
  238
+
  239
+		$baseElement->testArray = array();
  240
+
  241
+		$cloneElement = clone($baseElement);
  242
+
  243
+		$baseElement->testArray[] = 'a';
  244
+
  245
+		$this->assertFalse($baseElement === $cloneElement);
  246
+		$this->assertEquals(count($cloneElement->testArray), 0);
  247
+	}
  248
+
227 249
 	/**
228  
-	 * Tests the JDatabaseQueryElement::getElements method.
  250
+	 * Tests the JDatabaseQueryElement::__clone method properly clones an object.
229 251
 	 *
230 252
 	 * @return  void
231 253
 	 *
232 254
 	 * @since   11.3
233 255
 	 */
234  
-	public function testGetElements()
  256
+	public function test__clone_object()
235 257
 	{
236  
-		$e = new JDatabaseQueryElement('foo', 'bar');
237 258
 
238  
-		$this->assertThat(
239  
-			$e->getElements(),
240  
-			$this->equalTo(array('bar'))
241  
-		);
  259
+		$baseElement = new JDatabaseQueryElement($name = null, $elements = null);
  260
+
  261
+		$baseElement->testObject = new stdClass;
  262
+
  263
+		$cloneElement = clone($baseElement);
  264
+
  265
+		$this->assertFalse($baseElement === $cloneElement);
  266
+		$this->assertFalse($baseElement->testObject === $cloneElement->testObject);
242 267
 	}
243 268
 }
41  tests/suite/joomla/database/JDatabaseQueryTest.php
@@ -86,6 +86,7 @@ protected function setUp()
86 86
 		);
87 87
 	}
88 88
 
  89
+
89 90
 	/**
90 91
 	 * Test for the JDatabaseQuery::__call method.
91 92
 	 *
@@ -1272,4 +1273,44 @@ public function testWhere()
1272 1273
 			'Tests rendered value with glue.'
1273 1274
 		);
1274 1275
 	}
  1276
+	/**
  1277
+	* Tests the JDatabaseQuery::__clone method properly clones an array.
  1278
+	*
  1279
+	* @return  void
  1280
+	*
  1281
+	* @since   11.3
  1282
+	*/
  1283
+	public function test__clone_array()
  1284
+	{
  1285
+		$baseElement = new JDatabaseQueryInspector($this->getMockDatabase());
  1286
+
  1287
+		$baseElement->testArray = array();
  1288
+
  1289
+		$cloneElement = clone($baseElement);
  1290
+
  1291
+		$baseElement->testArray[] = 'test';
  1292
+
  1293
+		$this->assertFalse($baseElement === $cloneElement);
  1294
+		$this->assertTrue(count($cloneElement->testArray) == 0);
  1295
+	}
  1296
+
  1297
+	/**
  1298
+	 * Tests the JDatabaseQuery::__clone method properly clones an object.
  1299
+	 *
  1300
+	 * @return  void
  1301
+	 *
  1302
+	 * @since   11.3
  1303
+	 */
  1304
+	public function test__clone_object()
  1305
+	{
  1306
+		$baseElement = new JDatabaseQueryInspector($this->getMockDatabase());
  1307
+
  1308
+		$baseElement->testObject = new stdClass;
  1309
+
  1310
+		$cloneElement = clone($baseElement);
  1311
+
  1312
+		$this->assertFalse($baseElement === $cloneElement);
  1313
+
  1314
+		$this->assertFalse($baseElement->testObject === $cloneElement->testObject);
  1315
+	}
1275 1316
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.