Skip to content

Commit

Permalink
feature: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krissss committed May 19, 2023
1 parent 50429f4 commit 830a29b
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 12 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[{*.yml,*.json}]
indent_size = 2
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.editorconfig export-ignore
/.gitignore export-ignore
/.gitattributes export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.github export-ignore
43 changes: 43 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: phpunit

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php: [7.3, 7.4, 8.0, 8.1, 8.2]
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
#extensions: 'redis'

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: php${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
restore-keys: php${{ matrix.php }}-composer-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run phpunit
run: composer test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ build
vendor
.idea
.vscode
.phpunit*
composer.lock
/phpunit.xml
.phpunit.cache
27 changes: 18 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
"type": "library",
"license": "MIT",
"description": "Webman plugin webman-tech/laravel-validation",
"require": {
"php": ">=7.3",
"illuminate/validation": ">=8.0"
},
"require-dev": {
"illuminate/database": ">=8.0",
"phpunit/phpunit": "^9",
"symfony/translation": ">=5.0",
"webman-tech/laravel-translation": "^1.0",
"workerman/webman-framework": "^1.4"
},
"autoload": {
"psr-4": {
"WebmanTech\\LaravelValidation\\": "src"
Expand All @@ -11,17 +22,15 @@
"src/helper.php"
]
},
"require": {
"php": ">=7.2",
"illuminate/validation": "^7.0|^8.0|^9.0"
},
"require-dev": {
"illuminate/database": "^7.30",
"symfony/translation": "^5.4",
"webman-tech/laravel-translation": "^1.0",
"workerman/webman-framework": "^1.4"
"autoload-dev": {
"psr-4": {
"WebmanTech\\LaravelValidation\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"test": "phpunit --testdox --no-interaction"
}
}
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
4 changes: 2 additions & 2 deletions src/Facades/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected static function createFactory(): FactoryContract

protected static function createDatabasePresenceVerifier(): ?DatabasePresenceVerifierInterface
{
if (class_exists('Illuminate\Database\Capsule\Manager')) {
if (class_exists('Illuminate\Database\Capsule\Manager') && LaravelDb::getManagerInstance()) {
return new DatabasePresenceVerifier(LaravelDb::getManagerInstance()->getDatabaseManager());
}
return null;
Expand Down Expand Up @@ -92,4 +92,4 @@ public static function __callStatic($name, $arguments)
{
return static::instance()->{$name}(... $arguments);
}
}
}
3 changes: 3 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/support/helpers.php
/runtime
/resource
75 changes: 75 additions & 0 deletions tests/Facades/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace WebmanTech\LaravelValidation\Tests\Facades;

use Illuminate\Contracts\Validation\Factory as FactoryContract;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Illuminate\Validation\ValidationException;
use PHPUnit\Framework\TestCase;
use Throwable;
use WebmanTech\LaravelValidation\Facades\Validator;

/**
* https://laravel.com/docs/10.x/validation
*/
class ValidatorTest extends TestCase
{
public function testInstance()
{
$this->assertInstanceOf(FactoryContract::class, Validator::instance());
$this->assertInstanceOf(FactoryContract::class, validator());
$this->assertInstanceOf(ValidatorContract::class, validator(['title' => '123']));
}

public function testValidate()
{
$data1 = [
'email' => '123',
];
$okRules = [
'email' => 'required|string',
];
$errorRules = [
'email' => 'required|email',
];
// validate ok
$data = Validator::validate($data1, $okRules);
$this->assertEquals($data1, $data);
// validate error
try {
Validator::validate($data1, $errorRules);
} catch (Throwable $e) {
$this->assertInstanceOf(ValidationException::class, $e);
}
// manual validate
// ok
$validator = Validator::make($data, $okRules);
$this->assertFalse($validator->fails());
$this->assertEquals($data, $validator->validated());
// fails
$validator = Validator::make($data, $errorRules);
$this->assertTrue($validator->fails());
$this->assertEquals(1, $validator->errors()->count());
}

public function testMessages()
{
try {
Validator::validate([
'title' => ''
], [
'title' => 'required'
], [
'title.required' => '标题不能为空'
]);
} catch (ValidationException $e) {
$this->assertEquals('标题不能为空', $e->errors()['title'][0]);
}
}

public function testRules()
{
$this->assertTrue(true);
// https://laravel.com/docs/10.x/validation#available-validation-rules
}
}
18 changes: 18 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

if (!file_exists(__DIR__ . '/support/helpers.php')) {
mkdir(__DIR__ . '/support');
copy(__DIR__ . '/../vendor/workerman/webman-framework/src/support/helpers.php', __DIR__ . '/support/helpers.php');
}
require_once __DIR__ . '/support/helpers.php';

if (
!file_exists(__DIR__ . '/config/plugin/webman-tech/laravel-validation')
|| !file_exists(__DIR__ . '/resource')
) {
\WebmanTech\LaravelValidation\Install::install();
}

require_once __DIR__ . '/../vendor/workerman/webman-framework/src/support/bootstrap.php';
5 changes: 5 additions & 0 deletions tests/config/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'runtime_path' => base_path(false) . DIRECTORY_SEPARATOR . 'runtime',
];
3 changes: 3 additions & 0 deletions tests/config/container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return new \Webman\Container();
20 changes: 20 additions & 0 deletions tests/config/plugin/webman-tech/laravel-validation/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;

return [
'enable' => true,
/**
* translation 实例
*/
'translation' => function (): ?Translator {
return null;
},
/**
* 扩展自定义验证规则
*/
'extends' => function (ValidationFactory $validator): void {
//$validator->extend();
}
];
25 changes: 25 additions & 0 deletions tests/config/translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

/**
* Multilingual configuration
*/
return [
// Default language
'locale' => 'zh_CN',
// Fallback language
'fallback_locale' => ['zh_CN', 'en'],
// Folder where language files are stored
'path' => base_path() . '/resource/translations',
];

0 comments on commit 830a29b

Please sign in to comment.