Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

HTML Lorem #971

Merged
merged 26 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from 21 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
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Faker requires PHP >= 5.3.3.
- [Barcode](#fakerproviderbarcode)
- [Miscellaneous](#fakerprovidermiscellaneous)
- [Biased](#fakerproviderbiased)
- [Html Lorem](#fakerproviderhtmllorem)
- [Modifiers](#modifiers)
- [Localization](#localization)
- [Populating Entities Using an ORM or an ODM](#populating-entities-using-an-orm-or-an-odm)
Expand Down Expand Up @@ -298,6 +299,11 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
// with more chances to be close to 20
biasedNumberBetween($min = 10, $max = 20, $function = 'sqrt')

### `Faker\Provider\HtmlLorem`

//Generate HTML of a certain max-depth and max-width.
randomHtml(2,3) // <html><head><title>Aut illo dolorem et accusantium eum.</title></head><body><form action="example.com" method="POST"><label for="username">sequi</label><input type="text" id="username"><label for="password">et</label><input type="password" id="password"></form><b>Id aut saepe non mollitia voluptas voluptas.</b><table><thead><tr><tr>Non consequatur.</tr><tr>Incidunt est.</tr><tr>Aut voluptatem.</tr><tr>Officia voluptas rerum quo.</tr><tr>Asperiores similique.</tr></tr></thead><tbody><tr><td>Sapiente dolorum dolorem sint laboriosam commodi qui.</td><td>Commodi nihil nesciunt eveniet quo repudiandae.</td><td>Voluptates explicabo numquam distinctio necessitatibus repellat.</td><td>Provident ut doloremque nam eum modi aspernatur.</td><td>Iusto inventore.</td></tr><tr><td>Animi nihil ratione id mollitia libero ipsa quia tempore.</td><td>Velit est officia et aut tenetur dolorem sed mollitia expedita.</td><td>Modi modi repudiandae pariatur voluptas rerum ea incidunt non molestiae eligendi eos deleniti.</td><td>Exercitationem voluptatibus dolor est iste quod molestiae.</td><td>Quia reiciendis.</td></tr><tr><td>Inventore impedit exercitationem voluptatibus rerum cupiditate.</td><td>Qui.</td><td>Aliquam.</td><td>Autem nihil aut et.</td><td>Dolor ut quia error.</td></tr><tr><td>Enim facilis iusto earum et minus rerum assumenda quis quia.</td><td>Reprehenderit ut sapiente occaecati voluptatum dolor voluptatem vitae qui velit.</td><td>Quod fugiat non.</td><td>Sunt nobis totam mollitia sed nesciunt est deleniti cumque.</td><td>Repudiandae quo.</td></tr><tr><td>Modi dicta libero quisquam doloremque qui autem.</td><td>Voluptatem aliquid saepe laudantium facere eos sunt dolor.</td><td>Est eos quis laboriosam officia expedita repellendus quia natus.</td><td>Et neque delectus quod fugit enim repudiandae qui.</td><td>Fugit soluta sit facilis facere repellat culpa magni voluptatem maiores tempora.</td></tr><tr><td>Enim dolores doloremque.</td><td>Assumenda voluptatem eum perferendis exercitationem.</td><td>Quasi in fugit deserunt ea perferendis sunt nemo consequatur dolorum soluta.</td><td>Maxime repellat qui numquam voluptatem est modi.</td><td>Alias rerum rerum hic hic eveniet.</td></tr><tr><td>Tempore voluptatem.</td><td>Eaque.</td><td>Et sit quas fugit iusto.</td><td>Nemo nihil rerum dignissimos et esse.</td><td>Repudiandae ipsum numquam.</td></tr><tr><td>Nemo sunt quia.</td><td>Sint tempore est neque ducimus harum sed.</td><td>Dicta placeat atque libero nihil.</td><td>Et qui aperiam temporibus facilis eum.</td><td>Ut dolores qui enim et maiores nesciunt.</td></tr><tr><td>Dolorum totam sint debitis saepe laborum.</td><td>Quidem corrupti ea.</td><td>Cum voluptas quod.</td><td>Possimus consequatur quasi dolorem ut et.</td><td>Et velit non hic labore repudiandae quis.</td></tr></tbody></table></body></html>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I don't understand what "depth" and "width" mean...

Copy link
Contributor Author

@rudkjobing rudkjobing Sep 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just used them to limit the size of the document. Depth is how deep the tree can get in total. Width is how wide each layer can get.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should explain it in the README

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have attempted to make it a bit more clear.


## Modifiers

Faker provides three special providers, `unique()`, `optional()`, and `valid()`, to be called before any provider.
Expand Down
261 changes: 261 additions & 0 deletions src/Faker/Provider/HtmlLorem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
<?php

namespace Faker\Provider;

class HtmlLorem extends Base
{

const HTML_TAG = "html";
const HEAD_TAG = "head";
const BODY_TAG = "body";
const DIV_TAG = "div";
const P_TAG = "p";
const A_TAG = "a";
const SPAN_TAG = "span";
const TABLE_TAG = "table";
const THEAD_TAG = "thead";
const TBODY_TAG = "tbody";
const TR_TAG = "tr";
const TD_TAG = "td";
const TH_TAG = "th";
const UL_TAG = "ul";
const LI_TAG = "li";
const H_TAG = "h";
const B_TAG = "b";
const I_TAG = "i";
const TITLE_TAG = "title";
const FORM_TAG = "form";
const INPUT_TAG = "input";
const LABEL_TAG = "label";

/**
* @return string
*/
public static function randomHtml($maxDepth = 4, $maxWidth = 4)
{
$document = new \DOMDocument();

$head = $document->createElement("head");
static::addRandomTitle($head);

$body = $document->createElement("body");
static::addLoginForm($body);
static::addRandomSubTree($body, $maxDepth, $maxWidth);

$html = $document->createElement("html");
$html->appendChild($head);
$html->appendChild($body);

$document->appendChild($html);
return $document->saveHTML();
}

private static function addRandomSubTree(\DOMElement $root, $maxDepth, $maxWidth)
{
$maxDepth--;
if ($maxDepth <= 0) {
return $root;
}

$siblings = mt_rand(1, $maxWidth);
for ($i = 0; $i < $siblings; $i++) {
if ($maxDepth == 1) {
static::addRandomLeaf($root);
} else {
$sibling = $root->ownerDocument->createElement("div");
$root->appendChild($sibling);
static::addRandomAttribute($sibling);
static::addRandomSubTree($sibling, mt_rand(0, $maxDepth), $maxWidth);
}
};
return $root;
}

private static function addRandomLeaf(\DOMElement $node)
{
$rand = mt_rand(1, 10);
switch($rand){
case 1:
static::addRandomP($node);
break;
case 2:
static::addRandomA($node);
break;
case 3:
static::addRandomSpan($node);
break;
case 4:
static::addRandomUL($node);
break;
case 5:
static::addRandomH($node);
break;
case 6:
static::addRandomB($node);
break;
case 7:
static::addRandomI($node);
break;
case 8:
static::addRandomTable($node);
break;
default:
static::addRandomText($node);
break;
}
}

private static function addRandomAttribute(\DOMElement $node)
{
$rand = mt_rand(1, 2);
switch ($rand) {
case 1:
$node->setAttribute("class", Lorem::word());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't use Lorem statically, but use $this->generator->word instead.

Copy link
Contributor Author

@rudkjobing rudkjobing Sep 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would i use $this in a static method? The mix of static / non-static is slightly confusing.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it non-static

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

break;
case 2:
$node->setAttribute("id", (string)Base::randomNumber(5));
break;
}
}

private static function addRandomP(\DOMElement $element, $maxLength = 10)
{

$node = $element->ownerDocument->createElement(HtmlLorem::P_TAG);
$node->textContent = Lorem::sentence(mt_rand(1, $maxLength));
$element->appendChild($node);
}

private static function addRandomText(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode(Lorem::sentence(mt_rand(1, $maxLength)));
$element->appendChild($text);
}

private static function addRandomA(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode(Lorem::sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(HtmlLorem::A_TAG);
$node->setAttribute("href", Internet::safeEmailDomain());
$node->appendChild($text);
$element->appendChild($node);
}

private static function addRandomTitle(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode(Lorem::sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(HtmlLorem::TITLE_TAG);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use static there instead and for all constants

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you ever want to override them?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't, but some developers do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

$node->appendChild($text);
$element->appendChild($node);
}

private static function addRandomH(\DOMElement $element, $maxLength = 10)
{
$h = HtmlLorem::H_TAG . (string)mt_rand(1, 3);
$text = $element->ownerDocument->createTextNode(Lorem::sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement($h);
$node->appendChild($text);
$element->appendChild($node);

}

private static function addRandomB(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode(Lorem::sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(HtmlLorem::B_TAG);
$node->appendChild($text);
$element->appendChild($node);
}

private static function addRandomI(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode(Lorem::sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(HtmlLorem::I_TAG);
$node->appendChild($text);
$element->appendChild($node);
}

private static function addRandomSpan(\DOMElement $element, $maxLength = 10)
{
$text = $element->ownerDocument->createTextNode(Lorem::sentence(mt_rand(1, $maxLength)));
$node = $element->ownerDocument->createElement(HtmlLorem::SPAN_TAG);
$node->appendChild($text);
$element->appendChild($node);
}

private static function addLoginForm(\DOMElement $element)
{

$textInput = $element->ownerDocument->createElement(HtmlLorem::INPUT_TAG);
$textInput->setAttribute("type", "text");
$textInput->setAttribute("id", "username");

$textLabel = $element->ownerDocument->createElement(HtmlLorem::LABEL_TAG);
$textLabel->setAttribute("for", "username");
$textLabel->textContent = Lorem::word();

$passwordInput = $element->ownerDocument->createElement(HtmlLorem::INPUT_TAG);
$passwordInput->setAttribute("type", "password");
$passwordInput->setAttribute("id", "password");

$passwordLabel = $element->ownerDocument->createElement(HtmlLorem::LABEL_TAG);
$passwordLabel->setAttribute("for", "password");
$passwordLabel->textContent = Lorem::word();

$submit = $element->ownerDocument->createElement(HtmlLorem::INPUT_TAG);
$submit->setAttribute("type", "submit");
$submit->setAttribute("value", Lorem::word());

$submit = $element->ownerDocument->createElement(HtmlLorem::FORM_TAG);
$submit->setAttribute("action", Internet::safeEmailDomain());
$submit->SetAttribute("method", "POST");
$submit->appendChild($textLabel);
$submit->appendChild($textInput);
$submit->appendChild($passwordLabel);
$submit->appendChild($passwordInput);
$element->appendChild($submit);
}

private static function addRandomTable(\DOMElement $element, $maxRows = 10, $maxCols = 6, $maxTitle = 4, $maxLength = 10)
{
$rows = mt_rand(1, $maxRows);
$cols = mt_rand(1, $maxCols);

$table = $element->ownerDocument->createElement(HtmlLorem::TABLE_TAG);
$thead = $element->ownerDocument->createElement(HtmlLorem::THEAD_TAG);
$tbody = $element->ownerDocument->createElement(HtmlLorem::TBODY_TAG);

$table->appendChild($thead);
$table->appendChild($tbody);

$tr = $element->ownerDocument->createElement(HtmlLorem::TR_TAG);
$thead->appendChild($tr);
for ($i = 0; $i < $cols; $i++) {
$th = $element->ownerDocument->createElement(HtmlLorem::TR_TAG);
$th->textContent = Lorem::sentence(mt_rand(1, $maxTitle));
$tr->appendChild($th);
}
for ($i = 0; $i < $rows; $i++) {
$tr = $element->ownerDocument->createElement("tr");
$tbody->appendChild($tr);
for ($j = 0; $j < $cols; $j++) {
$th = $element->ownerDocument->createElement("td");
$th->textContent = Lorem::sentence(mt_rand(1, $maxLength));
$tr->appendChild($th);
}
}
$element->appendChild($table);
}

private static function addRandomUL(\DOMElement $element, $maxItems = 11, $maxLength = 4)
{
$num = mt_rand(1, $maxItems);
$ul = $element->ownerDocument->createElement(HtmlLorem::UL_TAG);
for ($i = 0; $i < $num; $i++) {
$li = $element->ownerDocument->createElement(HtmlLorem::LI_TAG);
$li->textContent = Lorem::sentence(mt_rand(1, $maxLength));
$ul->appendChild($li);
}
$element->appendChild($ul);
}
}
20 changes: 20 additions & 0 deletions test/Faker/Provider/HtmlLoremTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Faker\Test\Provider;

use Faker\Generator;
use Faker\Provider\HtmlLorem;

class HtmlLoremTest extends \PHPUnit_Framework_TestCase
{

public function testProvider()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testRandomHtmlReturnsValidHTMLString(), and use DOM validation to prove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
$faker = new Generator();
$faker->addProvider(new HtmlLorem($faker));
$node = $faker->randomHtml(6, 10);
$this->assertStringStartsWith("<html>", $node);
$this->assertStringEndsWith("</html>\n", $node);
}

}