Skip to content

Commit

Permalink
* Make source code compatible with PHP 5.4
Browse files Browse the repository at this point in the history
* Use PHPUnit mock instead of Mockery
* Add more unit-tests to coverage all source code
  • Loading branch information
nthachus committed Feb 22, 2018
1 parent 992b645 commit 51444ec
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 163 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class UserController extends Controller {

if ($validator->fails())
{
// actually withErrors is not really neccessary because we already show errors at client side for normal users
// actually withErrors is not really necessary because we already show errors at client side for normal users
return Redirect::back()->withErrors($validator);
}

Expand Down Expand Up @@ -187,7 +187,7 @@ Form::converter()->route()->extend('someotherrule', function($name, $parameters)
});
```

Second, you can create your own converter (which extends baseconverter or any current plugin converter) in `Bllim\Laravalid\Converter\` namespace and change plugin configuration in config file with your own plugin name.
Second, you can create your own converter (which extends `Base\Converter` or any current plugin converter) in `Bllim\Laravalid\Converter\` namespace and change plugin configuration in config file with your own plugin name.

> **Note:** If you are creating a converter for some existed html/js plugin please create it in `converter` folder and send a pull-request.
Expand Down Expand Up @@ -244,7 +244,7 @@ To use Jquery Validation, change plugin to `JqueryValidation` in config file and
### Contribution
You can fork and contribute to development of the package. All pull requests is welcome.

**Convertion Logic**
**Conversion Logic**
Package converts rules by using converters (in `src/Bllim/Laravalid/converter`). It uses `Converter` class of chosen plugin which extends `Converter/Base/Converter` class.
You can look at existed methods and plugins to understand how it works. Explanation will be ready, soon.

Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=5.4.0",
"illuminate/config": "~4.2",
"illuminate/html": "~4.2",
"illuminate/support": "~4.2",
Expand All @@ -19,8 +19,7 @@
"illuminate/translation": "~4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9"
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-0": {
Expand Down
18 changes: 16 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.0/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
Expand All @@ -8,11 +10,23 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">./src/</directory>
<exclude>
<directory suffix=".php">./vendor/</directory>
</exclude>
</whitelist>
</filter>

<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
</logging>
</phpunit>
2 changes: 1 addition & 1 deletion src/Bllim/Laravalid/converter/Base/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct($app)
$config = $app['config'];
$routeUrl = $app['url']->to($config->get('laravalid::route', 'laravalid'));

$ns = substr(static::class, 0, -9) ?: '\\';
$ns = substr($class = get_class($this), 0, strrpos($class, '\\')) . '\\';
($class = $ns . 'Rule') and static::$rule = new $class($routeUrl, $app['encrypter']);
($class = $ns . 'Message') and static::$message = new $class($app['translator']);
($class = $ns . 'Route') and static::$route = new $class($app['validator'], $app['encrypter']);
Expand Down
2 changes: 1 addition & 1 deletion src/Bllim/Laravalid/converter/JqueryValidation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function mergeOutputAttributes(array $outputAttributes, array &$ruleAttri
}
}

$outputAttributes += $ruleAttributes;
$outputAttributes = parent::mergeOutputAttributes($outputAttributes, $ruleAttributes, $inputType);

// remove duplicated rule attributes
if (!empty($inputType) && isset($ruleAttributes[$k = 'data-rule-' . $inputType]) && strcasecmp('true', $ruleAttributes[$k]) == 0)
Expand Down
47 changes: 19 additions & 28 deletions tests/Bllim/Laravalid/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

class FormBuilderTest extends \PHPUnit_Framework_TestCase
{

/**
* @var \Mockery\MockInterface|Converter\Base\Converter
* @var \PHPUnit_Framework_MockObject_MockObject|Converter\Base\Converter
*/
protected $converter;

Expand All @@ -19,28 +18,22 @@ protected function setUp()
{
parent::setUp();

$app = Converter\ConverterTest::initApplicationMock();
$this->converter = \Mockery::mock(new Converter\JqueryValidation\Converter($app));
$app = Converter\ConverterTest::initApplicationMock($this);
$this->converter = ($this->getName(false) == 'testIntegration')
? new Converter\JqueryValidation\Converter($app)
: $this->getMock(__NAMESPACE__ . '\Converter\Base\Converter', ['set', 'reset', 'convert'], [], '', false);

$this->form = new FormBuilder(new HtmlBuilder($url = $app['url']), $url, '_csrf_token', $this->converter);
}

protected function tearDown()
{
parent::tearDown();
\Mockery::close();
}

/**
* @param string $paramValue
* @param string $expectedValue
* @dataProvider dataForTestRawAttributeName
*/
public function testRawAttributeName($paramValue, $expectedValue)
{
$this->converter->shouldReceive('convert')->once()->andReturnUsing(function ($name) {
return $name;
});
$this->converter->expects($this->once())->method('convert')->willReturnArgument(0);
$value = Converter\ConverterTest::invokeMethod($this->form, 'getValidationAttributes', $paramValue);

$this->assertEquals($expectedValue, $value);
Expand All @@ -62,25 +55,25 @@ public function dataForTestRawAttributeName()

public function testSetValidation()
{
$this->converter->shouldReceive('set')->with(null)->once();
$this->converter->expects($this->once())->method('set')->with(null);
$this->form->setValidation(null);

$this->converter->shouldReceive('reset')->once();
$this->converter->expects($this->once())->method('reset');
$this->form->resetValidation();
}

public function testOpen()
{
$this->converter->shouldReceive('set')->with($rules = ['bar'])->once();
$this->converter->expects($this->once())->method('set')->with($rules = ['bar']);
$html = $this->form->open(['url' => '/', 'method' => 'get'], $rules);

$this->assertEquals('<form method="GET" action="/" accept-charset="UTF-8">', $html);
}

public function testModel()
{
$this->converter->shouldReceive('set')->with($rules = ['foo' => true])->once();
$this->converter->shouldReceive('set')->with(null)->once();
$this->converter->expects($this->exactly(2))->method('set')
->withConsecutive([$rules = ['foo' => true]], [null]);

$html = $this->form->model(new \stdClass(), ['url' => '/', 'method' => 'get'], $rules);

Expand All @@ -89,22 +82,21 @@ public function testModel()

public function testClose()
{
$this->converter->shouldReceive('reset')->once();
$this->converter->expects($this->once())->method('reset');
$this->assertEquals('</form>', $this->form->close());
}

public function testInput()
{
$this->converter->shouldReceive('convert')->once()->andReturnUsing(function ($name, $type = null) {
return [$name => $type];
});
$this->converter->expects($this->exactly(2))->method('convert')
->withConsecutive(['foo', 'date'], ['bar', 'url'])
->willReturnOnConsecutiveCalls(['foo' => 'date'], []);
$html = $this->form->input('date', 'foo[]');

$this->assertStringEndsWith('>', $html);
$this->assertStringStartsWith('<input ', $html);
$this->assertContains(' foo="date"', $html);

$this->converter->shouldReceive('convert')->with('bar', 'url')->once()->andReturn([]);
$html = $this->form->url('bar[]');

$this->assertStringEndsWith('>', $html);
Expand All @@ -115,7 +107,7 @@ public function testInput()

public function testTextArea()
{
$this->converter->shouldReceive('convert')->with('bar', null)->once()->andReturn([]);
$this->converter->expects($this->once())->method('convert')->with('bar', null)->willReturn([]);
$html = $this->form->textarea('bar[]');

$this->assertStringEndsWith('>', $html);
Expand All @@ -125,7 +117,7 @@ public function testTextArea()

public function testSelect()
{
$this->converter->shouldReceive('convert')->with('bar_foo', null)->once()->andReturn([]);
$this->converter->expects($this->once())->method('convert')->with('bar_foo', null)->willReturn([]);
$html = $this->form->select('bar_foo');

$this->assertStringEndsWith('>', $html);
Expand All @@ -135,15 +127,15 @@ public function testSelect()

public function testCheckbox()
{
$this->converter->shouldReceive('convert')->with('foo', 'checkbox')->once()->andReturn([]);
$this->converter->expects($this->exactly(2))->method('convert')
->withConsecutive(['foo', 'checkbox'], ['bar', 'radio'])->willReturn([]);
$html = $this->form->checkbox('foo[]');

$this->assertStringEndsWith('>', $html);
$this->assertStringStartsWith('<input ', $html);
$this->assertContains(' name="foo[]"', $html);
$this->assertContains(' type="checkbox"', $html);

$this->converter->shouldReceive('convert')->with('bar', 'radio')->once()->andReturn([]);
$html = $this->form->radio('bar[]');

$this->assertStringEndsWith('>', $html);
Expand Down Expand Up @@ -333,5 +325,4 @@ public function testIntegration()
$this->assertEquals('</form>', $this->form->close());
$this->assertEquals([], $this->converter->getValidationRules());
}

}
105 changes: 105 additions & 0 deletions tests/Bllim/Laravalid/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php namespace Bllim\Laravalid;

use Illuminate\Html\HtmlBuilder;

class ServiceProviderTest extends \PHPUnit_Framework_TestCase
{
public function testDeferred()
{
$provider = new LaravalidServiceProvider(null);

$this->assertAttributeSame(null, 'app', $provider);
$this->assertFalse($provider->isDeferred());
}

public function testProvides()
{
$provider = new LaravalidServiceProvider(null);
$this->assertEquals(array('laravalid'), $provider->provides());
}

public function testRegister()
{
$app = $this->getMock('Illuminate\Container\Container', array('bound', 'bind', 'make'));

$app->expects($this->once())->method('bound')->with('html')->willReturn(false);
$app->expects($this->exactly(2))->method('bind')
->withConsecutive(
array('html', $this->callback(function ($closure) use ($app, &$html) {
$html = $closure($app);
return ($html instanceof HtmlBuilder);
}), $this->isTrue()),
array('laravalid', $this->callback(function ($closure) use ($app) {
$form = $closure($app);
return ($form instanceof FormBuilder);
}), $this->isTrue())
);

$url = $this->getMock('Illuminate\Routing\UrlGenerator', array('to'), array(), '', false);
$config = $this->getMock('Illuminate\Config\Repository', array('get'), array(), '', false);
$session = $this->getMock('Illuminate\Session\Store', array('getToken'), array(), '', false);

$app->expects($this->atLeast(4))->method('make')->withConsecutive(['url'], ['config'])->willReturnMap(array(
array('url', array(), $url),
array('config', array(), $config),
array('session.store', array(), $session),
array('html', array(), &$html),
));

$config->expects($this->once())->method('get')->with('laravalid::plugin')->willReturn('\MyTestPlugin');
$session->expects($this->once())->method('getToken')->willReturn(uniqid());

if (!class_exists('MyTestPlugin\Converter'))
eval('namespace MyTestPlugin { class Converter extends \\' . __NAMESPACE__ . '\Converter\Base\Converter { public function __construct() { echo __CLASS__; } } }');

$this->expectOutputString('MyTestPlugin\Converter');
/** @noinspection PhpParamsInspection */
$provider = new LaravalidServiceProvider($app);
$provider->register();
}

public function testBoot()
{
$app = $this->getMock('Illuminate\Container\Container', array('make'));

$files = $this->getMock('Illuminate\Filesystem\Filesystem', null);
$config = $this->getMock('Illuminate\Config\Repository', array('get', 'package'), array(), '', false);
$router = $this->getMock('Illuminate\Routing\Router', array('any'), array(), '', false);
$route = $this->getMock('Illuminate\Routing\Route', array('where'), array(), '', false);

$form = $this->getMock(__NAMESPACE__ . '\FormBuilder', array('converter'), array(), '', false);
$converter = $this->getMock(__NAMESPACE__ . '\Converter\Base\Converter', array('route'), array(), '', false);
$valid = $this->getMock(__NAMESPACE__ . '\Converter\Base\Route', array('convert'), array(), '', false);
$request = $this->getMock('Illuminate\Http\Request', array('all'), array(), '', false);

$app->expects($this->atLeast(5))->method('make')->withConsecutive(['files'], ['config'])->willReturnMap(array(
array('files', array(), $files),
array('config', array(), $config),
array('router', array(), $router),
array('laravalid', array(), $form),
array('request', array(), $request),
));

$path = realpath(__DIR__ . '/../../../src');
$config->expects($this->once())->method('package')->with('bllim/laravalid', $path . '/config', 'laravalid');
$config->expects($this->once())->method('get')->with('laravalid::route', 'laravalid')->willReturnArgument(1);

$router->expects($this->once())->method('any')->willReturnCallback(function ($url, $action) use ($route) {
static::assertEquals('laravalid/{rule}', $url);
static::assertEquals('["exists",[{"params":"~"}]]', $action('exists'));
return $route;
});
$route->expects($this->once())->method('where')->with('rule', '[\w-]+');

$form->expects($this->once())->method('converter')->willReturn($converter);
$converter->expects($this->once())->method('route')->willReturn($valid);
$valid->expects($this->once())->method('convert')->willReturnCallback(function () {
return json_encode(func_get_args());
});
$request->expects($this->once())->method('all')->willReturn(array('params' => '~'));

/** @noinspection PhpParamsInspection */
$provider = new LaravalidServiceProvider($app);
$provider->boot();
}
}
Loading

0 comments on commit 51444ec

Please sign in to comment.