Skip to content

Commit

Permalink
Merge f40ec0f into b523176
Browse files Browse the repository at this point in the history
  • Loading branch information
Xety committed Dec 27, 2015
2 parents b523176 + f40ec0f commit 5a5b6f7
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 251 deletions.
12 changes: 5 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0

env:
matrix:
Expand All @@ -13,7 +14,7 @@ env:

matrix:
allow_failures:
- php: 5.4
- php: 7.0
env: COVERALLS=1 DEFAULT=0

fast_finish: true
Expand All @@ -25,10 +26,10 @@ matrix:
- php: 5.4
env: DB=sqlite db_class='Cake\Database\Driver\Sqlite' db_dsn='sqlite::memory:'

- php: 5.4
- php: 7.0
env: PHPCS=1 DEFAULT=0

- php: 5.4
- php: 7.0
env: COVERALLS=1 DEFAULT=0

before_script:
Expand All @@ -39,9 +40,6 @@ before_script:

- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi"

- sh -c "if [ '$PHPCS' = '1' ]; then pear channel-discover pear.cakephp.org; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then pear install --alldeps cakephp/CakePHP_CodeSniffer; fi"

- sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi"

Expand All @@ -53,7 +51,7 @@ script:
- sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi"
- sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then phpcs -p --extensions=php --standard=CakePHP --ignore=vendor --ignore=docs . ; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi"

notifications:
email: false
69 changes: 35 additions & 34 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
{
"name": "xety/cake3-sluggable",
"type": "cakephp-plugin",
"description": "A simple Cake3 plugin to slug fields and find records by slug.",
"keywords": ["cakephp", "cake3", "plugin", "slug", "sluggable", "fields", "records"],
"homepage": "https://github.com/Xety/Cake3-Sluggable",
"license": "MIT",
"support":{
"source":"https://github.com/Xety/Cake3-Sluggable",
"issues":"https://github.com/Xety/Cake3-Sluggable/issues"
},
"authors": [
{
"name": "Xety",
"email": "zoro.fmt@gmail.com",
"homepage": "https://github.com/Xety"
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"cakephp/cakephp": "3.0.*-dev",
"phpunit/phpunit": "4.1.*"
},
"autoload": {
"psr-4": {
"Xety\\Cake3Sluggable\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Xety\\Cake3Sluggable\\Test\\": "tests"
}
}
"name": "xety/cake3-sluggable",
"type": "cakephp-plugin",
"description": "A simple Cake3 plugin to slug fields and find records by slug.",
"keywords": ["cakephp", "cake3", "plugin", "slug", "sluggable", "fields", "records"],
"homepage": "https://github.com/Xety/Cake3-Sluggable",
"license": "MIT",
"support":{
"source":"https://github.com/Xety/Cake3-Sluggable",
"issues":"https://github.com/Xety/Cake3-Sluggable/issues"
},
"authors": [
{
"name": "Xety",
"email": "zoro.fmt@gmail.com",
"homepage": "https://github.com/Xety"
}
],
"require": {
"php": ">=5.4.16",
"cakephp/cakephp": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"cakephp/cakephp-codesniffer": "master-dev"
},
"autoload": {
"psr-4": {
"Xety\\Cake3Sluggable\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Xety\\Cake3Sluggable\\Test\\": "tests"
}
}

}
60 changes: 34 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?><phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Cake3Sluggable Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>
<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Cake3Sluggable Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>
<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<!-- Prevent coverage reports from looking in tests, vendors, config folders -->
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<directory suffix=".ctp">./src/</directory>
</whitelist>
</filter>

</phpunit>
98 changes: 51 additions & 47 deletions src/Model/Behavior/SluggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,59 @@
use Cake\ORM\Query;
use Cake\Utility\Inflector;

class SluggableBehavior extends Behavior {
class SluggableBehavior extends Behavior
{

/**
* Default config.
*
* @var array
*/
protected $_defaultConfig = [
'field' => 'title',
'slug' => 'slug',
'replacement' => '-',
];
/**
* Default config.
*
* @var array
*/
protected $_defaultConfig = [
'field' => 'title',
'slug' => 'slug',
'replacement' => '-',
];

/**
* Slug a field passed in the default config with its replacement.
*
* @param \Cake\ORM\Entity $entity The entity that is going to be updated.
*
* @return void
*/
public function slug(Entity $entity) {
$config = $this->config();
$value = $entity->get($config['field']);
$entity->set($config['slug'], strtolower(Inflector::slug($value, $config['replacement'])));
}
/**
* Slug a field passed in the default config with its replacement.
*
* @param \Cake\ORM\Entity $entity The entity that is going to be updated.
*
* @return void
*/
public function slug(Entity $entity)
{
$config = $this->config();
$value = $entity->get($config['field']);
$entity->set($config['slug'], strtolower(Inflector::slug($value, $config['replacement'])));
}

/**
* BeforeSave handle.
*
* @param \Cake\Event\Event $event The beforeSave event that was fired.
* @param \Cake\ORM\Entity $entity The entity that is going to be saved.
*
* @return void
*/
public function beforeSave(Event $event, Entity $entity) {
$this->slug($entity);
}
/**
* BeforeSave handle.
*
* @param \Cake\Event\Event $event The beforeSave event that was fired.
* @param \Cake\ORM\Entity $entity The entity that is going to be saved.
*
* @return void
*/
public function beforeSave(Event $event, Entity $entity)
{
$this->slug($entity);
}

/**
* Custom finder by slug.
*
* @param \Cake\ORM\Query $query The query finder.
* @param array $options The options passed in the query builder.
*
* @return \Cake\ORM\Query
*/
public function findSlug(Query $query, array $options) {
return $query->where([
$options['slugField'] => $options['slug']
]);
}
/**
* Custom finder by slug.
*
* @param \Cake\ORM\Query $query The query finder.
* @param array $options The options passed in the query builder.
*
* @return \Cake\ORM\Query
*/
public function findSlug(Query $query, array $options)
{
return $query->where([
$options['slugField'] => $options['slug']
]);
}
}
67 changes: 34 additions & 33 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,40 @@

use Cake\TestSuite\Fixture\TestFixture;

class UsersFixture extends TestFixture {
class UsersFixture extends TestFixture
{

/**
* Fields
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'username' => ['type' => 'string', 'length' => 20],
'slug' => ['type' => 'string', 'length' => 20],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
'username' => ['type' => 'unique', 'columns' => ['username']]
],
'_options' => [
'engine' => 'InnoDB', 'collation' => 'utf8_general_ci'
],
];
/**
* Fields
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'username' => ['type' => 'string', 'length' => 20],
'slug' => ['type' => 'string', 'length' => 20],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
'username' => ['type' => 'unique', 'columns' => ['username']]
],
'_options' => [
'engine' => 'InnoDB', 'collation' => 'utf8_general_ci'
],
];

/**
* Records
*
* @var array
*/
public $records = [
[
'username' => 'Mariano',
'slug' => 'mariano'
],
[
'username' => 'Larry',
'slug' => 'larry'
]
];
/**
* Records
*
* @var array
*/
public $records = [
[
'username' => 'Mariano',
'slug' => 'mariano'
],
[
'username' => 'Larry',
'slug' => 'larry'
]
];
}
Loading

0 comments on commit 5a5b6f7

Please sign in to comment.