Skip to content

Commit

Permalink
test: Added
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Aug 16, 2024
1 parent b543489 commit 2ece998
Show file tree
Hide file tree
Showing 20 changed files with 467 additions and 57 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# .github/workflows/lint.yml

on: [push]
name: Check & fix styling
jobs:
php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.dist.php --allow-risky=yes

- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
push_options: '--force'
branch: ${{ steps.extract_branch.outputs.branch }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38 changes: 38 additions & 0 deletions .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# github action that checks code with Rector
name: Rector

on: [push]

jobs:
rector:
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@v3
with:
# Must be used to trigger workflow after push
token: ${{ secrets.ACCESS_TOKEN }}

-
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none

- uses: "ramsey/composer-install@v2"

- run: vendor/bin/rector process --clear-cache

- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
-
# commit only to core contributors who have repository access
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: '[rector] Rector fixes'
push_options: '--force'
branch: ${{ steps.extract_branch.outputs.branch }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50 changes: 50 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: tests

on:
push:
pull_request:
types:
- opened
- ready_for_review
- reopened
- synchronize
schedule:
- cron: '0 0 * * *'

jobs:
moonshine-tests:
if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [8.2, 8.3]
laravel: [11]

steps:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- uses: actions/checkout@v4

- name: Set up MySQL
env:
DB_USERNAME: root
DB_PASSWORD: root
DB_DATABASE: tests
run: |
sudo /etc/init.d/mysql start
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USERNAME }} -p${{ env.DB_PASSWORD }}
- name: Copy phpunit.xml.dist
run: php -r "file_exists('phpunit.xml.dist') || copy('phpunit-example.xml.dist', 'phpunit.xml.dist');"

- name: Install Dependencies
run: composer install --no-interaction --prefer-dist --optimize-autoloader

- name: Execute tests via PHPUnit
env:
DB_PASSWORD: root
run: ./vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ vendor
composer.lock
node_modules
.phpunit.result.cache
phpunit.xml.dist
.php-cs-fixer.cache
app
reports
3 changes: 3 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
'concat_space' => [
'spacing' => 'one'
]
])
->setFinder($finder);
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5.8",
"mockery/mockery": "^1.4.4",
"phpstan/phpstan": "^1.4.7",
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0",
"brianium/paratest": "^6.8"
"phpunit/phpunit": "^11.0",
"orchestra/testbench": "^9.0",
"rector/rector": "^1.0",
"moonshine/moonshine": "^2.20"
},
"autoload": {
"psr-4": {
Expand Down
27 changes: 27 additions & 0 deletions phpunit-example.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" backupGlobals="false"
beStrictAboutTestsThatDoNotTestAnything="false" colors="true" processIsolation="false" stopOnError="false"
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd"
cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<ini name="date.timezone" value="UTC"/>
<ini name="intl.default_locale" value="C.UTF-8"/>
<ini name="memory_limit" value="2048M"/>
<env name="DB_CONNECTION" value="testing"/>
<!--
<env name="REDIS_HOST" value="127.0.0.1" />
<env name="REDIS_PORT" value="6379" />
-->
</php>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
36 changes: 13 additions & 23 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false"
beStrictAboutTestsThatDoNotTestAnything="false" colors="true" processIsolation="false" stopOnError="false"
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd"
cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>

<ini name="date.timezone" value="UTC" />
<ini name="intl.default_locale" value="C.UTF-8" />
<ini name="memory_limit" value="2048M" />
<env name="DB_CONNECTION" value="testing" />
<ini name="date.timezone" value="UTC"/>
<ini name="intl.default_locale" value="C.UTF-8"/>
<ini name="memory_limit" value="2048M"/>
<env name="DB_CONNECTION" value="testing"/>
<!--
<env name="REDIS_HOST" value="127.0.0.1" />
<env name="REDIS_PORT" value="6379" />
-->
<env name="REDIS_HOST" value="127.0.0.1" />
<env name="REDIS_PORT" value="6379" />
-->
</php>
<coverage>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>
37 changes: 37 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
]);

$rectorConfig->skip([
__DIR__ . '/app',
__DIR__ . '/public',
__DIR__ . '/resources',
__DIR__ . '/vendor',
__DIR__ . '/stubs',
__DIR__ . '/tests',
ExplicitBoolCompareRector::class,
FirstClassCallableRector::class,
]);

$rectorConfig->importNames();
$rectorConfig->importShortClasses();
$rectorConfig->removeUnusedImports();

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_82,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::TYPE_DECLARATION,
]);
};
15 changes: 6 additions & 9 deletions src/Casts/LayoutsCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use JsonException;
use MoonShine\Layouts\Collections\LayoutItemCollection;
use Throwable;

Expand All @@ -15,7 +14,7 @@ class LayoutsCast implements CastsAttributes
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
public function get(Model $model, string $key, mixed $value, array $attributes): LayoutItemCollection
{
if($value instanceof LayoutItemCollection) {
return $value;
Expand Down Expand Up @@ -47,13 +46,11 @@ private function _map(mixed $value): LayoutItemCollection
$value = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
}

$values = collect($value)->map(function (array $data) {
return new LayoutItem(
$data['name'],
$data['key'] ?? 0,
$data['values'] ?? [],
);
})->filter();
$values = collect($value)->map(fn(array $data): LayoutItem => new LayoutItem(
$data['name'],
$data['key'] ?? 0,
$data['values'] ?? [],
))->filter();
} catch (Throwable) {}

return LayoutItemCollection::make($values);
Expand Down
4 changes: 2 additions & 2 deletions src/Collections/LayoutCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ final class LayoutCollection extends Collection
{
public function findByKey(int $key, ?LayoutContract $default = null): ?LayoutContract
{
return $this->first(fn(LayoutContract $layout) => $layout->key() === $key, $default);
return $this->first(fn(LayoutContract $layout): bool => $layout->key() === $key, $default);
}

public function findByName(string $name, ?LayoutContract $default = null): ?LayoutContract
{
return $this->first(fn(LayoutContract $layout) => $layout->name() === $name, $default);
return $this->first(fn(LayoutContract $layout): bool => $layout->name() === $name, $default);
}
}
8 changes: 8 additions & 0 deletions src/Collections/LayoutItemCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@
*/
final class LayoutItemCollection extends Collection
{
public function findByName(string $value): ?LayoutItem
{
return $this->firstWhere(fn(LayoutItem $item): bool => $item->getName() === $value);
}

public function findByKey(int $value): ?LayoutItem
{
return $this->firstWhere(fn(LayoutItem $item): bool => $item->getKey() === $value);
}
}
12 changes: 6 additions & 6 deletions src/Fields/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public function headingFields(): Fields
{
return Fields::make([
Flex::make(array_filter([
! $this->disableSort ? Preview::make(
formatted: static fn () => Icon::make('heroicons.outline.bars-4')
)
->withoutWrapper()
->customAttributes(['class' => 'handle', 'style' => 'cursor: move']) : null,
$this->disableSort ? null : Preview::make(
formatted: static fn () => Icon::make('heroicons.outline.bars-4')
)
->withoutWrapper()
->customAttributes(['class' => 'handle', 'style' => 'cursor: move']),

Position::make()
->withoutWrapper()
Expand Down Expand Up @@ -149,7 +149,7 @@ public function fields(): Fields

if ($this->isForcePreview) {
$this->fields->onlyFields()
->map(fn (Field $f) => $f->forcePreview());
->map(fn (Field $f): Field => $f->forcePreview());
}


Expand Down
Loading

0 comments on commit 2ece998

Please sign in to comment.