Skip to content
This repository
Browse code

ENHANCEMENT Add YamlFixture String argument support

Enhanced YamlFixtureTest to cover this addition.
  • Loading branch information...
commit c2821902994bca9439a8579fd7d4df654db52c5b 1 parent 402297e
Michał Ochman authored June 29, 2012
52  dev/YamlFixture.php
@@ -84,19 +84,30 @@ class YamlFixture extends Object {
84 84
 	 * @var array
85 85
 	 */
86 86
 	protected $fixtureDictionary;
87  
-	
  87
+
  88
+	/**
  89
+	 * String containing fixture
  90
+	 *
  91
+	 * @var String
  92
+	 */
  93
+	protected $fixtureString;
  94
+
88 95
 	/**
89 96
 	 * @param String Absolute file path, or relative path to {@link Director::baseFolder()}
90 97
 	 */
91  
-	function __construct($fixtureFile) {
92  
-		if(!Director::is_absolute($fixtureFile)) $fixtureFile = Director::baseFolder().'/'. $fixtureFile;
93  
-		
94  
-		if(!file_exists($fixtureFile)) {
95  
-			throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixtureFile . '" not found');
  98
+	function __construct($fixture) {
  99
+		if(false !== strpos($fixture, "\n")) {
  100
+			$this->fixtureString = $fixture;
  101
+		} else {
  102
+			if(!Director::is_absolute($fixture)) $fixture = Director::baseFolder().'/'. $fixture;
  103
+
  104
+			if(!file_exists($fixture)) {
  105
+				throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixture . '" not found');
  106
+			}
  107
+
  108
+			$this->fixtureFile = $fixture;
96 109
 		}
97 110
 		
98  
-		$this->fixtureFile = $fixtureFile;
99  
-		
100 111
 		parent::__construct();
101 112
 	}
102 113
 	
@@ -106,6 +117,13 @@ function __construct($fixtureFile) {
106 117
 	function getFixtureFile() {
107 118
 		return $this->fixtureFile;
108 119
 	}
  120
+
  121
+	/**
  122
+	 * @return String Fixture string
  123
+	 */
  124
+	function getFixtureString() {
  125
+		return $this->fixtureString;
  126
+	}
109 127
 	
110 128
 	/**
111 129
 	 * Get the ID of an object from the fixture.
@@ -162,7 +180,11 @@ public function saveIntoDatabase(DataModel $model) {
162 180
 		DataObject::set_validation_enabled(false);
163 181
 		
164 182
 		$parser = new Spyc();
165  
-		$fixtureContent = $parser->loadFile($this->fixtureFile);
  183
+		if (isset($this->fixtureString)) {
  184
+			$fixtureContent = $parser->load($this->fixtureString);
  185
+		} else {
  186
+			$fixtureContent = $parser->loadFile($this->fixtureFile);
  187
+		}
166 188
 
167 189
 		$this->fixtureDictionary = array();
168 190
 		foreach($fixtureContent as $dataClass => $items) {
@@ -228,12 +250,12 @@ protected function writeDataObject($model, $dataClass, $items) {
228 250
 				}
229 251
 			}
230 252
 			$obj->write();
231  
-            //If LastEdited was set in the fixture, set it here
232  
-            if (array_key_exists('LastEdited', $fields)) {
233  
-                $manip = array($dataClass => array("command" => "update", "id" => $obj->id,
234  
-                    "fields" => array("LastEdited" => "'".$this->parseFixtureVal($fields['LastEdited'])."'")));
235  
-                DB::manipulate($manip);
236  
-            }
  253
+			//If LastEdited was set in the fixture, set it here
  254
+			if (array_key_exists('LastEdited', $fields)) {
  255
+				$manip = array($dataClass => array("command" => "update", "id" => $obj->id,
  256
+					"fields" => array("LastEdited" => "'".$this->parseFixtureVal($fields['LastEdited'])."'")));
  257
+				DB::manipulate($manip);
  258
+			}
237 259
 		}
238 260
 	}
239 261
 	
10  tests/testing/YamlFixtureTest.php
@@ -13,12 +13,22 @@ function testAbsoluteFixturePath() {
13 13
 		$absPath = FRAMEWORK_PATH . '/tests/testing/YamlFixtureTest.yml';
14 14
 		$obj = new YamlFixture($absPath);
15 15
 		$this->assertEquals($absPath, $obj->getFixtureFile());
  16
+		$this->assertNull($obj->getFixtureString());
16 17
 	}
17 18
 	
18 19
 	function testRelativeFixturePath() {
19 20
 		$relPath = FRAMEWORK_DIR . '/tests/testing/YamlFixtureTest.yml';
20 21
 		$obj = new YamlFixture($relPath);
21 22
 		$this->assertEquals(Director::baseFolder() . '/' . $relPath, $obj->getFixtureFile());
  23
+		$this->assertNull($obj->getFixtureString());
  24
+	}
  25
+
  26
+	function testStringFixture() {
  27
+		$absPath = FRAMEWORK_PATH . '/tests/testing/YamlFixtureTest.yml';
  28
+		$string = file_get_contents($absPath);
  29
+		$obj = new YamlFixture($string);
  30
+		$this->assertEquals($string, $obj->getFixtureString());
  31
+		$this->assertNull($obj->getFixtureFile());
22 32
 	}
23 33
 	
24 34
 	/**

0 notes on commit c282190

Please sign in to comment.
Something went wrong with that request. Please try again.