Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Automated Test Suite #151

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
18 changes: 16 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
@@ -15,4 +15,18 @@
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

<logging>
<log type="coverage-html" target="build/coverage" title="Ardent" highlight="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/LaravelBook/Ardent</directory>
<exclude>
<directory>vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Empty file removed tests/.gitkeep
Empty file.
30 changes: 30 additions & 0 deletions tests/LaravelBook/Ardent/Test/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php namespace LaravelBook\Ardent\Test;

use PHPUnit_Framework_TestCase;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Mockery as m;

class TestCase extends PHPUnit_Framework_TestCase
{
public function setup()
{
$this->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();
}
}
167 changes: 167 additions & 0 deletions tests/LaravelBook/Ardent/Test/ValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php namespace LaravelBook\Ardent\Test;

use LaravelBook\Ardent\Ardent;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Mockery as m;

class ValidationTest extends TestCase
{
public function testValidateCalledOnSave()
{
$model = m::mock('LaravelBook\Ardent\Test\ValidatingModel[validate]');

$model->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++;
}
}
6 changes: 6 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

error_reporting(E_ALL);

$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add('LaravelBook\Ardent\Test', __DIR__);