Skip to content

Commit

Permalink
Add orchestra/testbench.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed Aug 12, 2017
1 parent 15e551b commit 27fbd1a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 88 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
],
"autoload": {
"psr-4": {
"Collective\\Html\\": "src/"
"Collective\\Html\\": "src/"
},
"files": [
"src/helpers.php"
"src/helpers.php"
]
},
"require": {
Expand All @@ -38,9 +38,9 @@
"illuminate/support": "~5.4.0"
},
"require-dev": {
"illuminate/database": "~5.4.0",
"illuminate/view": "~5.4.0",
"mockery/mockery": "^0.9.4",
"orchestra/database": "~3.4.0",
"orchestra/testbench": "~3.4.8",
"phpunit/phpunit": "~4.8.35 || ~5.7"
},
"replace": {
Expand Down
51 changes: 0 additions & 51 deletions phpunit.php

This file was deleted.

8 changes: 7 additions & 1 deletion 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="phpunit.php"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand All @@ -22,4 +22,10 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
</php>

</phpunit>
71 changes: 39 additions & 32 deletions tests/FormAccessibleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

use Mockery as m;
use Carbon\Carbon;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;
use Collective\Html\FormBuilder;
use Collective\Html\HtmlBuilder;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Contracts\View\Factory;
use Orchestra\Testbench\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\RouteCollection;
use Collective\Html\Eloquent\FormAccessible;
use Illuminate\Database\Capsule\Manager as Capsule;

class FormAccessibleTest extends TestCase
{
public function setUp()
protected function setUp()
{
Capsule::table('models')->truncate();
parent::setUp();

Model::unguard();

$this->loadMigrationsFrom(__DIR__.'/migrations/');

$this->now = Carbon::now();

$this->modelData = [
Expand All @@ -33,17 +29,28 @@ public function setUp()
'created_at' => $this->now,
'updated_at' => $this->now,
];
}

$this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
$this->viewFactory = m::mock(Factory::class);
$this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory);
$this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, $this->viewFactory, 'abc');
protected function getPackageProviders($app)
{
return [
\Orchestra\Database\ConsoleServiceProvider::class,
\Collective\Html\HtmlServiceProvider::class,
];
}

protected function getPackageAliases($app)
{
return [
'Form' => \Collective\Html\FormFacade::class,
'Html' => \Collective\Html\HtmlFacade::class,
];
}

public function testItCanMutateValuesForForms()
{
$model = new ModelThatUsesForms($this->modelData);
$this->formBuilder->setModel($model);
Form::setModel($model);

$this->assertEquals($model->getFormValue('string'), 'ponmlkjihgfedcba');
$this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp);
Expand All @@ -58,47 +65,47 @@ public function testItCanMutateRelatedValuesForForms()
];
$model->setRelation('related', $relatedModel);

$this->formBuilder->setModel($model);
Form::setModel($model);

$this->assertEquals($this->formBuilder->getValueAttribute('related[string]'), 'ponmlkjihgfedcba');
$this->assertEquals($this->formBuilder->getValueAttribute('related[address][street]'), '123 Evergreen Terrace');
$this->assertEquals(Form::getValueAttribute('related[string]'), 'ponmlkjihgfedcba');
$this->assertEquals(Form::getValueAttribute('related[address][street]'), '123 Evergreen Terrace');
}

public function testItCanGetRelatedValueForForms()
{
$model = new ModelThatUsesForms($this->modelData);
$this->assertEquals($model->getFormValue('address.street'), 'abcde st');
}

public function testItCanUseGetAccessorValuesWhenThereAreNoFormAccessors()
{
$model = new ModelThatUsesForms($this->modelData);
$this->formBuilder->setModel($model);
$this->assertEquals($this->formBuilder->getValueAttribute('email'), 'mutated@tjshafer.com');
Form::setModel($model);

$this->assertEquals(Form::getValueAttribute('email'), 'mutated@tjshafer.com');
}

public function testItReturnsSameResultWithAndWithoutThisFeature()
{
$modelWithAccessor = new ModelThatUsesForms($this->modelData);
$modelWithoutAccessor = new ModelThatDoesntUseForms($this->modelData);

$this->formBuilder->setModel($modelWithAccessor);
$valuesWithAccessor[] = $this->formBuilder->getValueAttribute('array');
$valuesWithAccessor[] = $this->formBuilder->getValueAttribute('array[0]');
$valuesWithAccessor[] = $this->formBuilder->getValueAttribute('transform.key');
$this->formBuilder->setModel($modelWithoutAccessor);
$valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('array');
$valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('array[0]');
$valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('transform.key');
Form::setModel($modelWithAccessor);
$valuesWithAccessor[] = Form::getValueAttribute('array');
$valuesWithAccessor[] = Form::getValueAttribute('array[0]');
$valuesWithAccessor[] = Form::getValueAttribute('transform.key');
Form::setModel($modelWithoutAccessor);
$valuesWithoutAccessor[] = Form::getValueAttribute('array');
$valuesWithoutAccessor[] = Form::getValueAttribute('array[0]');
$valuesWithoutAccessor[] = Form::getValueAttribute('transform.key');

$this->assertEquals($valuesWithAccessor, $valuesWithoutAccessor);
}

public function testItCanStillMutateValuesForViews()
{
$model = new ModelThatUsesForms($this->modelData);
$this->formBuilder->setModel($model);
Form::setModel($model);

$this->assertEquals($model->string, 'ABCDEFGHIJKLMNOP');
$this->assertEquals($model->created_at, '1 second ago');
Expand All @@ -107,7 +114,7 @@ public function testItCanStillMutateValuesForViews()
public function testItDoesntRequireTheUseOfThisFeature()
{
$model = new ModelThatDoesntUseForms($this->modelData);
$this->formBuilder->setModel($model);
Form::setModel($model);

$this->assertEquals($model->string, 'ABCDEFGHIJKLMNOP');
$this->assertEquals($model->created_at, '1 second ago');
Expand Down
30 changes: 30 additions & 0 deletions tests/migrations/2017_08_12_182750_create_html_models_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;

class CreateHtmlModelsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('models', function ($table) {
$table->increments('id');
$table->string('string');
$table->string('email');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('models');
}
}

0 comments on commit 27fbd1a

Please sign in to comment.