Add tests
This commit is contained in:
parent
567999909f
commit
602fde7227
@ -271,7 +271,7 @@ class AdminController extends Controller
|
||||
|
||||
$announ = Option::form('announ', OptionForm::AUTO_DETECT, function ($form) {
|
||||
$form->textarea('announcement')->rows(10)->description();
|
||||
})->renderWithOutTable()->handle(function () {
|
||||
})->renderWithoutTable()->handle(function () {
|
||||
Option::set('announcement_'.config('app.locale'), request('announcement'));
|
||||
});
|
||||
|
||||
|
@ -32,7 +32,7 @@ class OptionForm
|
||||
protected $hookAfter;
|
||||
protected $alwaysCallback = null;
|
||||
|
||||
protected $renderWithOutTable = false;
|
||||
protected $renderWithoutTable = false;
|
||||
protected $renderInputTagsOnly = false;
|
||||
protected $renderWithoutSubmitButton = false;
|
||||
|
||||
@ -43,12 +43,12 @@ class OptionForm
|
||||
* @param string $title
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($id, $title)
|
||||
public function __construct($id, $title = self::AUTO_DETECT)
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
if ($title == self::AUTO_DETECT) {
|
||||
$this->title = trans("options.$this->id.title");
|
||||
$this->title = trans("options.$id.title");
|
||||
} else {
|
||||
$this->title = $title;
|
||||
}
|
||||
@ -216,26 +216,6 @@ class OptionForm
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse id formatted as *[*]. Return id & offset when succeed.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool|array
|
||||
*/
|
||||
protected function parseIdWithOffset($id)
|
||||
{
|
||||
preg_match('/(.*)\[(.*)\]/', $id, $matches);
|
||||
|
||||
if (isset($matches[2])) {
|
||||
return [
|
||||
'id' => $matches[1],
|
||||
'offset' => $matches[2],
|
||||
];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the HTTP post request and update modified options.
|
||||
*
|
||||
@ -257,7 +237,6 @@ class OptionForm
|
||||
}
|
||||
|
||||
$postOptionQueue = [];
|
||||
$arrayOptionQueue = [];
|
||||
|
||||
foreach ($this->items as $item) {
|
||||
if ($item instanceof OptionFormGroup) {
|
||||
@ -278,15 +257,6 @@ class OptionForm
|
||||
$allPostData[$item->id] = false;
|
||||
}
|
||||
|
||||
// Str::is('*[*]', $item->id)
|
||||
if (false !== ($result = $this->parseIdWithOffset($item->id))) {
|
||||
// Push array option value to cache.
|
||||
// Values of post ids like *[*] is collected as arrays in $allPostData
|
||||
// automatically by Laravel.
|
||||
$arrayOptionQueue[$result['id']] = $allPostData[$result['id']];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Compare with raw option value
|
||||
if (($data = Arr::get($allPostData, $item->id)) != option($item->id, null, true)) {
|
||||
$formatted = is_null($item->format) ? $data : call_user_func($item->format, $data);
|
||||
@ -294,10 +264,6 @@ class OptionForm
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($arrayOptionQueue as $key => $value) {
|
||||
Option::set($key, $value);
|
||||
}
|
||||
|
||||
if (! is_null($this->hookAfter)) {
|
||||
call_user_func($this->hookAfter, $this);
|
||||
}
|
||||
@ -316,18 +282,7 @@ class OptionForm
|
||||
*/
|
||||
protected function getValueById($id)
|
||||
{
|
||||
if (false === ($result = $this->parseIdWithOffset($id))) {
|
||||
return Arr::get($this->values, $id, option_localized($id));
|
||||
} else {
|
||||
$option = Arr::get(
|
||||
$this->values,
|
||||
$result['id'],
|
||||
// Fallback to load from options
|
||||
@unserialize(option_localized($result['id']))
|
||||
);
|
||||
|
||||
return Arr::get($option, $result['offset']);
|
||||
}
|
||||
return Arr::get($this->values, $id, option_localized($id));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -354,9 +309,9 @@ class OptionForm
|
||||
}
|
||||
}
|
||||
|
||||
public function renderWithOutTable()
|
||||
public function renderWithoutTable()
|
||||
{
|
||||
$this->renderWithOutTable = true;
|
||||
$this->renderWithoutTable = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
{!! $msg !!}
|
||||
@endforeach
|
||||
|
||||
@if ($renderWithOutTable)
|
||||
@if ($renderWithoutTable)
|
||||
@each('common.option-form.item', $items, 'item')
|
||||
@else
|
||||
<table class="table">
|
||||
|
@ -8,13 +8,114 @@ use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
class OptionFormTest extends TestCase
|
||||
{
|
||||
public function testRenderTitle()
|
||||
{
|
||||
$form = new OptionForm('test');
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals(trans('options.test.title'), trim($crawler->filter('.box-title')->text()));
|
||||
|
||||
$form = new OptionForm('test', 'test');
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals('test', trim($crawler->filter('.box-title')->text()));
|
||||
}
|
||||
|
||||
public function testDisallowInvalidType()
|
||||
{
|
||||
$this->expectException(\BadMethodCallException::class);
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->nope();
|
||||
}
|
||||
|
||||
public function testRenderBoxType()
|
||||
{
|
||||
$form = new OptionForm('test');
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertCount(1, $crawler->filter('.box-primary'));
|
||||
|
||||
$form = new OptionForm('test');
|
||||
$returned = $form->type('warning');
|
||||
$this->assertSame($form, $returned);
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertCount(1, $crawler->filter('.box-warning'));
|
||||
}
|
||||
|
||||
public function testRenderHint()
|
||||
{
|
||||
$form = new OptionForm('test');
|
||||
$returned = $form->hint();
|
||||
$this->assertSame($form, $returned);
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals(trans('options.test.hint'), $crawler->filter('.fa-question-circle')->attr('title'));
|
||||
|
||||
$form = new OptionForm('test');
|
||||
$form->hint('this is hint');
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals('this is hint', $crawler->filter('.fa-question-circle')->attr('title'));
|
||||
}
|
||||
|
||||
public function testPassValues()
|
||||
{
|
||||
$form = new OptionForm('test');
|
||||
$form->text('k');
|
||||
$returned = $form->with('k', 'v');
|
||||
$this->assertSame($form, $returned);
|
||||
$form->handle();
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals('v', $crawler->filter('[name=k]')->attr('value'));
|
||||
|
||||
$form = new OptionForm('test');
|
||||
$form->text('k1');
|
||||
$form->text('k2');
|
||||
$form->with(['k1' => 'v1', 'k2' => 'v2']);
|
||||
$form->handle();
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals('v1', $crawler->filter('[name=k1]')->attr('value'));
|
||||
$this->assertEquals('v2', $crawler->filter('[name=k2]')->attr('value'));
|
||||
}
|
||||
|
||||
public function testAddButton()
|
||||
{
|
||||
$form = new OptionForm('test');
|
||||
$returned = $form->addButton(['href' => 'http://example.com', 'class' => ['a', 'b'], 'text' => 'link']);
|
||||
$form->addButton(['style' => 'primary', 'text' => 'press me', 'name' => 'btn']);
|
||||
$form->addButton(['style' => 'warning', 'type' => 'submit']);
|
||||
$this->assertSame($form, $returned);
|
||||
|
||||
$crawler = new Crawler($form->render());
|
||||
$a = $crawler->filter('a');
|
||||
$this->assertEquals('http://example.com', $a->attr('href'));
|
||||
$this->assertEquals('el-button el-button--default a b', $a->attr('class'));
|
||||
$this->assertEquals('link', $a->text());
|
||||
|
||||
$button = $crawler->filter('button.el-button--primary');
|
||||
$this->assertEquals('press me', $button->text());
|
||||
$this->assertEquals('btn', $button->attr('name'));
|
||||
$this->assertEquals('button', $button->attr('type'));
|
||||
|
||||
$button = $crawler->filter('button.el-button--warning');
|
||||
$this->assertEquals('submit', $button->attr('type'));
|
||||
}
|
||||
|
||||
public function testAddMessage()
|
||||
{
|
||||
$form = new OptionForm('test', 'test');
|
||||
$returned = $form->addMessage();
|
||||
$form->addMessage('greeting', 'warning');
|
||||
$this->assertSame($form, $returned);
|
||||
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals(trans('options.test.message'), $crawler->filter('.callout-info')->text());
|
||||
$this->assertEquals('greeting', $crawler->filter('.callout-warning')->text());
|
||||
}
|
||||
|
||||
public function testHookBefore()
|
||||
{
|
||||
$called = false;
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->before(function () use (&$called) {
|
||||
$returned = $form->before(function () use (&$called) {
|
||||
$called = true;
|
||||
});
|
||||
$this->assertSame($form, $returned);
|
||||
|
||||
$request = request();
|
||||
$request->setMethod('POST');
|
||||
@ -28,9 +129,10 @@ class OptionFormTest extends TestCase
|
||||
{
|
||||
$called = false;
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->after(function () use (&$called) {
|
||||
$returned = $form->after(function () use (&$called) {
|
||||
$called = true;
|
||||
});
|
||||
$this->assertSame($form, $returned);
|
||||
|
||||
$request = request();
|
||||
$request->setMethod('POST');
|
||||
@ -40,6 +142,26 @@ class OptionFormTest extends TestCase
|
||||
$this->assertTrue($called);
|
||||
}
|
||||
|
||||
public function testHookAlways()
|
||||
{
|
||||
$called = false;
|
||||
$form = new OptionForm('test', 'test');
|
||||
$returned = $form->always(function () use (&$called) {
|
||||
$called = true;
|
||||
});
|
||||
$this->assertSame($form, $returned);
|
||||
|
||||
$request = request();
|
||||
$request->setMethod('POST');
|
||||
$request->merge(['option' => 'test']);
|
||||
|
||||
$form->handle();
|
||||
$this->assertFalse($called);
|
||||
|
||||
$form->render();
|
||||
$this->assertTrue($called);
|
||||
}
|
||||
|
||||
public function testDirectHook()
|
||||
{
|
||||
$called = false;
|
||||
@ -55,30 +177,22 @@ class OptionFormTest extends TestCase
|
||||
$this->assertTrue($called);
|
||||
}
|
||||
|
||||
public function testHookAlways()
|
||||
public function testRenderWithoutTable()
|
||||
{
|
||||
$called = false;
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->always(function () use (&$called) {
|
||||
$called = true;
|
||||
});
|
||||
|
||||
$request = request();
|
||||
$request->setMethod('POST');
|
||||
$request->merge(['option' => 'test']);
|
||||
|
||||
$form->handle();
|
||||
$this->assertFalse($called);
|
||||
|
||||
$form->render();
|
||||
$this->assertTrue($called);
|
||||
$form->text('text');
|
||||
$returned = $form->renderWithoutTable();
|
||||
$this->assertSame($form, $returned);
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertCount(0, $crawler->filter('table'));
|
||||
}
|
||||
|
||||
public function testRenderInputTagsOnly()
|
||||
{
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->text('text');
|
||||
$form->renderInputTagsOnly();
|
||||
$returned = $form->renderInputTagsOnly();
|
||||
$this->assertSame($form, $returned);
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertCount(0, $crawler->filter('td.key'));
|
||||
$this->assertCount(1, $crawler->filter('td.value'));
|
||||
@ -88,26 +202,16 @@ class OptionFormTest extends TestCase
|
||||
{
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->text('text');
|
||||
$form->renderWithoutSubmitButton();
|
||||
$returned = $form->renderWithoutSubmitButton();
|
||||
$this->assertSame($form, $returned);
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertCount(0, $crawler->filter('button'));
|
||||
}
|
||||
|
||||
public function testDisallowInvalidType()
|
||||
public function testToString()
|
||||
{
|
||||
$this->expectException(\BadMethodCallException::class);
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->nope();
|
||||
}
|
||||
|
||||
public function testAddMessage()
|
||||
{
|
||||
$form = new OptionForm('test', 'test');
|
||||
$form->addMessage();
|
||||
$form->addMessage('greeting', 'warning');
|
||||
|
||||
$crawler = new Crawler($form->render());
|
||||
$this->assertEquals(trans('options.test.message'), $crawler->filter('.callout-info')->text());
|
||||
$this->assertEquals('greeting', $crawler->filter('.callout-warning')->text());
|
||||
$form = new OptionForm('test');
|
||||
$crawler = new Crawler(sprintf('%s', $form));
|
||||
$this->assertCount(1, $crawler->filter('div.box'));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user