diff --git a/.gitignore b/.gitignore
index 0a94a6c..7293b13 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
# the project itself is not installable, so we'll make sure nothing from composer gets into the repo
vendor
composer.lock
+build
diff --git a/composer.json b/composer.json
index 2cdc960..155dd71 100644
--- a/composer.json
+++ b/composer.json
@@ -29,10 +29,14 @@
"illuminate/database": "~4.1",
"illuminate/validation": "~4.1"
},
+ "require-dev": {
+ "phpunit/phpunit": "~3.7",
+ "mockery/mockery": "~0.8"
+ },
"autoload": {
"psr-0": {
"LaravelBook\\Ardent": "src/"
}
},
- "minimum-stability": "dev"
+ "minimum-stability": "stable"
}
diff --git a/phpunit.xml b/phpunit.xml
index e601408..96d69d5 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -1,7 +1,7 @@
./tests/
-
\ No newline at end of file
+
+
+
+
+
+
+
+
+ src/LaravelBook/Ardent
+
+ vendor
+
+
+
+
diff --git a/tests/.gitkeep b/tests/.gitkeep
deleted file mode 100644
index e69de29..0000000
diff --git a/tests/LaravelBook/Ardent/Test/TestCase.php b/tests/LaravelBook/Ardent/Test/TestCase.php
new file mode 100644
index 0000000..2a613a2
--- /dev/null
+++ b/tests/LaravelBook/Ardent/Test/TestCase.php
@@ -0,0 +1,30 @@
+validator = m::mock('Illuminate\Validation\Validator')
+ ->shouldIgnoreMissing();
+
+ Validator::shouldReceive('make')
+ ->andReturn($this->validator);
+
+ Input::shouldReceive('hasSessionStore')
+ ->andReturn(false);
+ }
+
+ public function teardown()
+ {
+ Input::clearResolvedInstances();
+ Validator::clearResolvedInstances();
+
+ m::close();
+ parent::teardown();
+ }
+}
diff --git a/tests/LaravelBook/Ardent/Test/ValidationTest.php b/tests/LaravelBook/Ardent/Test/ValidationTest.php
new file mode 100644
index 0000000..3cd6935
--- /dev/null
+++ b/tests/LaravelBook/Ardent/Test/ValidationTest.php
@@ -0,0 +1,167 @@
+shouldReceive('validate')->once()
+ ->andReturn(false);
+
+ $model->save();
+ }
+
+ public function testValidationFailurePreventsSave()
+ {
+ $model = m::mock('LaravelBook\Ardent\Test\ValidatingModel[validate]');
+
+ $model->shouldReceive('validate')
+ ->andReturn(false);
+
+ $model->save();
+
+ $this->assertEquals(0, $model->saveCalled);
+ }
+
+ /**
+ * @expectedException LaravelBook\Ardent\InvalidModelException
+ */
+ public function testValidationThrowsWhenConfigured()
+ {
+ $model = new ValidatingModel;
+ $model->throwOnValidation = true;
+
+ $this->validator->shouldReceive('passes')
+ ->andReturn(false);
+
+ $model->validate();
+ }
+
+ public function testValidationSuccessAllowsSave()
+ {
+ $model = m::mock('LaravelBook\Ardent\Test\ValidatingModel[validate]');
+
+ $model->shouldReceive('validate')
+ ->andReturn(true);
+
+ $model->save();
+
+ $this->assertEquals(1, $model->saveCalled);
+ }
+
+ public function testValidationUsesPassedRules()
+ {
+ Validator::clearResolvedInstances();
+
+ $model = new ValidatingModel;
+
+ $rules = array('hello' => uniqid());
+
+ Input::shouldReceive('hasSessionStore');
+
+ Validator::shouldReceive('make')->once()
+ ->with(m::any(), $rules, m::any())
+ ->andReturn($this->validator);
+
+ $model->validate($rules);
+ }
+
+ public function testValidationUsesStaticRules()
+ {
+ Validator::clearResolvedInstances();
+
+ $model = new ValidatingModel;
+
+ $rules = array('hello' => uniqid());
+
+ Input::shouldReceive('hasSessionStore');
+
+ Validator::shouldReceive('make')->once()
+ ->with(m::any(), ValidatingModel::$rules, m::any())
+ ->andReturn($this->validator);
+
+ $model->validate();
+ }
+
+ public function testErrorsAreAlwaysAvailable()
+ {
+ $model = new ValidatingModel;
+
+ $this->assertInstanceOf('Illuminate\Support\MessageBag', $model->errors());
+ }
+
+ public function testValidationProvidesErrors()
+ {
+ $model = new ValidatingModel;
+ $messages = new MessageBag;
+
+
+ $this->validator->shouldReceive('messages')
+ ->andReturn($messages);
+
+ $model->validate();
+
+ $this->assertSame($messages, $model->errors());
+ $this->assertSame($messages, $model->validationErrors);
+ }
+
+ public function testValidationOverridesOldErrors()
+ {
+ $model = new ValidatingModel;
+ $messages = new MessageBag;
+ $model->validationErrors = $messages;
+
+ $messages->add('hello', 'world');
+
+ $this->validator->shouldReceive('passes')->once()
+ ->andReturn(true);
+
+ $model->validate();
+
+ $this->assertInstanceOf('Illuminate\Support\MessageBag', $model->errors());
+ $this->assertNotSame($messages, $model->errors());
+ $this->assertCount(0, $model->errors());
+ }
+
+ public function testValidationFailureFlashesInputData()
+ {
+ // Reset Input mock
+ Input::clearResolvedInstances();
+
+ $model = new ValidatingModel;
+
+ $this->validator->shouldReceive('passes')
+ ->andReturn(false);
+
+ Validator::shouldReceive('make')
+ ->andReturn($this->validator);
+
+ Input::shouldReceive('hasSessionStore')
+ ->andReturn(true);
+
+ Input::shouldReceive('flash')->once();
+
+ $model->validate();
+ }
+}
+
+class ValidatingModel extends Ardent
+{
+ public static $rules = array(
+ 'name' => array('required'),
+ 'email' => array('email')
+ );
+
+ public $saveCalled = 0;
+
+ protected function performSave(array $options) {
+ $this->saveCalled++;
+ }
+}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
new file mode 100644
index 0000000..4c6b442
--- /dev/null
+++ b/tests/bootstrap.php
@@ -0,0 +1,6 @@
+add('LaravelBook\Ardent\Test', __DIR__);