Skip to content

Commit

Permalink
Form unit test and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtfkx committed Aug 30, 2017
1 parent 8450cd6 commit b4f8f5d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 9 deletions.
30 changes: 22 additions & 8 deletions src/Elements/Form.php
Expand Up @@ -36,21 +36,21 @@ class Form extends AbstractElement
protected $enctype;

/**
* @param string $value
* @param string|null $value
* @return Form
*/
public function action(string $value): Form
public function action(?string $value = ''): Form
{
$this->action = $value;

return $this;
}

/**
* @param string $value
* @param string|null $value
* @return Form
*/
public function method(string $value): Form
public function method(?string $value): Form
{
$this->method = $value;

Expand All @@ -70,6 +70,19 @@ protected function enctype(string $value): Form
return $this;
}

/**
* Сеттер для установки указания в каком окнеоткрывать результат работы
*
* @param string|null $value
* @return Form
*/
public function target(?string $value): Form
{
$this->target = $value;

return $this;
}

/**
* Установить тип отправки данных multipart/form-data
*
Expand Down Expand Up @@ -103,10 +116,10 @@ public function urlencoded(): Form
/**
* Сеттер для установки признака включения/отключения автозаполнения полей формы
*
* @param bool $state
* @param bool|null $state
* @return Form
*/
public function autocomplete(bool $state = true): Form
public function autocomplete(?bool $state = true): Form
{
$this->autocomplete = $state;

Expand All @@ -116,10 +129,10 @@ public function autocomplete(bool $state = true): Form
/**
* Сеттер для установки признака включения/отключения автовалидации перед отправкой данных
*
* @param bool $state
* @param bool|null $state
* @return Form
*/
public function novalidate(bool $state = true): Form
public function novalidate(?bool $state = true): Form
{
$this->novalidate = $state;

Expand All @@ -136,6 +149,7 @@ public function __toString()
$this->attrs([
'action' => $this->action,
'method' => $this->method,
'target' => $this->target,
'enctype' => $this->enctype,
'autocomplete' => $this->autocomplete,
'novalidate' => $this->novalidate,
Expand Down
2 changes: 1 addition & 1 deletion src/FormBuilder.php
Expand Up @@ -32,7 +32,7 @@ public function __construct()
* @param string $method Метод отправки данных формы
* @return Elements\Form
*/
public function open(string $action, string $method = 'get'): Elements\Form
public function open(?string $action = '', ?string $method = 'get'): Elements\Form
{
$this->form = (new Elements\Form())->action($action)->method($method);

Expand Down
142 changes: 142 additions & 0 deletions tests/Form/FormTest.php
@@ -0,0 +1,142 @@
<?php

namespace Ngtfkx\Laradeck\FormBuilder\Tests\Form;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class FormTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testFormOpen()
{
$form = '' . fb()->open();

$this->assertEquals('<form action="" method="get">', $form);
}

public function testFormClose()
{
$form = '' . fb()->close();

$this->assertEquals('</form>', $form);
}

public function testActionSet()
{
$form = '' . fb()->open('/');

$this->assertContains('action="/"', $form);
}

public function testActionNull()
{
$form = '' . fb()->open(null);

$this->assertNotContains('action', $form);
}

public function testMethodSet()
{
$form = '' . fb()->open(null, 'put');

$this->assertContains('method="put', $form);
}

public function testMethodNull()
{
$form = '' . fb()->open(null, null);

$this->assertNotContains('method', $form);
}

public function testAutocompleteOn()
{
$form = '' . fb()->open()->autocomplete(true);

$this->assertContains('autocomplete="autocomplete', $form);

$form = '' . fb()->open()->autocomplete();

$this->assertContains('autocomplete="autocomplete', $form);
}

public function testAutocompleteOff()
{
$form = '' . fb()->open()->autocomplete(false);

$this->assertNotContains('autocomplete', $form);
}

public function testAutocompleteNull()
{
$form = '' . fb()->open()->autocomplete(null);

$this->assertNotContains('autocomplete', $form);
}

public function testNovalidateOn()
{
$form = '' . fb()->open()->novalidate(true);

$this->assertContains('novalidate="novalidate', $form);

$form = '' . fb()->open()->novalidate();

$this->assertContains('novalidate="novalidate', $form);
}

public function testNovalidateOff()
{
$form = '' . fb()->open()->novalidate(false);

$this->assertNotContains('novalidate', $form);
}

public function testNovalidateNull()
{
$form = '' . fb()->open()->novalidate(null);

$this->assertNotContains('novalidate', $form);
}

public function testTargetSet()
{
$form = '' . fb()->open()->target('_self');

$this->assertContains('target="_self', $form);
}

public function testTargetNull()
{
$form = '' . fb()->open()->target(null);

$this->assertNotContains('target', $form);
}

public function testUrlencoded()
{
$form = '' . fb()->open()->urlencoded();

$this->assertContains('enctype="application/x-www-form-urlencoded"', $form);
}

public function testPlain()
{
$form = '' . fb()->open()->plain();

$this->assertContains('enctype="text/plain"', $form);
}

public function testMultipart()
{
$form = '' . fb()->open()->multipart();

$this->assertContains('enctype="multipart/form-data"', $form);
}
}

0 comments on commit b4f8f5d

Please sign in to comment.