Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\Bridge\Laravel;
use Cocur\Slugify\Bridge\Laravel\SlugifyServiceProvider;
use Illuminate\Foundation\Application;
/**
* SlugifyServiceProviderTest
*
* @category test
* @package cocur/slugify
* @subpackage bridge
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @author Colin Viebrock
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
* @group unit
*/
class SlugifyProviderTest extends \PHPUnit_Framework_TestCase
{
/** @var Application */
private $app;
/** @var SlugifyServiceProvider */
private $provider;
protected function setUp()
{
$this->app = new Application();
$this->provider = new SlugifyServiceProvider($this->app);
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Laravel\SlugifyServiceProvider::register()
*/
public function registerRegistersTheServiceProvider()
{
$this->provider->register();
// the service provider is deferred, so this forces it to load
$this->app->make('slugify');
$this->assertArrayHasKey('slugify', $this->app);
$this->assertInstanceOf('Cocur\Slugify\Slugify', $this->app['slugify']);
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Laravel\SlugifyServiceProvider::provides()
*/
public function containsReturnsTheNameOfThProvider()
{
$this->assertContains('slugify', $this->provider->provides());
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Cocur\Slugify\Tests\Bridge\Latte;
use Cocur\Slugify\Bridge\Latte\SlugifyHelper;
use Mockery as m;
/**
* SlugifyHelperTest
*
* @category test
* @package cocur/slugify
* @subpackage bridge
* @author Lukáš Unger <looky.msc@gmail.com>
* @license http://www.opensource.org/licenses/MIT The MIT License
* @group unit
*/
class SlugifyHelperTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->slugify = m::mock('Cocur\Slugify\SlugifyInterface');
$this->helper = new SlugifyHelper($this->slugify);
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Latte\SlugifyHelper::slugify()
*/
public function slugify()
{
$this->slugify->shouldReceive('slugify')->with('hällo wörld', '_')->once()->andReturn('haello_woerld');
$this->assertEquals('haello_woerld', $this->helper->slugify('hällo wörld', '_'));
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Cocur\Slugify\Tests\Bridge\League;
use Cocur\Slugify\Bridge\League\SlugifyServiceProvider;
use Cocur\Slugify\RuleProvider\DefaultRuleProvider;
use Cocur\Slugify\RuleProvider\RuleProviderInterface;
use Cocur\Slugify\SlugifyInterface;
use League\Container\Container;
use Mockery as m;
class SlugifyServiceProviderTest extends \PHPUnit_Framework_TestCase
{
public function testProvidesSlugify()
{
$container = new Container();
$container->addServiceProvider(new SlugifyServiceProvider());
$slugify = $container->get(SlugifyInterface::class);
$this->assertInstanceOf(SlugifyInterface::class, $slugify);
$this->assertAttributeInstanceOf(DefaultRuleProvider::class, 'provider', $slugify);
}
public function testProvidesSlugifyAsSharedService()
{
$container = new Container();
$container->addServiceProvider(new SlugifyServiceProvider());
$slugify = $container->get(SlugifyInterface::class);
$this->assertSame($slugify, $container->get(SlugifyInterface::class));
}
public function testProvidesSlugifyUsingSharedConfigurationOptions()
{
$container = new Container();
$options = [
'lowercase' => false,
];
$container->share('config.slugify.options', $options);
$container->addServiceProvider(new SlugifyServiceProvider());
/* @var SlugifyInterface $slugify */
$slugify = $container->get(SlugifyInterface::class);
$slug = 'Foo-Bar-Baz';
$this->assertSame($slug, $slugify->slugify($slug));
}
public function testProvidesSlugifyUsingSharedProvider()
{
$container = new Container();
$ruleProvider = $this->getRuleProviderMock();
$container->share(RuleProviderInterface::class, $ruleProvider);
$container->addServiceProvider(new SlugifyServiceProvider());
$slugify = $container->get(SlugifyInterface::class);
$this->assertAttributeSame($ruleProvider, 'provider', $slugify);
}
/**
* @return m\Mock|RuleProviderInterface
*/
private function getRuleProviderMock()
{
$ruleProvider = m::mock(RuleProviderInterface::class);
$ruleProvider
->shouldReceive('getRules')
->withAnyArgs()
->andReturn([])
;
return $ruleProvider;
}
}

View File

@@ -0,0 +1,133 @@
<?php
namespace Cocur\Slugify\Tests\Bridge\Nette;
use Cocur\Slugify\Bridge\Nette\SlugifyExtension;
use Mockery as m;
/**
* SlugifyExtensionTest
*
* @category test
* @package cocur/slugify
* @subpackage bridge
* @author Lukáš Unger <looky.msc@gmail.com>
* @license http://www.opensource.org/licenses/MIT The MIT License
* @group unit
*/
class SlugifyExtensionTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->extension = new SlugifyExtension();
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Nette\SlugifyExtension::loadConfiguration()
*/
public function loadConfiguration()
{
$slugify = m::mock('Nette\DI\ServiceDefinition');
$slugify
->shouldReceive('setClass')
->with('Cocur\Slugify\SlugifyInterface')
->once()
->andReturn($slugify);
$slugify
->shouldReceive('setFactory')
->with('Cocur\Slugify\Slugify')
->once()
->andReturn($slugify);
$helper = m::mock('Nette\DI\ServiceDefinition');
$helper
->shouldReceive('setClass')
->with('Cocur\Slugify\Bridge\Latte\SlugifyHelper')
->once()
->andReturn($helper);
$helper
->shouldReceive('setAutowired')
->with(false)
->once()
->andReturn($helper);
$builder = m::mock('Nette\DI\ContainerBuilder');
$builder
->shouldReceive('addDefinition')
->with('slugify.slugify')
->once()
->andReturn($slugify);
$builder
->shouldReceive('addDefinition')
->with('slugify.helper')
->once()
->andReturn($helper);
$compiler = m::mock('Nette\DI\Compiler');
$compiler
->shouldReceive('getContainerBuilder')
->once()
->andReturn($builder);
$this->extension->setCompiler($compiler, 'slugify');
$this->extension->loadConfiguration();
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Nette\SlugifyExtension::beforeCompile()
*/
public function beforeCompile()
{
$latteFactory = m::mock('Nette\DI\ServiceDefinition');
$latteFactory
->shouldReceive('addSetup')
->with('addFilter', ['slugify', ['@slugify.helper', 'slugify']])
->once()
->andReturn($latteFactory);
$latte = m::mock('Nette\DI\ServiceDefinition');
$latte
->shouldReceive('addSetup')
->with('addFilter', ['slugify', ['@slugify.helper', 'slugify']])
->once()
->andReturn($latte);
$builder = m::mock('Nette\DI\ContainerBuilder');
$builder
->shouldReceive('getByType')
->with('Nette\Bridges\ApplicationLatte\ILatteFactory')
->once()
->andReturn('latte.latteFactory');
$builder
->shouldReceive('hasDefinition')
->with('latte.latteFactory')
->once()
->andReturn(true);
$builder
->shouldReceive('getDefinition')
->with('latte.latteFactory')
->once()
->andReturn($latteFactory);
$builder
->shouldReceive('hasDefinition')
->with('nette.latte')
->once()
->andReturn(true);
$builder
->shouldReceive('getDefinition')
->with('nette.latte')
->once()
->andReturn($latte);
$compiler = m::mock('Nette\DI\Compiler');
$compiler
->shouldReceive('getContainerBuilder')
->once()
->andReturn($builder);
$this->extension->setCompiler($compiler, 'slugify');
$this->extension->beforeCompile();
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\Bridge\Plum;
use Cocur\Slugify\Bridge\Plum\SlugifyConverter;
use Mockery;
use PHPUnit_Framework_TestCase;
/**
* SlugifyConverterTest
*
* @package Cocur\Slugify\Bridge\Plum
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @copyright 2012-2015 Florian Eckerstorfer
* @group unit
*/
class SlugifyConverterTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Cocur\Slugify\Bridge\Plum\SlugifyConverter::__construct()
* @covers Cocur\Slugify\Bridge\Plum\SlugifyConverter::convert()
*/
public function convertSlugifiesString()
{
$slugify = Mockery::mock('Cocur\Slugify\SlugifyInterface');
$slugify->shouldReceive('slugify')->with('Hello World')->once()->andReturn('hello_world');
$converter = new SlugifyConverter($slugify);
$this->assertSame('hello_world', $converter->convert('Hello World'));
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Plum\SlugifyConverter::__construct()
* @covers Cocur\Slugify\Bridge\Plum\SlugifyConverter::convert()
*/
public function constructorCreatesSlugifyIfNoneIsProvided()
{
$converter = new SlugifyConverter();
$this->assertSame('hello-world', $converter->convert('Hello World'));
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\Bridge\Silex;
use Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider;
use Cocur\Slugify\Bridge\Twig\SlugifyExtension;
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
/**
* SlugifyServiceProviderTest
*
* @category test
* @package cocur/slugify
* @subpackage bridge
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
* @group unit
*/
class SlugifySilexProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider
*/
public function register()
{
// it seems like Application is not mockable.
$app = new Application();
$app->register(new SlugifyServiceProvider());
$app->boot();
$this->assertArrayHasKey('slugify', $app);
$this->assertArrayHasKey('slugify.provider', $app);
$this->assertArrayHasKey('slugify.options', $app);
$this->assertInstanceOf('Cocur\Slugify\Slugify', $app['slugify']);
}
/**
* @test
*/
public function registerWithTwig()
{
$app = new Application();
$app->register(new TwigServiceProvider());
$app->register(new SlugifyServiceProvider());
$this->assertTrue($app['twig']->hasExtension(SlugifyExtension::class));
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\Bridge\Symfony;
use Cocur\Slugify\Bridge\Symfony\CocurSlugifyBundle;
use Cocur\Slugify\Bridge\Symfony\CocurSlugifyExtension;
/**
* CocurSlugifyBundleTest
*
* @category test
* @package cocur/slugify
* @subpackage bridge
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
* @group unit
*/
class CocurSlugifyBundleTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Cocur\Slugify\Bridge\Symfony\CocurSlugifyBundle::getContainerExtension()
*/
public function testGetContainerExtension()
{
$bundle = new CocurSlugifyBundle();
static::assertInstanceOf(CocurSlugifyExtension::class, $bundle->getContainerExtension());
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\Bridge\Symfony;
use Cocur\Slugify\Bridge\Symfony\CocurSlugifyExtension;
use Mockery as m;
/**
* CocurSlugifyExtensionTest
*
* @category test
* @package cocur/slugify
* @subpackage bridge
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
* @group unit
*/
class CocurSlugifyExtensionTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->extension = new CocurSlugifyExtension();
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Symfony\CocurSlugifyExtension::load()
*/
public function load()
{
$twigDefinition = m::mock('Symfony\Component\DependencyInjection\Definition');
$twigDefinition
->shouldReceive('addTag')
->with('twig.extension')
->once()
->andReturn($twigDefinition);
$twigDefinition
->shouldReceive('setPublic')
->with(false)
->once();
$container = m::mock('Symfony\Component\DependencyInjection\ContainerBuilder');
$container
->shouldReceive('setDefinition')
->with('cocur_slugify', m::type('Symfony\Component\DependencyInjection\Definition'))
->once();
$container
->shouldReceive('setDefinition')
->with('cocur_slugify.twig.slugify', m::type('Symfony\Component\DependencyInjection\Definition'))
->once()
->andReturn($twigDefinition);
$container
->shouldReceive('setAlias')
->with('slugify', 'cocur_slugify')
->once();
$this->extension->load([], $container);
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the cocur/slugify package.
*
* (c) Enrico Stahn <enrico.stahn@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\Bridge\Symfony;
use Cocur\Slugify\Bridge\Symfony\Configuration;
use Symfony\Component\Config\Definition\Processor;
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
public function testAll()
{
$configs = [
[
'lowercase' => true,
'separator' => '_',
'regexp' => 'abcd',
'rulesets' => ['burmese', 'hindi']
],
];
$this->process($configs);
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testLowercaseOnlyAcceptsBoolean()
{
$configs = [['lowercase' => 'abc']];
$this->process($configs);
}
/**
* Processes an array of configurations and returns a compiled version.
*
* @param array $configs An array of raw configurations
*
* @return array A normalized array
*/
protected function process($configs)
{
$processor = new Processor();
return $processor->processConfiguration(new Configuration(), $configs);
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\Bridge\Twig;
use Cocur\Slugify\Bridge\Twig\SlugifyExtension;
use Mockery as m;
/**
* SlugifyExtensionTest
*
* @category test
* @package cocur/slugify
* @subpackage bridge
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
* @group unit
*/
class SlugifyExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Cocur\Slugify\SlugifyInterface|\Mockery\MockInterface
*/
protected $slugify;
/**
* @var SlugifyExtension
*/
protected $extension;
protected function setUp()
{
$this->slugify = m::mock('Cocur\Slugify\SlugifyInterface');
$this->extension = new SlugifyExtension($this->slugify);
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Twig\SlugifyExtension::getFilters()
*/
public function getFilters()
{
$filters = $this->extension->getFilters();
$this->assertCount(1, $filters);
$this->assertInstanceOf('\Twig_SimpleFilter', $filters[0]);
}
/**
* @test
* @covers Cocur\Slugify\Bridge\Twig\SlugifyExtension::slugifyFilter()
*/
public function slugifyFilter()
{
$this->slugify->shouldReceive('slugify')->with('hällo wörld', '_')->once()->andReturn('haello_woerld');
$this->assertEquals('haello_woerld', $this->extension->slugifyFilter('hällo wörld', '_'));
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Cocur\Slugify\Tests\Bridge\ZF2;
use Cocur\Slugify\Bridge\ZF2\Module;
/**
* Class ModuleTest
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class ModuleTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Module
*/
private $module;
protected function setUp()
{
$this->module = new Module();
}
/**
* @test
* @covers Cocur\Slugify\Bridge\ZF2\Module::getServiceConfig()
*/
public function getServiceConfig()
{
$smConfig = $this->module->getServiceConfig();
$this->assertTrue(is_array($smConfig));
$this->assertArrayHasKey('factories', $smConfig);
$this->assertArrayHasKey('Cocur\Slugify\Slugify', $smConfig['factories']);
$this->assertArrayHasKey('aliases', $smConfig);
$this->assertArrayHasKey('slugify', $smConfig['aliases']);
}
/**
* @test
* @covers Cocur\Slugify\Bridge\ZF2\Module::getViewHelperConfig()
*/
public function getViewHelperConfig()
{
$vhConfig = $this->module->getViewHelperConfig();
$this->assertTrue(is_array($vhConfig));
$this->assertArrayHasKey('factories', $vhConfig);
$this->assertArrayHasKey('slugify', $vhConfig['factories']);
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Cocur\Slugify\Tests\Bridge\ZF2;
use Cocur\Slugify\Bridge\ZF2\Module;
use Cocur\Slugify\Bridge\ZF2\SlugifyService;
use Zend\ServiceManager\ServiceManager;
/**
* Class SlugifyServiceTest
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyServiceTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SlugifyService
*/
private $slugifyService;
protected function setUp()
{
$this->slugifyService = new SlugifyService();
}
/**
* @test
* @covers Cocur\Slugify\Bridge\ZF2\SlugifyService::__invoke()
*/
public function invokeWithoutCustomConfig()
{
$sm = $this->createServiceManagerMock();
$slugify = call_user_func($this->slugifyService, $sm);
$this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify);
// Make sure reg exp is default one
$actual = 'Hello My Friend.zip';
$expected = 'hello-my-friend-zip';
$this->assertEquals($expected, $slugify->slugify($actual));
}
/**
* @test
* @covers Cocur\Slugify\Bridge\ZF2\SlugifyService::__invoke()
*/
public function invokeWithCustomConfig()
{
$sm = $this->createServiceManagerMock([
Module::CONFIG_KEY => [
'options' => ['regexp' => '/([^a-z0-9.]|-)+/']
]
]);
$slugify = call_user_func($this->slugifyService, $sm);
$this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify);
// Make sure reg exp is the one provided and dots are kept
$actual = 'Hello My Friend.zip';
$expected = 'hello-my-friend.zip';
$this->assertEquals($expected, $slugify->slugify($actual));
}
protected function createServiceManagerMock(array $config = [])
{
$sm = new ServiceManager();
$sm->setService('Config', $config);
return $sm;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Cocur\Slugify\Tests\Bridge\ZF2;
use Cocur\Slugify\Bridge\ZF2\SlugifyViewHelperFactory;
use Cocur\Slugify\Slugify;
use Zend\ServiceManager\ServiceManager;
use Zend\View\HelperPluginManager;
/**
* Class SlugifyViewHelperFactoryTest
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyViewHelperFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SlugifyViewHelperFactory
*/
private $factory;
protected function setUp()
{
$this->factory = new SlugifyViewHelperFactory();
}
/**
* @test
* @covers Cocur\Slugify\Bridge\ZF2\SlugifyViewHelperFactory::__invoke()
*/
public function createService()
{
$sm = new ServiceManager();
$sm->setService('Cocur\Slugify\Slugify', new Slugify());
$vhm = new HelperPluginManager();
$vhm->setServiceLocator($sm);
$viewHelper = call_user_func($this->factory, $vhm);
$this->assertInstanceOf('Cocur\Slugify\Bridge\ZF2\SlugifyViewHelper', $viewHelper);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Cocur\Slugify\Tests\Bridge\ZF2;
use Cocur\Slugify\Bridge\ZF2\SlugifyViewHelper;
use Cocur\Slugify\Slugify;
/**
* Class SlugifyViewHelperTest
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyViewHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SlugifyViewHelper
*/
private $viewHelper;
/**
* @var Slugify
*/
private $slugify;
/**
* @covers Cocur\Slugify\Bridge\ZF2\SlugifyViewHelper::__construct()
*/
protected function setUp()
{
$this->slugify = new Slugify();
$this->viewHelper = new SlugifyViewHelper($this->slugify);
}
/**
* @test
* @covers Cocur\Slugify\Bridge\ZF2\SlugifyViewHelper::__invoke()
*/
public function invoke()
{
$actual = 'Hällo Wörld';
$expected = call_user_func($this->viewHelper, $actual);
$this->assertEquals($expected, $this->slugify->slugify($actual));
$expected = call_user_func($this->viewHelper, $actual, '_');
$this->assertEquals($expected, $this->slugify->slugify($actual, '_'));
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests\RuleProvider;
use Cocur\Slugify\RuleProvider\FileRuleProvider;
use org\bovigo\vfs\vfsStream;
use PHPUnit_Framework_TestCase;
/**
* FileRuleProviderTest
*
* @package Cocur\Slugify\RuleProvider
* @author Florian Eckerstorfer
* @copyright 2015 Florian Eckerstorfer
* @group unit
*/
class FileRuleProviderTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Cocur\Slugify\RuleProvider\FileRuleProvider::__construct()
* @covers Cocur\Slugify\RuleProvider\FileRuleProvider::getRules()
*/
public function getRulesReturnsRulesReadFromJsonFile()
{
vfsStream::setup('fixtures', null, [
'german.json' => '{"ä": "a"}',
'austrian.json' => '{"ß": "sz"}',
]);
$provider = new FileRuleProvider(vfsStream::url('fixtures'));
$this->assertEquals(['ä' => 'a'], $provider->getRules('german'));
$this->assertEquals(['ß' => 'sz'], $provider->getRules('austrian'));
}
}

View File

@@ -0,0 +1,263 @@
<?php
/**
* This file is part of cocur/slugify.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cocur\Slugify\Tests;
use Cocur\Slugify\Slugify;
use Mockery;
/**
* SlugifyTest
*
* @category test
* @package org.cocur.slugify
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @author Ivo Bathke <ivo.bathke@gmail.com>
* @author Marchenko Alexandr
* @copyright 2012-2014 Florian Eckerstorfer
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Slugify
*/
private $slugify;
/**
* @var \Cocur\Slugify\RuleProvider\RuleProviderInterface|\Mockery\MockInterface
*/
private $provider;
protected function setUp()
{
$this->provider = Mockery::mock('\Cocur\Slugify\RuleProvider\RuleProviderInterface');
$this->provider->shouldReceive('getRules')->andReturn([]);
$this->slugify = new Slugify([], $this->provider);
}
/**
* @test
* @dataProvider defaultRuleProvider
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function slugifyReturnsSlugifiedStringUsingDefaultProvider($string, $result)
{
$slugify = new Slugify();
$this->assertEquals($result, $slugify->slugify($string));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::addRule()
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function addRuleAddsRule()
{
$this->assertInstanceOf(
'Cocur\Slugify\Slugify',
$this->slugify->addRule('X', 'y')
);
$this->assertEquals('y', $this->slugify->slugify('X'));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::addRules()
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function addRulesAddsMultipleRules()
{
$this->assertInstanceOf(
'Cocur\Slugify\Slugify',
$this->slugify->addRules(['x' => 'y', 'a' => 'b'])
);
$this->assertEquals('yb', $this->slugify->slugify('xa'));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::activateRuleset()
*/
public function activateRulesetActivatesTheGivenRuleset()
{
$provider = Mockery::mock('\Cocur\Slugify\RuleProvider\RuleProviderInterface');
$provider->shouldReceive('getRules')->with('esperanto')->once()->andReturn(['ĉ' => 'cx']);
$slugify = new Slugify(['rulesets' => []], $provider);
$this->assertInstanceOf(
'Cocur\Slugify\Slugify',
$slugify->activateRuleset('esperanto')
);
$this->assertEquals('sercxi', $slugify->slugify('serĉi'));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::create()
*/
public function createReturnsAnInstance()
{
$this->assertInstanceOf('Cocur\\Slugify\\SlugifyInterface', Slugify::create());
}
/**
* @test
* @covers Cocur\Slugify\Slugify::__construct()
*/
public function constructWithOtherRegexp()
{
$this->slugify = new Slugify(['regexp' => '/([^a-z0-9.]|-)+/']);
$this->assertEquals('file-name.tar.gz', $this->slugify->slugify('File Name.tar.gz'));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::__construct()
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function doNotConvertToLowercase()
{
$actual = 'File Name';
$expected = 'File-Name';
$this->slugify = new Slugify(['lowercase' => false]);
$this->assertEquals($expected, $this->slugify->slugify($actual));
}
/**
* @test
* @dataProvider customRulesProvider
*/
public function customRules($rule, $string, $result)
{
$slugify = new Slugify();
$slugify->activateRuleSet($rule);
$this->assertSame($result, $slugify->slugify($string));
}
public function customRulesProvider()
{
return [
['azerbaijani', 'əöüğşçı', 'eougsci'],
['azerbaijani', 'Fərhad Səfərov', 'ferhad-seferov'],
['croatian', 'Č Ć Ž Š Đ č ć ž š đ', 'c-c-z-s-dj-c-c-z-s-dj'],
['danish', 'Æ æ Ø ø Å å É é', 'ae-ae-oe-oe-aa-aa-e-e'],
['romanian', 'ă î â ş ș ţ ț Ă Î Â Ş Ș Ţ Ț', 'a-i-a-s-s-t-t-a-i-a-s-s-t-t'],
['serbian', 'А Б В Г Д Ђ Е Ж З И Ј К Л Љ М Н Њ О П Р С Т Ћ У Ф Х Ц Ч Џ Ш а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш Š Đ Ž Ć Č š đ ž ć č', 'a-b-v-g-d-dj-e-z-z-i-j-k-l-lj-m-n-nj-o-p-r-s-t-c-u-f-h-c-c-dz-s-a-b-v-g-d-dj-e-z-z-i-j-k-l-lj-m-n-nj-o-p-r-s-t-c-u-f-h-c-c-dz-s-s-dj-z-c-c-s-dj-z-c-c'],
['lithuanian', 'Ą Č Ę Ė Į Š Ų Ū Ž ą č ę ė į š ų ū ž', 'a-c-e-e-i-s-u-u-z-a-c-e-e-i-s-u-u-z'],
['estonian', 'Š Ž Õ Ä Ö Ü š ž õ ä ö ü', 's-z-o-a-o-u-s-z-o-a-o-u'],
];
}
/**
* @test
* @covers Cocur\Slugify\Slugify::__construct()
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function slugifyDefaultsToSeparatorOption()
{
$actual = 'file name';
$expected = 'file__name';
$this->slugify = new Slugify(['separator' => '__']);
$this->assertEquals($expected, $this->slugify->slugify($actual));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::__construct()
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function slugifyHonorsSeparatorArgument()
{
$actual = 'file name';
$expected = 'file__name';
$this->slugify = new Slugify(['separator' => 'dummy']);
$this->assertEquals($expected, $this->slugify->slugify($actual, '__'));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function slugifyOptionsArray()
{
$this->assertEquals('file-name', $this->slugify->slugify('file name'));
$this->assertEquals('file+name', $this->slugify->slugify('file name', ['separator' => '+']));
$this->assertEquals('name-1', $this->slugify->slugify('name(1)'));
$this->assertEquals('name(1)', $this->slugify->slugify('name(1)', ['regexp' => '/([^a-z0-9.()]|-)+/']));
$this->assertEquals('file-name', $this->slugify->slugify('FILE NAME'));
$this->assertEquals('FILE-NAME', $this->slugify->slugify('FILE NAME', ['lowercase' => false]));
}
/**
* @test
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function slugifyCustomRuleSet()
{
$slugify = new Slugify();
$this->assertSame('fur', $slugify->slugify('für', ['ruleset' => 'turkish']));
$this->assertSame('fuer', $slugify->slugify('für'));
}
public function defaultRuleProvider()
{
return [
[' a b ', 'a-b'],
['Hello', 'hello'],
['Hello World', 'hello-world'],
['Привет мир', 'privet-mir'],
['Привіт світ', 'privit-svit'],
['Hello: World', 'hello-world'],
['H+e#l1l--o/W§o r.l:d)', 'h-e-l1l-o-w-o-r-l-d'],
[': World', 'world'],
['Hello World!', 'hello-world'],
['Ä ä Ö ö Ü ü ß', 'ae-ae-oe-oe-ue-ue-ss'],
['Á À á à É È é è Ó Ò ó ò Ñ ñ Ú Ù ú ù', 'a-a-a-a-e-e-e-e-o-o-o-o-n-n-u-u-u-u'],
['Â â Ê ê Ô ô Û û', 'a-a-e-e-o-o-u-u'],
['Â â Ê ê Ô ô Û 1', 'a-a-e-e-o-o-u-1'],
['°¹²³⁴⁵⁶⁷⁸⁹@₀₁₂₃₄₅₆₇₈₉', '0123456789at0123456789'],
['Mórë thån wørds', 'more-thaan-woerds'],
['Блоґ їжачка', 'blog-jizhachka'],
['фильм', 'film'],
['драма', 'drama'],
['Ύπαρξη Αυτής η Σκουληκομυρμηγκότρυπα', 'iparxi-autis-i-skoulikomirmigkotripa'],
['Français Œuf où à', 'francais-oeuf-ou-a'],
['هذه هي اللغة العربية', 'hthh-hy-llgh-laarby'],
['مرحبا العالم', 'mrhb-laa-lm'],
['Één jaar', 'een-jaar'],
['tiếng việt rất khó', 'tieng-viet-rat-kho'],
['Nguyễn Đăng Khoa', 'nguyen-dang-khoa'],
['နှစ်သစ်ကူးတွင် သတ္တဝါတွေ စိတ်ချမ်းသာ ကိုယ်ကျန်းမာ၍ ကောင်းခြင်း အနန္တနှင့် ပြည့်စုံကြပါစေ', 'nhitthitkutwin-thttwatwe-seikkhyaantha-koekyaanmaywae-kaungkhyin-anntnhin-pyisonkypase'],
['Zażółć żółcią gęślą jaźń', 'zazolc-zolcia-gesla-jazn'],
['Mężny bądź chroń pułk twój i sześć flag', 'mezny-badz-chron-pulk-twoj-i-szesc-flag'],
['ერთი ორი სამი ოთხი ხუთი', 'erti-ori-sami-otkhi-khuti'],
['अ ऒ न द', 'a-oii-na-tha'],
['Æ Ø Å æ ø å', 'ae-oe-aa-ae-oe-aa'],
[str_repeat('Übergrößenträger', 1000), str_repeat('uebergroessentraeger', 1000)],
[str_repeat('my🎉', 5000), substr(str_repeat('my-', 5000), 0, -1)],
[str_repeat('hi🇦🇹', 5000), substr(str_repeat('hi-', 5000), 0, -1)],
['Č Ć Ž Š Đ č ć ž š đ', 'c-c-z-s-d-c-c-z-s-d'],
['Ą Č Ę Ė Į Š Ų Ū Ž ą č ę ė į š ų ū ž', 'a-c-e-e-i-s-u-u-z-a-c-e-e-i-s-u-u-z'],
];
}
}