Skip to content

Commit

Permalink
Adding more tests
Browse files Browse the repository at this point in the history
+ cleaning
  • Loading branch information
arcanedev-maroc committed Jan 31, 2019
1 parent 4ff22fe commit ef70e7a
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 38 deletions.
8 changes: 8 additions & 0 deletions src/Elements/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ protected function applyValueToOptions()
);
}

/**
* Apply the selected value to the options.
*
* @param \Illuminate\Support\Collection $value
* @param \Illuminate\Support\Collection $children
*
* @return \Illuminate\Support\Collection
*/
protected static function applyValueToElements(Collection $value, Collection $children)
{
return $children->map(function (HtmlElement $child) use ($value) {
Expand Down
6 changes: 4 additions & 2 deletions src/Entities/ChildrenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function render()
*/
public function toHtml()
{
return $this->map(function ($child): string {
$mapper = function ($child): string {
if ($child instanceof Renderable)
return $child->render();

Expand All @@ -67,7 +67,9 @@ public function toHtml()
return $child;

throw new InvalidChildException;
})->implode('');
};

return $this->map($mapper)->implode('');
}

/* -----------------------------------------------------------------
Expand Down
20 changes: 4 additions & 16 deletions src/Html.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
<?php namespace Arcanedev\Html;

use Arcanedev\Html\Contracts\Html as HtmlContract;
use Arcanedev\Html\Elements\{A,
Button,
Div,
Element,
Fieldset,
File,
Form,
I,
Img,
Input,
Label,
Legend,
Option,
Select,
Span,
Textarea};
use Arcanedev\Html\Elements\{
A, Button, Div, Element, Fieldset, File, Form, I, Img, Input, Label,
Legend, Option, Select, Span, Textarea
};
use Arcanedev\Html\Entities\Attributes\ClassAttribute;

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Elements/HtmlElementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php namespace Arcanedev\Html\Tests\Elements;

use Arcanedev\Html\Elements\Element;

/**
* Class HtmlElementTest
*
* @package Arcanedev\Html\Tests\Elements
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
class HtmlElementTest extends TestCase
{
/* -----------------------------------------------------------------
| Tests
| -----------------------------------------------------------------
*/

/** @test */
public function it_can_register_a_macro()
{
Element::macro('btnPrimary', function () {
return $this->class('btn btn-primary');
});

$elt = Element::withTag('a');

static::assertEquals('<a></a>', $elt->toHtml());
static::assertEquals('<a class="btn btn-primary"></a>', $elt->btnPrimary()->toHtml());
}
}
104 changes: 104 additions & 0 deletions tests/Entities/ChildrenCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php namespace Arcanedev\Html\Tests\Entities;

use Arcanedev\Html\Elements\Span;
use Arcanedev\Html\Entities\ChildrenCollection;
use Arcanedev\Html\Tests\TestCase;

/**
* Class ChildrenCollectionTest
*
* @package Arcanedev\Html\Tests\Entities
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
*/
class ChildrenCollectionTest extends TestCase
{
/* -----------------------------------------------------------------
| Tests
| -----------------------------------------------------------------
*/

/** @test */
public function it_can_be_instantiated()
{
$children = new ChildrenCollection;

$expectations = [
\Arcanedev\Html\Contracts\Renderable::class,
\Illuminate\Support\Collection::class,
];

foreach ($expectations as $expected) {
static::assertInstanceOf($expected, $children);
}

static::assertCount(0, $children);
static::assertTrue($children->isEmpty());
}

/** @test */
public function it_can_parse_children()
{
$children = ChildrenCollection::parse(['foo', null, 'bar']);

static::assertCount(3, $children);
static::assertEquals(['foo', null, 'bar'], $children->all());
}

/** @test */
public function it_can_convert_to_html()
{
$children = ChildrenCollection::parse(['foo', null, 'bar'], function ($child) {
return ! is_null($child)
? Span::make()->html($child)
: $child;
});

static::assertCount(3, $children);
static::assertEquals('<span>foo</span><span>bar</span>', $children->toHtml());
}

/**
* @test
*
* @dataProvider getInvalidChildrenToParseDataProvider
*
* @param mixed $child
*
* @expectedException \Arcanedev\Html\Exceptions\InvalidChildException
*/
public function it_must_throw_exception_on_parse_with_invalid_child($child)
{
ChildrenCollection::parse($child);
}

/**
* @test
*
* @dataProvider getInvalidChildrenToParseDataProvider
*
* @param mixed $child
*
* @expectedException \Arcanedev\Html\Exceptions\InvalidChildException
*/
public function it_must_throw_exception_on_convert_to_html_with_invalid_child($child)
{
ChildrenCollection::make([$child])->toHtml();
}

/**
* >DATA PROVIDER
*
* @see it_must_throw_exception_on_parse_with_invalid_child
* @see it_must_throw_exception_on_convert_to_html_with_invalid_child
*
* @return array
*/
public function getInvalidChildrenToParseDataProvider()
{
return [
[true],
[1],
[.1],
];
}
}
46 changes: 46 additions & 0 deletions tests/Html/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ public function it_can_create_an_input_that_has_autofocus()
);
}

/** @test */
public function it_can_make_checkbox_input()
{
static::assertHtmlStringEqualsHtmlString(
'<input id="is_checked" name="is_checked" type="checkbox" value="1">',
$this->html->checkbox('is_checked')
);

static::assertHtmlStringEqualsHtmlString(
'<input checked="checked" id="is_checked" name="is_checked" type="checkbox" value="1">',
$this->html->checkbox('is_checked', true)
);

static::assertHtmlStringEqualsHtmlString(
'<input id="is_checked" name="is_checked" type="checkbox" value="yes">',
$this->html->checkbox('is_checked', false, 'yes')
);
}

/** @test */
public function it_can_check_an_input()
{
Expand Down Expand Up @@ -130,4 +149,31 @@ public function it_can_create_a_time_input()
$this->html->time()
);
}

/** @test */
public function it_can_create_a_hidden_input()
{
static::assertHtmlStringEqualsHtmlString(
'<input type="hidden" id="_token" name="_token" value="12345">',
$this->html->hidden('_token', '12345')
);
}

/** @test */
public function it_can_make_text_input()
{
static::assertHtmlStringEqualsHtmlString(
'<input type="text" id="title" name="title" value="Hello there">',
$this->html->text('title', 'Hello there')
);
}

/** @test */
public function it_can_make_number_input()
{
static::assertHtmlStringEqualsHtmlString(
'<input type="number" id="price" name="price" value="120">',
$this->html->number('price', 120)
);
}
}
20 changes: 0 additions & 20 deletions tests/HtmlTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace Arcanedev\Html\Tests;

use Arcanedev\Html\Html;
use Illuminate\Support\HtmlString;

/**
* Class HtmlTest
Expand Down Expand Up @@ -36,25 +35,6 @@ protected function setUp()
| -----------------------------------------------------------------
*/

/** @test */
public function it_can_make_checkbox()
{
static::assertHtmlStringEqualsHtmlString(
'<input id="is_checked" name="is_checked" type="checkbox" value="1">',
$this->html->checkbox('is_checked')
);

static::assertHtmlStringEqualsHtmlString(
'<input checked="checked" id="is_checked" name="is_checked" type="checkbox" value="1">',
$this->html->checkbox('is_checked', true)
);

static::assertHtmlStringEqualsHtmlString(
'<input id="is_checked" name="is_checked" type="checkbox" value="yes">',
$this->html->checkbox('is_checked', false, 'yes')
);
}

/** @test */
public function it_can_make_class_attribute()
{
Expand Down

0 comments on commit ef70e7a

Please sign in to comment.