From f67bea8cb072f3af2a81f947ffaa01d2225ca463 Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Sat, 28 Oct 2023 09:25:52 +0200 Subject: [PATCH] remove deprecated features --- .github/workflows/build.yaml | 18 +++++-------- .php-cs-fixer.php | 3 ++- README.md | 2 +- UPGRADE.md | 7 ++++++ composer.json | 33 ++++++++++++------------ docs/index.md | 16 +++--------- phpstan-baseline.neon | 4 --- src/Test/WebTestCase.php | 46 +++++++++++----------------------- tests/Test/WebTestCaseTest.php | 40 +++-------------------------- 9 files changed, 56 insertions(+), 113 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 7e4c8cf..6e3f5a0 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -31,19 +31,13 @@ jobs: fail-fast: false matrix: include: - - description: 'Symfony 5.4' - php: '7.4' - symfony: '5.4.*' - composer_option: '--prefer-lowest' - - description: 'Symfony 6.0' - php: '8.0' - symfony: '6.0.*' - - description: 'Symfony 6.1' + - description: 'Symfony 6.4' php: '8.1' - symfony: '6.1.*' - - description: 'Symfony 6.2' + symfony: '6.4.*@dev' + composer_option: '--prefer-lowest' + - description: 'Symfony 7.0' php: '8.2' - symfony: '6.2.*' + symfony: '7.0.*@dev' name: PHP ${{ matrix.php }} tests (${{ matrix.description }}) steps: - name: Checkout @@ -60,7 +54,7 @@ jobs: - name: require specific Symfony version if: matrix.symfony run: | - sed -ri '/symfony\/swiftmailer-bundle/! s/"symfony\/(.+)": "(.+)"/"symfony\/\1": "'${{ matrix.symfony }}'"/' composer.json; + sed -ri 's/"symfony\/(.+)": "(.+)"/"symfony\/\1": "'${{ matrix.symfony }}'"/' composer.json; - name: use possible dev deps if: contains(matrix.symfony, '@dev') run: | diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index efa88f3..71e8714 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -11,11 +11,12 @@ ->setRules([ '@Symfony' => true, '@Symfony:risky' => true, - '@PHP73Migration' => true, + '@PHP80Migration:risky' => true, '@PHPUnit84Migration:risky' => true, 'declare_strict_types' => false, 'native_function_invocation' => ['include' => ['@all']], 'php_unit_mock_short_will_return' => true, + 'trailing_comma_in_multiline' => ['elements' => ['arguments', 'arrays', 'match', 'parameters']], ]) ->setFinder($finder) ; diff --git a/README.md b/README.md index 6611648..66d0dd1 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Documentation >**WARNING** if you're using version 3 of this bundle with Symfony 4.2, and you want to upgrade to >Symfony 4.3, you need to require version 4 of this bundle. See also [UPGRADE file](UPGRADE.md). ->If you use Symfony 6.0, you need to pay attention to definition of service, as explained in following documentation. +>If you use Symfony 6, you need to pay attention to definition of service, as explained in following documentation. [Read the documentation](docs/index.md) diff --git a/UPGRADE.md b/UPGRADE.md index 1a1b1c5..42bb614 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,6 +1,13 @@ UPGRADE ======= +From 5.4 to 5.5 +--------------- + +The `assertMailSent` method has been removed, since Swiftmailer is not supported anymore. You should use the +Symfony Mailer component instead, and the `assertEmailCount` method. +New minimum requirements are PHP 8.1 and Symfony 6.4. + From 4.0 to 5.0 --------------- diff --git a/composer.json b/composer.json index 38c8783..a487017 100644 --- a/composer.json +++ b/composer.json @@ -15,24 +15,25 @@ } ], "require": { - "php": "^7.4 || ^8.0", - "doctrine/data-fixtures": "^1.5", - "symfony/browser-kit": "^5.4 || ^6.0", - "symfony/console": "^5.4 || ^6.0", - "symfony/css-selector": "^5.4 || ^6.0", - "symfony/dependency-injection": "^5.4 || ^6.0", - "symfony/dom-crawler": "^5.4 || ^6.0" + "php": "^8.1", + "doctrine/data-fixtures": "^1.6", + "symfony/browser-kit": "^6.4 || 7.0", + "symfony/console": "^6.4 || ^7.0", + "symfony/css-selector": "^6.4 || ^7.0", + "symfony/dependency-injection": "^6.4 || ^7.0", + "symfony/dom-crawler": "^6.4 || ^7.0" }, "require-dev": { - "doctrine/lexer": "^1.2 || ^2.0 || ^3.0", - "doctrine/orm": "^2.8", - "mikey179/vfsstream": "^1.6", - "phpunit/phpunit": "^9.5", - "symfony/doctrine-bridge": "^5.4 || ^6.0", - "symfony/framework-bundle": "^5.4 || ^6.0", - "symfony/mime": "^5.4 || ^6.0", - "symfony/process": "^5.4 || ^6.0", - "symfony/security-core": "^5.4 || ^6.0" + "doctrine/doctrine-fixtures-bundle": "^3.4 || ^4.0", + "doctrine/lexer": "^2.1", + "doctrine/orm": "^2.16", + "mikey179/vfsstream": "^1.6.7", + "phpunit/phpunit": "^9.6", + "symfony/doctrine-bridge": "^6.4 || ^7.0", + "symfony/framework-bundle": "^6.4 || ^7.0", + "symfony/mime": "^6.4 || ^7.0", + "symfony/process": "^6.4 || ^7.0", + "symfony/security-core": "^6.4 || ^7.0" }, "config": { "bin-dir": "bin", diff --git a/docs/index.md b/docs/index.md index ee43946..b361a4a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -42,7 +42,7 @@ final class MyTest extends WebTestCase } ``` -With Symfony 6, you'll also need to define an alias in your `config/services_test.yaml` file: +With Symfony 6+, you'll also need to define an alias in your `config/services_test.yaml` file: ```yaml services: @@ -142,11 +142,11 @@ services: In case of a select, note that you need to add `option` after your field's id, and you can pass a third optional parameter with the position. If, for example, you want to retrieve the value of your second option in a `bar` field of a `foo` form - (maybe beacuse the first one is empty), you can do `self::getFormValue('foo_bar option', 1)` + (maybe because the first one is empty), you can do `self::getFormValue('foo_bar option', 1)` * Forms with collections - Tipically, a form with a collection is a problem during tests, because the values of collections are not displayed + Typically, a form with a collection is a problem during tests, because the values of collections are not displayed in the HTML (but, instead, added via JavaScript). You can solve such problema by visiting form URL and then using `self::postForm('your_form_name', $values)` (where`$values` can include collection values). @@ -154,7 +154,7 @@ services: * Selecting checkboxes You can check many checkboxes using `self::tickCheckboxes($form, $values)`, using for values the same - array you would use for a select multiple. This allows you to easily switch bewteen `true` and `false` + array you would use for a select multiple. This allows you to easily switch between `true` and `false` in `multiple` option of `ChoiceType`. ### Fixtures-related @@ -195,14 +195,6 @@ services: for example: `self::submitFormByData('submit', $values, [], 'POST', [], ['foo[bar]' => ['value1', 'value2', 'value3']])` -### E-mail related - -* Mail sent assertion - - Check how many mails has been sent with `self::assertMailSent(1)` (or 2, 3, etc.). - You need to call `$self::client->enableProfiler()` before. - Currently, this is working only with SwiftMailer. - ### Command related * Test commands diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 294532a..b29e27b 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,7 +10,3 @@ parameters: count: 1 path: src/Test/WebTestCase.php - - - message: "#SwiftmailerBundle#" - count: 2 - path: src/Test/WebTestCase.php diff --git a/src/Test/WebTestCase.php b/src/Test/WebTestCase.php index 06623bd..45445d4 100644 --- a/src/Test/WebTestCase.php +++ b/src/Test/WebTestCase.php @@ -4,9 +4,9 @@ use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\Executor\ORMExecutor; +use Doctrine\Common\DataFixtures\Loader; use Doctrine\Common\DataFixtures\Purger\ORMPurger; use Doctrine\ORM\EntityManagerInterface; -use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader as Loader; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SymfonyWebTestCase; @@ -36,7 +36,7 @@ abstract class WebTestCase extends SymfonyWebTestCase protected static ?string $authPw = null; /** @var ContainerInterface */ - protected static $container = null; + protected static $container; protected function setUp(): void { @@ -104,7 +104,7 @@ protected static function saveOutput(bool $delete = true): void * * @throws \InvalidArgumentException */ - protected static function login(string $username = 'admin1@example.org', ?string $firewall = null, ?string $service = null): void + protected static function login(string $username = 'admin1@example.org', string $firewall = null, string $service = null): void { $service ??= static::$container->getParameter('beelab_test.user_service'); $object = static::$container->get($service); @@ -186,19 +186,19 @@ protected static function getTxtFile(string $file = '0'): UploadedFile protected function loadFixtures( array $fixtures, string $namespace = 'App\\DataFixtures\\ORM\\', - ?string $managerService = null, - bool $append = false + string $managerService = null, + bool $append = false, ): void { if (null !== $managerService) { $manager = static::$container->get($managerService); if (!$manager instanceof EntityManagerInterface) { - throw new \InvalidArgumentException(\sprintf('The service "%s" is not an EntityManager', \get_class($manager))); + throw new \InvalidArgumentException(\sprintf('The service "%s" is not an EntityManager', $manager::class)); } } else { $manager = self::$em; } $manager->getConnection()->executeStatement('SET foreign_key_checks = 0'); - $loader = new Loader(static::$container); + $loader = new Loader(); foreach ($fixtures as $fixture) { $this->loadFixtureClass($loader, $namespace.$fixture); } @@ -207,23 +207,6 @@ protected function loadFixtures( $manager->getConnection()->executeStatement('SET foreign_key_checks = 1'); } - /** - * Assert that $num mail has been sent - * Need self::$client->enableProfiler() before calling. - */ - protected static function assertMailSent(int $num, string $message = ''): void - { - if (false === $profile = self::$client->getProfile()) { - self::markTestSkipped('Profiler not enabled.'); - } - /** @var \Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector|null $collector */ - $collector = $profile->getCollector('swiftmailer'); - if (null === $collector) { - self::markTestSkipped('Swiftmailer profiler not found.'); - } - self::assertEquals($num, $collector->getMessageCount(), $message); - } - /** * Get a form field value, from its id * Useful for POSTs. @@ -238,7 +221,7 @@ protected static function getFormValue(string $fieldId, int $position = 0): stri */ protected static function ajax(string $method, string $uri, array $params = [], array $files = []): Crawler { - return self::$client->request($method, $uri, $params, $files, ['HTTP_X-Requested-With' => 'XMLHttpRequest']); + return self::$client->xmlHttpRequest($method, $uri, $params, $files); } /** @@ -255,7 +238,7 @@ protected static function commandTest( Command $command, array $arguments = [], array $otherCommands = [], - array $inputs = null + array $inputs = null, ): string { $application = new Application(self::$client->getKernel()); $application->add($command); @@ -294,7 +277,7 @@ protected static function getFile(string $file, string $data, string $ext, strin { $name = 'file_'.$file.'.'.$ext; $path = \tempnam(\sys_get_temp_dir(), 'sf_test_').$name; - \file_put_contents($path, 0 === \strpos($mime, 'text') ? $data : \base64_decode($data)); + \file_put_contents($path, \str_starts_with($mime, 'text') ? $data : \base64_decode($data)); return new UploadedFile($path, $name, $mime); } @@ -324,7 +307,7 @@ protected static function setSessionException(string $msg = 'error...'): void self::$client->getCookieJar()->set($cookie); } - protected static function clickLinkByData(string $dataName, ?string $parent = null): Crawler + protected static function clickLinkByData(string $dataName, string $parent = null): Crawler { $selector = (null === $parent ? '' : $parent.' ').'a[data-'.$dataName.']'; $linkNode = self::$client->getCrawler()->filter($selector); @@ -332,7 +315,7 @@ protected static function clickLinkByData(string $dataName, ?string $parent = nu return self::$client->click($linkNode->link()); } - protected static function clickLinkBySelectorText(string $linkText, ?string $parent = null): Crawler + protected static function clickLinkBySelectorText(string $linkText, string $parent = null): Crawler { $selector = (null === $parent ? '' : $parent.' ').'a:contains("'.$linkText.'")'; $linkNode = self::$client->getCrawler()->filter($selector); @@ -349,7 +332,7 @@ protected static function submitFormByData( array $values = [], string $method = 'POST', array $serverParams = [], - array $checkboxValues = [] + array $checkboxValues = [], ): Crawler { $buttonNode = self::$client->getCrawler()->filter('button[data-'.$dataName.']'); $form = $buttonNode->form($values, $method); @@ -374,8 +357,9 @@ protected static function tickCheckboxes(Form $form, array $checkboxValues = []) } } - private static function findCheckbox(Form $form, string $name, string $value): ?ChoiceFormField + private static function findCheckbox(Form $form, string $name, string $value): ChoiceFormField { + /** @var ChoiceFormField $field */ foreach ($form->offsetGet($name) as $field) { $available = $field->availableOptionValues(); if ($value === \reset($available)) { diff --git a/tests/Test/WebTestCaseTest.php b/tests/Test/WebTestCaseTest.php index 8187ced..952e4a7 100644 --- a/tests/Test/WebTestCaseTest.php +++ b/tests/Test/WebTestCaseTest.php @@ -3,11 +3,10 @@ namespace Beelab\TestBundle\Tests; use Beelab\TestBundle\Test\WebTestCase; +use Doctrine\Common\DataFixtures\Loader; use org\bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase; -use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader; use Symfony\Bundle\FrameworkBundle\KernelBrowser; -use Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector; use Symfony\Component\BrowserKit\CookieJar; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DomCrawler\Crawler; @@ -219,7 +218,7 @@ public function testGetTxtFile(): void public function testLoadFixtureClass(): void { - $loader = $this->getMockBuilder(ContainerAwareLoader::class)->disableOriginalConstructor()->getMock(); + $loader = $this->getMockBuilder(Loader::class)->disableOriginalConstructor()->getMock(); // Call `loadFixtureClass` method $method = new \ReflectionMethod(self::$mock, 'loadFixtureClass'); @@ -267,8 +266,8 @@ public function testAjax(): void { self::$client ->expects(self::once()) - ->method('request') - ->with('GET', 'http://ajax/', [], [], ['HTTP_X-Requested-With' => 'XMLHttpRequest']) + ->method('xmlHttpRequest') + ->with('GET', 'http://ajax/') ->willReturn(new Crawler()); // Call `ajax` method @@ -284,37 +283,6 @@ public function testCommandTest(): void self::markTestIncomplete('Not implemented'); } - public function testAssertMailSent(): void - { - self::markTestIncomplete('cannot mock static call to "assertEquals"'); - /* - $swiftmailerProfiler = $this->createMock(MessageDataCollector::class); - $swiftmailerProfiler - ->expects(self::once()) - ->method('getMessageCount') - ->willReturn(1); - - $profiler = $this->getMockBuilder(Profile::class)->disableOriginalConstructor()->getMock(); - $profiler - ->expects(self::once()) - ->method('getCollector') - ->with('swiftmailer') - ->willReturn($swiftmailerProfiler); - - self::$client - ->expects(self::once()) - ->method('getProfile') - ->willReturn($profiler); - - self::$mock->method('assertEquals')->willReturn(true); - - // Call `assertMailSent` method - $method = new \ReflectionMethod(self::$mock, 'assertMailSent'); - $method->setAccessible(true); - $method->invoke(self::$mock, 1); - */ - } - public function testClickLinkByData(): void { $link = $this->createMock(Link::class);