Skip to content

Commit

Permalink
Add humbug and remove some issues detected by mutation tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
U-EMAG\michal.lipek authored and U-EMAG\michal.lipek committed Dec 16, 2015
1 parent 1b3ed50 commit 7958b7a
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 36 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ composer.lock
composer.phar
autoload.php
/vendor/
/temp/
/temp/
humbuglog.*
humbug.phar*Ta
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://api.travis-ci.org/renq/Impreso.png)](https://travis-ci.org/renq/Impreso)
[![Build Status](https://travis-ci.org/renq/Impreso.svg?branch=master)](https://travis-ci.org/renq/Impreso)
[![Coverage Status](https://coveralls.io/repos/renq/Impreso/badge.png)](https://coveralls.io/r/renq/Impreso)

Impreso
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require-dev": {
"satooshi/php-coveralls": "dev-master",
"phpunit/phpcov": "*",
"phpunit/phpunit": "*",
"phpunit/phpunit": "4.7",
"sebastian/phpcpd": "*",
"pdepend/pdepend" : "1.1.0",
"squizlabs/php_codesniffer": "1.*"
Expand Down
12 changes: 12 additions & 0 deletions humbug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"timeout": 10,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "humbuglog.txt",
"json": "humbuglog.json"
}
}
54 changes: 39 additions & 15 deletions src/Impreso/Container/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
use Impreso\Renderer\Renderer;
use Impreso\Element\Base as ElementBase;

/**
* Class Base
* @package Impreso\Container
*/
class Base extends ElementBase
{
private $elements = array();
private $renderer;

public function addElement(\Impreso\Element\Base $element)
public function addElement(ElementBase $element)
{
if (!$element->getId()) {
$element->setId($this->stringToId($element->getName()));
Expand All @@ -26,16 +30,21 @@ public function addElement(\Impreso\Element\Base $element)
}

/**
* @return \Impreso\Element\Base[] array
* @return ElementBase[] array
*/
public function getElements()
{
return $this->elements;
}

/**
* @param array $data
* @param bool $strict
* @return $this
*/
public function populate(array $data, $strict = false)
{
$rows = empty($data) ? array() : explode('&', http_build_query($data));
$rows = empty($data) ? [] : explode('&', http_build_query($data));
foreach ($rows as $row) {
list($key, $value) = array_map('urldecode', explode('=', $row));
$elements = $this->getElementsByName($key);
Expand All @@ -48,43 +57,51 @@ public function populate(array $data, $strict = false)
// I'm not proud of this code, but it works... TODO refactor this
$element->setValue($data[substr($key, 0, -3)]);
continue;
}
else {
} else {
$element->setValue($value);
}
}
}
return $this;
}

/**
* @return array
*/
public function getData()
{
$result = array();
$tmp = array();
$result = [];
$tmp = [];
foreach ($this->getElements() as $element) {
/* @var $element \Impreso\Element\Element */
if ($element->has('disabled')) continue;
if ($element->has('disabled')) {
continue;
}

$value = $element->getValue();
if (is_array($value)) {
foreach ($value as $v) {
$tmp[] = urlencode($element->getName()).'='.urlencode($v);
}
}
else {
} else {
$tmp[] = urlencode($element->getName()).'='.urlencode($element->getValue());
}
}
parse_str(implode('&', $tmp), $result);
return $result;
}

/**
* @param $id string
* @return bool
*/
public function hasElement($id)
{
return (isset($this->elements[$id]));
}

/**
* @param $name
* @param $name string
* @return \Impreso\Element\Base[]
*/
public function getElementsByName($name)
Expand Down Expand Up @@ -151,22 +168,29 @@ public function getRenderer()
public function render()
{
if (!$this->getRenderer() instanceof Renderer) {
throw new \UnexpectedValueException('No renderer. Set renderer first using setRenderer() method.');
throw new \UnexpectedValueException('No renderer. Set renderer first by using setRenderer() method.');
}
return $this->getRenderer()->render($this);
}

/**
* @return string
*/
public function __toString()
{
try {
return (string)$this->render();
}
catch (\UnexpectedValueException $e) {
} catch (\UnexpectedValueException $e) {
return 'ERROR (Exception): ' . $e->getMessage();
}
}

protected function stringToId($string){
/**
* @param $string
* @return string
*/
protected function stringToId($string)
{
$id = preg_replace('/[^0-9a-z_-]/', '', $string);
$num = 0;
$result = $id;
Expand Down
5 changes: 3 additions & 2 deletions src/Impreso/Element/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Impreso\Element;

abstract class Base
abstract class Base implements Renderable
{

private $validAttributes = array();
Expand Down Expand Up @@ -79,7 +79,8 @@ public function remove($attribute)
unset($this->attributes[$attribute]);
}

public function getAttributes() {
public function getAttributes()
{
return $this->attributes;
}

Expand Down
17 changes: 17 additions & 0 deletions src/Impreso/Element/Renderable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: michal.lipek
* Date: 2015-12-16
* Time: 13:07
*/

namespace Impreso\Element;

interface Renderable
{
/**
* @return string
*/
public function render();
}
13 changes: 9 additions & 4 deletions src/Impreso/Renderer/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@

namespace Impreso\Renderer;


use Impreso\Container\Base;

/**
* Interface Renderer
* @package Impreso\Renderer
*/
interface Renderer
{

/**
* @param Base $container
* @return $this
*/
public function render(Base $container);

}
}
28 changes: 28 additions & 0 deletions tests/Impreso/Container/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,32 @@ public function testEmptyPopulate()
$result = $base->populate(array());
$this->assertEquals($base, $result);
}

public function testSetAndGetRenderer()
{
/* @var $renderer \Impreso\Renderer\Renderer | \PHPUnit_Framework_MockObject_MockObject */
$renderer = $this->getMockForAbstractClass('Impreso\Renderer\Renderer');
/* @var $base \Impreso\Container\Base | \PHPUnit_Framework_MockObject_MockObject */
$base = $this->getMockForAbstractClass('\Impreso\Container\Base');
$result = $base->setRenderer($renderer);

$this->assertSame($base, $result);
$this->assertSame($base->getRenderer(), $renderer);
}

public function testSetElementId()
{
/* @var $base \Impreso\Container\Base | \PHPUnit_Framework_MockObject_MockObject */
$base = $this->getMockForAbstractClass('\Impreso\Container\Base');
$element1 = new Text('text');
$element2 = new Text('text');

$this->assertEmpty($element1->getId());

$base->addElement($element1);
$this->assertEquals('text', $element1->getId());

$base->addElement($element2);
$this->assertEquals('text1', $element2->getId());
}
}
15 changes: 10 additions & 5 deletions tests/Impreso/Container/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Tests\Impreso\Container;


use Impreso\Container\Form;
use Impreso\Element\Hidden;
use Impreso\Element\Password;
Expand All @@ -28,13 +27,13 @@ public function testPassword()
$password2 = new Password('repeat');
$password2->addValidator(new CustomValidator(
'repeat error',
function($value) use ($password) {
function ($value) use ($password) {
return $password->getValue() == $value;
}
));

$form->addElement($password)
->addElement($password2);
$form->addElement($password);
$form->addElement($password2);

$form->populate(array(
'password' => 'ABCdef0123!@#',
Expand Down Expand Up @@ -65,7 +64,6 @@ public function testGetElementsByName()
$input = new Text('age');
$form->addElement($input);

//$this->assertEquals(array($select), $form->getElementsByName('name'));
$this->assertEquals(array($select), $form->getElementsByName('name[]'));
$this->assertEquals(array($input), $form->getElementsByName('age'));
}
Expand Down Expand Up @@ -124,4 +122,11 @@ public function testGetAllErrorsWhenValidFieldsAreHidden()

$this->assertEquals($expectedResult, $form->getAllErrors(true));
}

public function testFluentInterface()
{
$input = new Text('name');
$form = new Form();
$this->assertSame($form, $form->addElement($input));
}
}
40 changes: 33 additions & 7 deletions tests/Impreso/Element/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@

namespace Tests\Impreso\Element;


class BaseTest extends \PHPUnit_Framework_TestCase
{
public function testValidAttributes()
{
/* @var $base \Impreso\Element\Base */
$base = $this->getMockForAbstractClass('\Impreso\Element\Base');
$base = $this->getBaseMock();
$base->setValidAttributes(array());
$this->assertEquals(array(), $base->getValidAttributes());

Expand All @@ -30,8 +28,7 @@ public function testValidAttributes()

public function testSetGetAttributes()
{
/* @var $base \Impreso\Element\Base */
$base = $this->getMockForAbstractClass('\Impreso\Element\Base');
$base = $this->getBaseMock();
$base->setValidAttributes(array('id', 'class'));
$base->set('id', 'my-id');
$base->set('class', 'my-class');
Expand All @@ -43,9 +40,38 @@ public function testSetGetAttributes()

public function testIncorrectAttribute()
{
/* @var $base \Impreso\Element\Base */
$base = $this->getMockForAbstractClass('\Impreso\Element\Base');
$base = $this->getBaseMock();
$this->setExpectedException('\InvalidArgumentException');
$base->set('ugly-attr', 'wrong');
}

public function testFluentInterface()
{
$base = $this->getBaseMock();
$this->assertSame($base, $base->setValidAttributes(['none']));
}

public function testSetName()
{
$base = $this->getBaseMock();
$base->setValidAttributes(['name']);
$result = $base->setName('test');
$this->assertSame($base, $result);
}

public function testSetId()
{
$base = $this->getBaseMock();
$result = $base->setId('test');
$this->assertSame($base, $result);
}

/**
* @return \Impreso\Element\Base | \PHPUnit_Framework_MockObject_MockObject
*/
private function getBaseMock()
{
$base = $this->getMockForAbstractClass('\Impreso\Element\Base');
return $base;
}
}

0 comments on commit 7958b7a

Please sign in to comment.