Skip to content

Commit

Permalink
Fix namespace sorting and tests (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
chesn0k authored Oct 18, 2024
1 parent 27b41bb commit 8c9107d
Show file tree
Hide file tree
Showing 36 changed files with 63 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/Twig/TwigEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function tokenize(Source $source): TokenStream {
public static function sortNamespaces(string $input): string {
$lines = \explode(\PHP_EOL, $input);
$lines = \array_unique($lines);
\sort($lines, \SORT_FLAG_CASE | \SORT_NATURAL);
\usort($lines, 'strcmp');
return \trim(\implode(\PHP_EOL, $lines)) . \PHP_EOL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Twig/TwigSortSetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function compile(Compiler $compiler): void {
->raw(";\n")
->write('$data = explode("\n", $data);' . "\n")
->write('$data = array_unique($data);' . "\n")
->write('sort($data, SORT_FLAG_CASE|SORT_NATURAL);' . "\n")
->write('usort($data, "strcmp");' . "\n")
->write('yield ltrim(implode("\n", $data)) . "\n";' . "\n");
}

Expand Down
4 changes: 2 additions & 2 deletions src/Twig/TwigSortTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function parse(Token $token): Node {
$ref->setAttribute('always_defined', TRUE);

return new Node([
new SetNode(TRUE, $ref, $body, $lineno, $this->getTag()),
new TwigSortSetNode(['ref' => $ref], [], $lineno, $this->getTag()),
new SetNode(TRUE, $ref, $body, $lineno),
new TwigSortSetNode(['ref' => $ref], [], $lineno),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion templates/Plugin/Migrate/_destination/destination.twig
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class {{ class }} extends DestinationBase {% if services %}implements Cont
/**
* {@inheritdoc}
*/
public function fields(MigrationInterface $migration = NULL): array {
public function fields(?MigrationInterface $migration = NULL): array {
return [
'id' => $this->t('The row ID.'),
// @todo Describe row fields here.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/_action/action.twig
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class {{ class }} extends {{ configurable ? 'ConfigurableActionBase' : 'Ac
/**
* {@inheritdoc}
*/
public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
public function access($entity, ?AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$access = $entity->access('update', $account, TRUE)
->andIf($entity->get('field_example')->access('edit', $account, TRUE));
Expand All @@ -117,7 +117,7 @@ final class {{ class }} extends {{ configurable ? 'ConfigurableActionBase' : 'Ac
/**
* {@inheritdoc}
*/
public function execute(ContentEntityInterface $entity = NULL): void {
public function execute(?ContentEntityInterface $entity = NULL): void {
$entity->set('field_example', 'New value')->save();
}

Expand Down
2 changes: 1 addition & 1 deletion templates/Service/_path-processor/path-processor.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class {{ class }} implements InboundPathProcessorInterface, OutboundPathPr
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL): string {
public function processOutbound($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL): string {
return preg_replace('#^/node/#i', '/content/', $path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Drupal\example\Plugin\migrate\destination;

use Drupal\migrate\Attribute\MigrateDestination;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Row;

/**
Expand All @@ -30,7 +30,7 @@ public function getIds(): array {
/**
* {@inheritdoc}
*/
public function fields(MigrationInterface $migration = NULL): array {
public function fields(?MigrationInterface $migration = NULL): array {
return [
'id' => $this->t('The row ID.'),
// @todo Describe row fields here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Attribute\MigrateDestination;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -65,7 +65,7 @@ public function getIds(): array {
/**
* {@inheritdoc}
*/
public function fields(MigrationInterface $migration = NULL): array {
public function fields(?MigrationInterface $migration = NULL): array {
return [
'id' => $this->t('The row ID.'),
// @todo Describe row fields here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
/**
* {@inheritdoc}
*/
public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
public function access($entity, ?AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$access = $entity->access('update', $account, TRUE)
->andIf($entity->get('field_example')->access('edit', $account, TRUE));
Expand All @@ -76,7 +76,7 @@ public function access($entity, AccountInterface $account = NULL, $return_as_obj
/**
* {@inheritdoc}
*/
public function execute(ContentEntityInterface $entity = NULL): void {
public function execute(?ContentEntityInterface $entity = NULL): void {
$entity->set('field_example', 'New value')->save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* {@inheritdoc}
*/
public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
public function access($entity, ?AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$access = $entity->access('update', $account, TRUE)
->andIf($entity->get('field_example')->access('edit', $account, TRUE));
Expand All @@ -76,7 +76,7 @@ public function access($entity, AccountInterface $account = NULL, $return_as_obj
/**
* {@inheritdoc}
*/
public function execute(ContentEntityInterface $entity = NULL): void {
public function execute(?ContentEntityInterface $entity = NULL): void {
$entity->set('field_example', 'New value')->save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(
* {@inheritdoc}
*/
public function log($level, string|\Stringable $message, array $context = []): void {
$message = (string) $message;
// Convert PSR3-style messages to \Drupal\Component\Render\FormattableMarkup
// style, so they can be translated too.
$placeholders = $this->parser->parseMessagePlaceholders($message, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
* {@inheritdoc}
*/
public function log($level, string|\Stringable $message, array $context = []): void {
$message = (string) $message;
// Convert PSR3-style messages to \Drupal\Component\Render\FormattableMarkup
// style, so they can be translated too.
$placeholders = $this->parser->parseMessagePlaceholders($message, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function processInbound($path, Request $request): string {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL): string {
public function processOutbound($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL): string {
return preg_replace('#^/node/#i', '/content/', $path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function processInbound($path, Request $request): string {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL): string {
public function processOutbound($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL): string {
return preg_replace('#^/node/#i', '/content/', $path);
}

Expand Down
22 changes: 19 additions & 3 deletions tests/functional/Generator/_hook/example.module
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,29 @@ declare(strict_types=1);
function example_theme($existing, $type, $theme, $path) {
return [
'my_module_display' => [
'variables' => ['my_modules' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'my_module_per_page' => NULL],
'variables' => [
'my_modules' => NULL,
'topics' => NULL,
'parents' => NULL,
'tid' => NULL,
'sortby' => NULL,
'my_module_per_page' => NULL,
],
],
'my_module_list' => [
'variables' => ['my_modules' => NULL, 'parents' => NULL, 'tid' => NULL],
'variables' => [
'my_modules' => NULL,
'parents' => NULL,
'tid' => NULL,
],
],
'my_module_icon' => [
'variables' => ['new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0],
'variables' => [
'new_posts' => NULL,
'num_posts' => 0,
'comment_mode' => 0,
'sticky' => 0,
],
],
'status_report' => [
'render element' => 'requirements',
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/acme/tests/src/Kernel/BundleClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Drupal\Tests\acme\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\acme\Entity\User\UserBase;
use Drupal\acme\Entity\User\UserBundle;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/bar/tests/src/Functional/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\bar\Functional;

use Drupal\dcg_test\TestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/bar/tests/src/Functional/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Drupal\Component\Render\FormattableMarkup as FM;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\dcg_test\TestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\dcg_test\TestTrait;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/bar/tests/src/Functional/InstallFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\bar\Functional;

use Drupal\dcg_test\TestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/bar/tests/src/Kernel/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Drupal\Tests\bar\Kernel;

use Drupal\bar\BarServiceProvider;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\bar\BarServiceProvider;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/figma/tests/src/Functional/ContentEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Drupal\Tests\figma\Functional;

use Drupal\Component\Render\FormattableMarkup as FM;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use Drupal\figma\Entity\Example;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/foo/tests/src/Functional/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\foo\Functional;

use Drupal\dcg_test\TestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/nigma/tests/src/Functional/ContentEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Drupal\Tests\nigma\Functional;

use Drupal\Component\Render\FormattableMarkup as FM;
use Drupal\dcg_test\TestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/nigma/tests/src/Kernel/AccessHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Drupal\Tests\nigma\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\nigma\Entity\Example;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\nigma\Entity\Example;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

Expand Down
2 changes: 1 addition & 1 deletion tests/sut/plantain/tests/src/Kernel/BreakpointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\plantain\Kernel;

use Drupal\breakpoint\Breakpoint;
use Drupal\KernelTests\KernelTestBase;
use Drupal\breakpoint\Breakpoint;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/qux/tests/src/Functional/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\qux\Functional;

use Drupal\dcg_test\TestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/qux/tests/src/Functional/FieldTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Drupal\Tests\qux\Functional;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\dcg_test\TestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/sut/qux/tests/src/Kernel/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Drupal\Tests\qux\Kernel;

use Drupal\Core\Access\AccessResultInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\NodeTypeInterface;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\NodeTypeInterface;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/qux/tests/src/Kernel/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Drupal\Tests\qux\Kernel;

use Drupal\Core\Form\FormState;
use Drupal\filter\FilterPluginCollection;
use Drupal\KernelTests\KernelTestBase;
use Drupal\filter\FilterPluginCollection;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/qux/tests/src/Kernel/ViewStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function assertWrapperClass(string $wrapper_class): void {
$build = \views_embed_view('qux_example');
$output = $this->container->get('renderer')->renderRoot($build);
$xml = new \SimpleXMLElement((string) $output);
$xpath = \sprintf('//div/div[@class = "%s"]/div//a[text() = "Hello world!"]', $wrapper_class);
$xpath = \sprintf('//div/div[@class = "%s"]/div//span[text() = "Hello world!"]', $wrapper_class);
self::assertCount(1, $xml->xpath($xpath));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/sut/sigma/tests/src/Functional/ContentEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Drupal\Tests\sigma\Functional;

use Drupal\Component\Render\FormattableMarkup as FM;
use Drupal\Tests\BrowserTestBase;
use Drupal\dcg_test\TestTrait;
use Drupal\sigma\Entity\Example;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/sut/sonata/tests/src/Unit/ExampleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Drupal\Tests\sonata\Unit;

use Composer\Console\Application;
use Drupal\sonata\Command\ExampleCommand;
use Drupal\Tests\UnitTestCase;
use Drupal\sonata\Command\ExampleCommand;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Console\Tester\CommandTester;

Expand Down
2 changes: 1 addition & 1 deletion tests/sut/tandoor/tests/src/Kernel/BreakpointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\tandoor\Kernel;

use Drupal\breakpoint\Breakpoint;
use Drupal\KernelTests\KernelTestBase;
use Drupal\breakpoint\Breakpoint;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down
Loading

0 comments on commit 8c9107d

Please sign in to comment.