From aae950cb7f5f19e91f943573068fff133ee56a17 Mon Sep 17 00:00:00 2001 From: Clark Fischer Date: Sat, 11 Jan 2014 11:53:43 -0800 Subject: [PATCH 1/5] Added PHPUnit and set up test suite PHPUnit is added as a development dependency, and is configured to track test coverage for all files within src/LaravelBook/Ardent. The test coverage is generated in `build/coverage/`, which was added to gitignore. --- .gitignore | 1 + composer.json | 3 +++ phpunit.xml | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) 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..0b946da 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,9 @@ "illuminate/database": "~4.1", "illuminate/validation": "~4.1" }, + "require-dev": { + "phpunit/phpunit": "~3.7" + }, "autoload": { "psr-0": { "LaravelBook\\Ardent": "src/" diff --git a/phpunit.xml b/phpunit.xml index e601408..40f8c2a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -15,4 +15,18 @@ ./tests/ - \ No newline at end of file + + + + + + + + + src/LaravelBook/Ardent + + vendor + + + + From 850bda92e565d1d94eacca9ddbb31780e9840550 Mon Sep 17 00:00:00 2001 From: Clark Fischer Date: Sat, 11 Jan 2014 12:00:17 -0800 Subject: [PATCH 2/5] Added Test Suite Boilerplate Added a 'bootstrap' file, which loads the autoloader, and adds the `LaravelBook\Ardent\Test` namespace to the environment. Added a base `TestCase` class to be extended project-wide. --- phpunit.xml | 2 +- tests/LaravelBook/Ardent/Test/TestCase.php | 7 +++++++ tests/bootstrap.php | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/LaravelBook/Ardent/Test/TestCase.php create mode 100644 tests/bootstrap.php diff --git a/phpunit.xml b/phpunit.xml index 40f8c2a..96d69d5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ add('LaravelBook\Ardent\Test', __DIR__); From 45b468e0a00ca9a6d5e1af482fcfa4e2c11ca5e3 Mon Sep 17 00:00:00 2001 From: Clark Fischer Date: Sat, 11 Jan 2014 12:05:00 -0800 Subject: [PATCH 3/5] Removed vestigial gitkeep --- tests/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/.gitkeep diff --git a/tests/.gitkeep b/tests/.gitkeep deleted file mode 100644 index e69de29..0000000 From bcccd8432a8678c8301a508df33548cee1008510 Mon Sep 17 00:00:00 2001 From: Clark Fischer Date: Sat, 11 Jan 2014 12:33:56 -0800 Subject: [PATCH 4/5] Changed Minimum Stability to Stable We don't want to/shouldn't be testing against development branches or broken commits. --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 0b946da..155dd71 100644 --- a/composer.json +++ b/composer.json @@ -30,12 +30,13 @@ "illuminate/validation": "~4.1" }, "require-dev": { - "phpunit/phpunit": "~3.7" + "phpunit/phpunit": "~3.7", + "mockery/mockery": "~0.8" }, "autoload": { "psr-0": { "LaravelBook\\Ardent": "src/" } }, - "minimum-stability": "dev" + "minimum-stability": "stable" } From d05d20fcb53efe02551aee48be9c8a5df0426a3b Mon Sep 17 00:00:00 2001 From: Clark Fischer Date: Sat, 11 Jan 2014 14:08:48 -0800 Subject: [PATCH 5/5] Added initial draft of Ardent Test Cases --- tests/LaravelBook/Ardent/Test/TestCase.php | 23 +++ .../Ardent/Test/ValidationTest.php | 167 ++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 tests/LaravelBook/Ardent/Test/ValidationTest.php diff --git a/tests/LaravelBook/Ardent/Test/TestCase.php b/tests/LaravelBook/Ardent/Test/TestCase.php index 5c0bae4..2a613a2 100644 --- a/tests/LaravelBook/Ardent/Test/TestCase.php +++ b/tests/LaravelBook/Ardent/Test/TestCase.php @@ -1,7 +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++; + } +}