Skip to content

Commit

Permalink
Merge pull request #13 from Bee-Lab/fix-deprecation
Browse files Browse the repository at this point in the history
remove deprecated features
  • Loading branch information
garak authored Dec 2, 2023
2 parents 1fa0f27 + f67bea8 commit 6500def
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 113 deletions.
18 changes: 6 additions & 12 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: |
Expand Down
3 changes: 2 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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
---------------

Expand Down
33 changes: 17 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 4 additions & 12 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -142,19 +142,19 @@ 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).

* 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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@ parameters:
count: 1
path: src/Test/WebTestCase.php

-
message: "#SwiftmailerBundle#"
count: 2
path: src/Test/WebTestCase.php
46 changes: 15 additions & 31 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -104,7 +104,7 @@ protected static function saveOutput(bool $delete = true): void
*
* @throws \InvalidArgumentException
*/
protected static function login(string $username = '[email protected]', ?string $firewall = null, ?string $service = null): void
protected static function login(string $username = '[email protected]', string $firewall = null, string $service = null): void
{
$service ??= static::$container->getParameter('beelab_test.user_service');
$object = static::$container->get($service);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -324,15 +307,15 @@ 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);

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);
Expand All @@ -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);
Expand All @@ -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)) {
Expand Down
40 changes: 4 additions & 36 deletions tests/Test/WebTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 6500def

Please sign in to comment.