Skip to content

Commit

Permalink
Merge branch 'craft5' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot authored Sep 30, 2024
2 parents 417e22a + 62ac020 commit b5a79bf
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 39 deletions.
2 changes: 1 addition & 1 deletion bin/create-default-fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

$volume = $app->volumes->getVolumeByHandle('local');
if (! $volume) {
$volume = new \craft\models\Volume();
$volume = new \craft\models\Volume;
$volume->name = 'Local';
$volume->handle = 'local';
$volume->fs = $fs;
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"src/helpers/Craft.php",
"src/helpers/Http.php",
"src/helpers/Model.php",
"src/helpers/Queue.php",
"src/helpers/Test.php"
]
},
Expand Down
4 changes: 2 additions & 2 deletions src/bin/generate-docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function parseClass(string $className)
function parseComment(string $comment)
{
preg_match_all('/@see\s+(.+)$/m', $comment, $sees);
foreach (($sees[1] ?? []) as $index => $otherClass) {
foreach ($sees[1] as $index => $otherClass) {
$comment = str_replace($sees[0][$index], 'SEE['.$otherClass.']', $comment);
}

Expand All @@ -71,7 +71,7 @@ function parseComment(string $comment)
$comment = preg_replace('/(^\s+|\s+$)/', '', $comment);

preg_match_all('/^SEE\[(.+)\]$/m', $comment, $sees);
foreach (($sees[1] ?? []) as $index => $otherClass) {
foreach ($sees[1] as $index => $otherClass) {
$comment = str_replace($sees[0][$index], "\n\n".implode("\n\n", parseClass($otherClass))."\n\n", $comment);
}

Expand Down
3 changes: 1 addition & 2 deletions src/console/TestableResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public function __construct(
protected int $exitCode,
protected string $stdout,
protected string $stderr,
) {
}
) {}

public function assertSuccesful()
{
Expand Down
4 changes: 1 addition & 3 deletions src/events/RollbackTransactionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use yii\base\Event;

class RollbackTransactionEvent extends Event
{
}
class RollbackTransactionEvent extends Event {}
4 changes: 1 addition & 3 deletions src/exceptions/AutoCommittingFieldsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace markhuot\craftpest\exceptions;

class AutoCommittingFieldsException extends \Exception
{
}
class AutoCommittingFieldsException extends \Exception {}
2 changes: 1 addition & 1 deletion src/factories/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function source(string $source)

public function newElement()
{
return new \craft\elements\Asset();
return new \craft\elements\Asset;
}

public function definition(int $index = 0)
Expand Down
2 changes: 1 addition & 1 deletion src/factories/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function inferences(array $definition = [])

public function newElement()
{
return new MatrixBlockType();
return new MatrixBlockType;
}

public function store($blockType)
Expand Down
2 changes: 1 addition & 1 deletion src/factories/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function group($handle)

public function newElement()
{
return new \craft\elements\Category();
return new \craft\elements\Category;
}

public function definition(int $index = 0)
Expand Down
2 changes: 1 addition & 1 deletion src/factories/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function inferTypeId(?int $sectionid): ?int
*/
public function newElement()
{
return new \craft\elements\Entry();
return new \craft\elements\Entry;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/factories/EntryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function inferences(array $definition = [])

public function newElement()
{
return new EntryTypeModel();
return new EntryTypeModel;
}

public function store($entryType)
Expand Down
2 changes: 1 addition & 1 deletion src/factories/Fieldable.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function storeFields(FieldLayout $fieldLayout, $context = null)
->toArray();

if (empty($fieldLayout->getTabs()[0])) {
$fieldLayoutTab = new FieldLayoutTab();
$fieldLayoutTab = new FieldLayoutTab;
$fieldLayoutTab->name = 'Content';
$fieldLayoutTab->sortOrder = 1;
$fieldLayout->setTabs([$fieldLayoutTab]);
Expand Down
4 changes: 2 additions & 2 deletions src/factories/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function template(string $template)
*/
public function newElement()
{
return new \craft\models\Section();
return new \craft\models\Section;
}

/**
Expand Down Expand Up @@ -94,7 +94,7 @@ public function inferences(array $definition = [])
$handle = $definition['handle'];
$definition['siteSettings'] = collect(Craft::$app->sites->getAllSites())
->mapWithkeys(function ($site) use ($name, $handle) {
$settings = new Section_SiteSettings();
$settings = new Section_SiteSettings;
$settings->siteId = $site->id;
$settings->hasUrls = $this->hasUrls;
$settings->uriFormat = $this->uriFormat;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Craft.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function createVolume()
function volumeDefinition(array $definition = [])
{
if (version_greater_than_or_equal_to(\Craft::$app->version, '4')) {
$fileSystem = new \craft\fs\Local(); // @phpstan-ignore-line
$fileSystem = new \craft\fs\Local; // @phpstan-ignore-line
$fileSystem->name = $definition['name'].' FS'; // @phpstan-ignore-line
$fileSystem->handle = $definition['handle'].'Fs'; // @phpstan-ignore-line
$fileSystem->path = \Craft::getAlias('@storage').'/volumes/'.$definition['handle'].'/'; // @phpstan-ignore-line
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace markhuot\craftpest\helpers\queue;

use Craft;

if (! function_exists('queue')) {
function queue($callback = null): mixed
{
$result = null;

if ($callback) {
$result = $callback();
}

Craft::$app->queue->run();

return $result;
}
}
4 changes: 1 addition & 3 deletions src/http/requests/GetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace markhuot\craftpest\http\requests;

class GetRequest extends WebRequest
{
}
class GetRequest extends WebRequest {}
4 changes: 1 addition & 3 deletions src/http/requests/PostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace markhuot\craftpest\http\requests;

class PostRequest extends WebRequest
{
}
class PostRequest extends WebRequest {}
4 changes: 1 addition & 3 deletions src/overrides/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace markhuot\craftpest\overrides;

class Config extends \craft\services\Config
{
}
class Config extends \craft\services\Config {}
4 changes: 1 addition & 3 deletions src/overrides/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace markhuot\craftpest\overrides;

class ProjectConfig extends \craft\services\Config
{
}
class ProjectConfig extends \craft\services\Config {}
4 changes: 1 addition & 3 deletions src/pest/PhpReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace markhuot\craftpest\pest;

class PhpReporter extends Reporter
{
}
class PhpReporter extends Reporter {}
33 changes: 33 additions & 0 deletions src/test/CleanupRequestState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace markhuot\craftpest\test;

use Craft;
use markhuot\craftpest\web\Application;

trait CleanupRequestState
{
/**
* Cleanup any request state that may be left over from previous tests.
*
* This is normally cleaned up (and run) after a request goes through Craft's router.
* However, if a test isn't interacting with the route and is just calling actions or
* running queues then the after request callbacks won't be cleared.
*/
public function tearDownCleanupRequestState(): void
{
$app = Craft::$app;

if ($app instanceof Application) {
$reflect = new \ReflectionClass($app);
while ($reflect && ! $reflect->hasProperty('afterRequestCallbacks')) {
$reflect = $reflect->getParentClass();
}

if ($reflect) {
$property = $reflect->getProperty('afterRequestCallbacks');
$property->setValue($app, []);
}
}
}
}
2 changes: 1 addition & 1 deletion src/test/DatabaseAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait DatabaseAssertions
*/
public function assertDatabaseCount(string $tableName, int $expectedCount)
{
$actualCount = (new Query())->from($tableName)->count();
$actualCount = (new Query)->from($tableName)->count();

$this->assertEquals($expectedCount, $actualCount);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/RefreshesDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function rollBackTransaction()

$this->transaction->rollBack();

$event = new RollbackTransactionEvent();
$event = new RollbackTransactionEvent;
$event->sender = $this;
Event::trigger(RefreshesDatabase::class, 'EVENT_ROLLBACK_TRANSACTION', $event);

Expand Down
1 change: 1 addition & 0 deletions src/test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
{
use ActingAs,
Benchmark,
CleanupRequestState,
CookieState,
DatabaseAssertions,
Dd,
Expand Down
3 changes: 1 addition & 2 deletions src/web/BenchmarkResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class BenchmarkResult
public function __construct(
public int $startProfileAt,
public int $endProfileAt,
) {
}
) {}

// function summary()
// {
Expand Down
17 changes: 17 additions & 0 deletions tests/RequestStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

test('sets request state', function () {
Craft::$app->onAfterRequest(fn () => throw new Exception('An after request callback was not cleared.'));

expect(true)->toBeTrue();
});

test('expects request state to be empty', function () {
$app = Craft::$app;
$reflect = new ReflectionClass($app);
while ($reflect && ! $reflect->hasProperty('afterRequestCallbacks')) {
$reflect = $reflect->getParentClass();
}

expect($reflect->getProperty('afterRequestCallbacks')->getValue($app))->toBeEmpty();
})->depends('sets request state');

0 comments on commit b5a79bf

Please sign in to comment.