Skip to content

Commit

Permalink
Merge pull request #51 from nthachus/master
Browse files Browse the repository at this point in the history
Update to release version 1.3.1
  • Loading branch information
bgultekin committed Feb 24, 2018
2 parents c8e66ec + b9e61ec commit 9288c6c
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 177 deletions.
13 changes: 12 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ php:
- 5.4
- 5.5
- 5.6
- 7.1
- hhvm

matrix:
allow_failures:
- php: hhvm
fast_finish: true

cache:
directories:
- vendor
- $HOME/.composer/cache

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev
- travis_retry composer install --no-interaction --no-progress

script: phpunit
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
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
}
],
"require": {
"php": ">=5.3.0",
"laravelcollective/html": "^5.2",
"illuminate/support": "~5.2",
"php": ">=5.4.0",
"laravelcollective/html": "~5.0",
"illuminate/support": "~5.0",
"illuminate/validation": "~5.0",
"illuminate/routing": "~5.0",
"illuminate/translation": "~5.0"
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "~4.0"
},
"autoload": {
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>
15 changes: 7 additions & 8 deletions src/Bllim/Laravalid/Converter/Base/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Bllim\Laravalid\Converter\Base;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;

/**
* Base converter class for converter plugins.
*
Expand Down Expand Up @@ -79,17 +76,19 @@ abstract class Converter
*/
protected $useLaravelMessages;

public function __construct(Application $app)
/**
* @param \Illuminate\Contracts\Foundation\Application|\ArrayAccess $app
*/
public function __construct($app)
{
/* @var $app Application|\ArrayAccess */
$config = $app['config'];
/* @var $config \Illuminate\Contracts\Config\Repository */
$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[ResponseFactory::class], $app['encrypter']);
($class = $ns . 'Route') and static::$route = new $class($app['validator'], $app['Illuminate\Contracts\Routing\ResponseFactory'], $app['encrypter']);

$this->useLaravelMessages = $config->get('laravalid.useLaravelMessages', true);
}
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 @@ -212,7 +212,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
5 changes: 4 additions & 1 deletion src/Bllim/Laravalid/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class FormBuilder extends \Collective\Html\FormBuilder

public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Converter\Base\Converter $converter, Request $request = null)
{
parent::__construct($html, $url, $view, $csrfToken, $request);
if (with(new \ReflectionClass('Collective\Html\FormBuilder'))->getConstructor()->getNumberOfParameters() > 3)
parent::__construct($html, $url, $view, $csrfToken, $request);
else
parent::__construct($html, $url, $csrfToken);

$this->converter = $converter;
}
Expand Down
74 changes: 31 additions & 43 deletions tests/Bllim/Laravalid/Converter/ConverterTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<?php namespace Bllim\Laravalid\Converter;

use Bllim\Laravalid\FormBuilderTest;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Translation\LoaderInterface;
use Illuminate\Translation\Translator;

class ConverterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Mockery\MockInterface|Application
* @var \PHPUnit_Framework_MockObject_MockObject|\Illuminate\Contracts\Foundation\Application|\ArrayAccess
*/
protected $app;

Expand All @@ -21,39 +14,42 @@ class ConverterTest extends \PHPUnit_Framework_TestCase
*/
protected $converter;

static function initApplicationMock($mocks = [])
/**
* @param \PHPUnit_Framework_TestCase $test
* @param bool $trans Mock Translator::get() method?
* @return \PHPUnit_Framework_MockObject_MockObject|\Illuminate\Contracts\Foundation\Application|\ArrayAccess
*/
static function initApplicationMock(\PHPUnit_Framework_TestCase $test, $trans = false)
{
$config = \Mockery::mock(Repository::class);
$config->shouldReceive('get')->zeroOrMoreTimes()->andReturnUsing(function ($key, $default = null) {
$config = $test->getMock('Illuminate\Config\Repository', ['get'], [], '', false);
$config->expects($test->any())->method('get')->willReturnCallback(function ($key, $default = null) {
return isset($default) ? $default : ($key == 'laravalid::plugin' ? 'JqueryValidation' : null);
});

$url = \Mockery::mock(UrlGenerator::class);
$url->shouldReceive('to')->andReturnUsing(function ($path) {
$url = $test->getMock('Illuminate\Routing\UrlGenerator', ['to'], [], '', false);
$url->expects($test->any())->method('to')->willReturnCallback(function ($path) {
return '/' . ltrim($path, '/');
});

/* @var $encrypter \Mockery\MockInterface|Encrypter */
$encrypter = \Mockery::mock(Encrypter::class);
$encrypter->shouldReceive('encrypt')->andReturnUsing(function ($data) {
$encrypter = $test->getMock('Illuminate\Contracts\Encryption\Encrypter');
$encrypter->expects($test->any())->method('encrypt')->willReturnCallback(function ($data) {
return str_replace(['/', '+', '='], ['_', '-', ''], base64_encode($data));
});

/* @var $loader \Mockery\MockInterface|LoaderInterface|Loader */
$loader = \Mockery::mock(interface_exists(Loader::class) ? Loader::class : LoaderInterface::class);
$loader->shouldReceive('load')->with('en', 'validation', '*')->andReturn(static::$messages);
$loader = $test->getMock(interface_exists($cls = 'Illuminate\Contracts\Translation\Loader') ? $cls : 'Illuminate\Translation\LoaderInterface');
$loader->expects($test->any())->method('load')->with('en', 'validation', '*')->willReturn(static::$messages);
//
$translator = \Mockery::mock(new Translator($loader, 'en'));
$translator->shouldReceive('has')->zeroOrMoreTimes()->andReturn(false);

$mocks += compact('config', 'url', 'encrypter', 'translator');
/* @var $app \Mockery\MockInterface|Application */
$app = \Mockery::mock(Application::class . ', ' . \ArrayAccess::class);

$app->shouldReceive('make')->andReturnUsing($func = function ($key) use ($mocks) {
return isset($mocks[$key]) ? $mocks[$key] : null;
});
$app->shouldReceive('offsetGet')->zeroOrMoreTimes()->andReturnUsing($func);
$translator = $test->getMock('Illuminate\Translation\Translator', !$trans ? ['has'] : ['has', 'get'], [$loader, 'en']);
$translator->expects($test->any())->method('has')->willReturn(false);

$app = $test->getMock('Illuminate\Container\Container', ['make']);
$app->expects($test->any())->method('make')->willReturnMap([
['config', [], $config],
['url', [], $url],
['encrypter', [], $encrypter],
['translator', [], $translator],
['view', [], $test->getMock('Illuminate\Contracts\View\Factory')],
]);

return $app;
}
Expand All @@ -62,16 +58,10 @@ protected function setUp()
{
parent::setUp();

$this->app = $this->initApplicationMock();
$this->app = $this->initApplicationMock($this, $this->getName(false) == 'testDefaultErrorMessage');
$this->converter = new JqueryValidation\Converter($this->app);
}

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

static function invokeMethod($object, $methodName, $parameters = array())
{
$method = new \ReflectionMethod(get_class($object), $methodName);
Expand All @@ -82,9 +72,9 @@ static function invokeMethod($object, $methodName, $parameters = array())

public function testConstructor()
{
$this->assertEquals(JqueryValidation\Rule::class, get_class($this->converter->rule()));
$this->assertEquals(JqueryValidation\Message::class, get_class($this->converter->message()));
$this->assertEquals(JqueryValidation\Route::class, get_class($this->converter->route()));
$this->assertInstanceOf(__NAMESPACE__ . '\JqueryValidation\Rule', $this->converter->rule());
$this->assertInstanceOf(__NAMESPACE__ . '\JqueryValidation\Message', $this->converter->message());
$this->assertInstanceOf(__NAMESPACE__ . '\JqueryValidation\Route', $this->converter->route());

$this->assertAttributeEquals($this->converter->rule(), 'rule', $this->converter);
$this->assertAttributeEquals($this->converter->message(), 'message', $this->converter);
Expand Down Expand Up @@ -178,9 +168,7 @@ public function dataForGetTypeOfInput()
*/
public function testDefaultErrorMessage($params = [], $expected = [])
{
$translator = $this->app['translator'];
/* @var $translator \Mockery\MockInterface|Translator */
$translator->shouldReceive('get')->times(2)->andReturnUsing(function ($key, $data = []) {
$this->app['translator']->expects($this->exactly(2))->method('get')->willReturnCallback(function ($key, $data = []) {
return $key . (empty($data) ? '' : json_encode($data));
});

Expand Down
56 changes: 19 additions & 37 deletions tests/Bllim/Laravalid/Converter/MessageTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php namespace Bllim\Laravalid\Converter;

use Illuminate\Translation\Translator;

class MessageTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Mockery\MockInterface|Translator
* @var \PHPUnit_Framework_MockObject_MockObject|\Illuminate\Translation\Translator
*/
protected $translator;

Expand All @@ -18,16 +16,10 @@ protected function setUp()
{
parent::setUp();

$this->translator = \Mockery::mock(Translator::class);
$this->translator = $this->getMock('Illuminate\Translation\Translator', ['has', 'get'], [], '', false);
$this->message = new JqueryValidation\Message($this->translator);
}

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

public function testExtend()
{
$this->assertEmpty($this->message->convert('foo'));
Expand All @@ -43,38 +35,28 @@ public function testExtend()

public function testGetValidationMessage()
{
$this->translator->shouldReceive('has')->once()->andReturn(false);
$this->translator->shouldReceive('get')->with('validation.attributes.first_name')->once()->andReturnUsing(function ($key) {
return $key;
});
$this->translator->shouldReceive('get')
->with('validation.active_url', ['other' => 'old_name', 'attribute' => 'first name'])->once()
->andReturnUsing(function ($key, $data) {
return ($key == 'validation.active_url') ? $data['attribute'] . ' === ' . $data['other'] : $key;
});
$this->translator->expects($this->exactly(3))->method('has')
->withConsecutive([$this->anything()], ['validation.custom.lastName.email'], [$this->anything()])
->willReturnOnConsecutiveCalls(false, true, false);
//
$this->translator->expects($this->exactly(6))->method('get')
->withConsecutive(
['validation.attributes.first_name'], ['validation.active_url', ['other' => 'old_name', 'attribute' => 'first name']],
['validation.attributes.lastName'], ['validation.custom.lastName.email', ['max' => '100', 'attribute' => 'Last name']],
['validation.attributes.foo'], ['validation.max.numeric', ['attribute' => 'Bar']]
)
->willReturnOnConsecutiveCalls(
'validation.attributes.first_name', 'first name === old_name',
'Last name', 'Last name <= 100',
'Bar', 'validation.max.numeric'
);

$value = $this->message->getValidationMessage('first_name', 'activeUrl', ['other' => 'old_name']);
$this->assertEquals('first name === old_name', $value);

//
$this->translator->shouldReceive('has')->with('validation.custom.lastName.email')->once()->andReturn(true);
$this->translator->shouldReceive('get')->with('validation.attributes.lastName')->once()->andReturn('Last name');
$this->translator->shouldReceive('get')
->with('validation.custom.lastName.email', ['max' => '100', 'attribute' => 'Last name'])->once()
->andReturnUsing(function ($key, $data) {
return ($key == 'validation.custom.lastName.email') ? $data['attribute'] . ' <= ' . $data['max'] : $key;
});

$value = $this->message->getValidationMessage('lastName', 'email', ['max' => '100']);
$this->assertEquals('Last name <= 100', $value);

//
$this->translator->shouldReceive('has')->once()->andReturn(false);
$this->translator->shouldReceive('get')->with('validation.attributes.foo')->once()->andReturn('Bar');
$this->translator->shouldReceive('get')->with('validation.max.numeric', ['attribute' => 'Bar'])->once()->andReturnUsing(function ($key) {
return $key;
});

$value = $this->message->getValidationMessage('foo', 'max', [], 'numeric');
$this->assertEquals('validation.max.numeric', $value);
}
Expand All @@ -87,8 +69,8 @@ public function testGetValidationMessage()
*/
public function testAllRules($name = '', $params = [], $expected = [])
{
$this->translator->shouldReceive('has')->times(2)->andReturn(preg_match('/^[A-Z]/', $name));
$this->translator->shouldReceive('get')->atLeast()->times(4)->andReturnUsing(function ($key, $data = null) {
$this->translator->expects($this->exactly(2))->method('has')->willReturn(preg_match('/^[A-Z]/', $name));
$this->translator->expects($this->atLeast(4))->method('get')->willReturnCallback(function ($key, $data = null) {
return $key . (empty($data) ? '' : json_encode($data));
});

Expand Down
Loading

0 comments on commit 9288c6c

Please sign in to comment.