Skip to content

Commit

Permalink
テストケース修正
Browse files Browse the repository at this point in the history
テスト用のテーブルクラス作成
cakephp/cakephp-codesniffer追加
bootstrap.phpは別のCakePHP4プラグインからコピー
  • Loading branch information
imo-tikuwa committed Sep 29, 2021
1 parent 77e972f commit ed0f11a
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
composer.lock
Vagrantfile
vendor
.phpunit.result.cache
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"cakephp/cakephp": "~4.0"
},
"require-dev": {
"phpunit/phpunit": "*"
"phpunit/phpunit": "*",
"cakephp/cakephp-codesniffer": "^4.5"
},
"autoload": {
"psr-4": {
Expand All @@ -26,5 +27,13 @@
"SoftDelete\\Test\\": "tests",
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
}
},
"scripts": {
"check": [
"@test",
"@cs-check"
],
"cs-check": "phpcs --colors -p src/ tests/",
"test": "phpunit --colors=always"
}
}
155 changes: 155 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0"?>
<ruleset name="CakePHP_Custom">
<description>CakePHP coding standard</description>

<exclude-pattern>\.git</exclude-pattern>

<rule ref="Zend.NamingConventions">
<exclude name="Zend.NamingConventions.ValidVariableName" />
</rule>

<rule ref="PSR2"/>

<!--
Property and method names with underscore prefix are allowed in CakePHP.
Not using underscore prefix is a recommendation of PSR2, not a requirement.
-->
<rule ref="PSR2.Classes.PropertyDeclaration.Underscore">
<severity>0</severity>
</rule>
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
<severity>0</severity>
</rule>
<rule ref="Squiz.NamingConventions.ValidFunctionName.PublicUnderscore">
<severity>0</severity>
</rule>
<rule ref="PEAR.NamingConventions.ValidFunctionName.PublicUnderscore">
<severity>0</severity>
</rule>
<rule ref="Zend.NamingConventions.ValidVariableName">
</rule>
<rule ref="Zend.NamingConventions.ValidVariableName.PrivateNoUnderscore">
<severity>0</severity>
</rule>
<rule ref="Zend.NamingConventions.ValidVariableName.ContainsNumbers">
<severity>0</severity>
</rule>
<rule ref="Zend.NamingConventions.ValidVariableName.StringVarContainsNumbers">
<severity>0</severity>
</rule>
<rule ref="Zend.NamingConventions.ValidVariableName.MemberVarContainsNumbers">
<severity>0</severity>
</rule>
<rule ref="Zend.NamingConventions.ValidVariableName.NotCamelCaps">
<severity>0</severity>
</rule>
<rule ref="Zend.NamingConventions.ValidVariableName.MemberVarNotCamelCaps">
<severity>0</severity>
</rule>

<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>

<!--
Temporarily ignore until API docblock formatting and line length issues in core code are fixed.
-->
<rule ref="CakePHP.Commenting.FunctionComment" />
<rule ref="CakePHP.Commenting.FunctionComment.ParamCommentNotCapital">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.ParamCommentFullStop">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.ThrowsNotCapital">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.ThrowsNoFullStop">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.EmptyThrows">
<severity>0</severity>
</rule>
<rule ref="Generic.Files.LineLength.TooLong">
<severity>0</severity>
</rule>

<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>

<rule ref="Squiz.Classes.LowercaseClassKeywords"/>

<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>

<rule ref="Squiz.Commenting.DocCommentAlignment"/>
<rule ref="Generic.Commenting.Todo"/>

<rule ref="PEAR.ControlStructures.ControlSignature"/>

<rule ref="Generic.Files.LineEndings"/>

<rule ref="Generic.Formatting.NoSpaceAfterCast"/>

<rule ref="Squiz.Operators.ValidLogicalOperators"/>

<rule ref="Generic.PHP.DeprecatedFunctions"/>
<rule ref="Squiz.PHP.DisallowSizeFunctionsInLoops"/>
<rule ref="Squiz.PHP.Eval"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Squiz.PHP.NonExecutableCode"/>

<rule ref="Generic.PHP.NoSilencedErrors"/>

<rule ref="Squiz.Scope.MemberVarScope"/>
<rule ref="Squiz.Scope.StaticThisUsage"/>

<rule ref="Squiz.WhiteSpace.CastSpacing"/>
<rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace"/>
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>

<!-- Relax some src/* and tests/* rules -->
<rule ref="PSR1.Files.SideEffects">
<exclude-pattern>*/config/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PSR1.Classes.ClassDeclaration">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PSR1.Methods.CamelCapsMethodName">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PEAR.NamingConventions.ValidClassName">
<exclude-pattern>*/CakePHP/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Classes.ValidClassName">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.NamingConventions.ValidFunctionName">
<exclude-pattern>*/src/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="Generic.NamingConventions.CamelCapsFunctionName">
<exclude-pattern>*/src/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
<rule ref="PEAR.NamingConventions.ValidFunctionName">
<exclude-pattern>*/src/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<!-- All rules in ./Sniffs are included automatically -->
</ruleset>
2 changes: 1 addition & 1 deletion src/Model/Table/SoftDeleteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getSoftDeleteField()
__(
'Configured field `{0}` is missing from the table `{1}`.',
$field,
$this->alias()
$this->getAlias()
)
);
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Table/PostsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace SoftDelete\Test\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
use SoftDelete\Model\Table\SoftDeleteTrait;

/**
* Posts Model
*/
class PostsTable extends Table
{
use SoftDeleteTrait;

/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
$this->belongsTo('Users');
$this->belongsToMany('Tags');
$this->addBehavior('CounterCache', ['Users' => ['posts_count']]);
}

/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->integer('user_id')
->notEmptyString('user_id');

return $validator;
}
}
49 changes: 49 additions & 0 deletions tests/Table/PostsTagsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace SoftDelete\Test\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
use SoftDelete\Model\Table\SoftDeleteTrait;

/**
* PostsTags Model
*/
class PostsTagsTable extends Table
{
use SoftDeleteTrait;

/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
$this->belongsTo('Tags');
$this->belongsTo('Posts');
}

/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->integer('post_id')
->notEmptyString('post_id');
$validator
->integer('tag_id')
->notEmptyString('tag_id');

return $validator;
}
}
56 changes: 56 additions & 0 deletions tests/Table/TagsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace SoftDelete\Test\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
use SoftDelete\Model\Table\SoftDeleteTrait;

/**
* Tags Model
*/
class TagsTable extends Table
{
use SoftDeleteTrait;

/**
* Soft deletion field
*/
public $softDeleteField = 'deleted_date';

/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
$this->belongsToMany('Posts', [
'through' => 'PostsTags',
'joinTable' => 'posts_tags',
'foreignKey' => 'tag_id',
'targetForeignKey' => 'post_id'
]);
$this->hasMany('PostsTags');
}

/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->requirePresence('name')
->allowEmptyString('name');

return $validator;
}
}
48 changes: 48 additions & 0 deletions tests/Table/UsersTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace SoftDelete\Test\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
use SoftDelete\Model\Table\SoftDeleteTrait;

/**
* Users Model
*/
class UsersTable extends Table
{
use SoftDeleteTrait;

/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
$this->hasMany('Posts', [
'dependent' => true,
'cascadeCallbacks' => true,
]);
}

/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->integer('posts_count')
->notEmptyString('posts_count');

return $validator;
}
}
Loading

0 comments on commit ed0f11a

Please sign in to comment.