Skip to content

Fixing tests for Symfony5 #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ env:
matrix:
fast_finish: true
include:
- php: 7.1.33
- php: 7.3
- php: 7.3
env: MAKER_TEST_VERSION=dev
allow_failures:
- php: 7.3
- php: 7.2
env: MAKER_TEST_VERSION=stable-dev
- php: 7.2
env: MAKER_TEST_VERSION=dev

before_install:
Expand Down
5 changes: 5 additions & 0 deletions src/EventRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public function getEventClassName(string $event)
return Event::class;
}

// ignore an "object" type-hint
if ('object' === $type) {
continue;
}

return $type;
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Test/MakerTestEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ private function postMake()
$yaml = file_get_contents($this->path.'/config/packages/security.yaml');
$manipulator = new YamlSourceManipulator($yaml);
$data = $manipulator->getData();

foreach ($guardAuthenticators as $firewallName => $id) {
if (!isset($data['security']['firewalls'][$firewallName])) {
throw new \Exception(sprintf('Could not find firewall "%s"', $firewallName));
Expand Down Expand Up @@ -320,9 +321,9 @@ private function buildFlexSkeleton()

$rootPath = str_replace('\\', '\\\\', realpath(__DIR__.'/../..'));

// allow dev dependencies
// dev deps already will allow dev deps, but we should prefer stable
if (false !== strpos($targetVersion, 'dev')) {
MakerTestProcess::create('composer config minimum-stability dev', $this->flexPath)
MakerTestProcess::create('composer config prefer-stable true', $this->flexPath)
->run();
}

Expand Down Expand Up @@ -449,10 +450,7 @@ private function getTargetFlexVersion(): string

break;
case 'dev':
$version = $data['dev'];
$parts = explode('.', $version);

$this->targetFlexVersion = sprintf('%s.%s.x-dev', $parts[0], $parts[1]);
$this->targetFlexVersion = 'dev-master';

break;
default:
Expand Down
6 changes: 3 additions & 3 deletions src/Util/YamlSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private function updateData(array $newData)
}

// 3b) value DID change
$this->log(sprintf('updating value to {%s}', is_array($newVal) ? '<array>' : $newVal));
$this->log(sprintf('updating value to {%s}', \is_array($newVal) ? '<array>' : $newVal));
$this->changeValueInYaml($newVal);
}

Expand Down Expand Up @@ -465,7 +465,7 @@ private function changeValueInYaml($value)
$newPosition = $this->currentPosition + \strlen($newYamlValue);
$isNextContentComment = $this->isPreviousLineComment($newPosition);
if ($isNextContentComment) {
$newPosition++;
++$newPosition;
}

$newContents = substr($this->contents, 0, $this->currentPosition)
Expand Down Expand Up @@ -785,7 +785,7 @@ private function findEndPositionOfValue($value, $offset = null)

// a value like "foo:" can simply end a file
// this means the value is null
if ($offset === strlen($this->contents)) {
if ($offset === \strlen($this->contents)) {
return $offset;
}

Expand Down
26 changes: 20 additions & 6 deletions tests/EventRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,25 @@ public function testGetEventClassNameReturnsType()
{
$eventObj = new DummyEvent();
$dispatcher = $this->createMock(EventDispatcherInterface::class);

$listenersMap = [
'someFunctionToSkip',
[$eventObj, 'methodNoArg'],
[$eventObj, 'methodNoType'],
[$eventObj, 'methodObjectType'],
[$eventObj, 'methodWithType'],
];

// less than PHP 7.2, unset object type-hint example
// otherwise, it looks like a class in this namespace
if (PHP_VERSION_ID < 70200) {
unset($listenersMap[3]);
}

$dispatcher->expects($this->once())
->method('getListeners')
->with('foo.bar')
->willReturn([
'someFunctionToSkip',
[$eventObj, 'methodNoArg'],
[$eventObj, 'methodNoType'],
[$eventObj, 'methodWithType'],
]);
->willReturn($listenersMap);

$registry = new EventRegistry($dispatcher);
$this->assertSame(GetResponseForExceptionEvent::class, $registry->getEventClassName('foo.bar'));
Expand Down Expand Up @@ -85,6 +95,10 @@ public function methodNoType($event)
{
}

public function methodObjectType(object $event)
{
}

public function methodWithType(GetResponseForExceptionEvent $event)
{
}
Expand Down
24 changes: 19 additions & 5 deletions tests/Maker/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,9 @@ function (string $output, string $directory) {
->assert(function (string $output, string $directory) {
$this->assertStringContainsString('created: src/Controller/SweetFoodController.php', $output);
$this->assertStringContainsString('created: src/Form/SweetFoodType.php', $output);
}),
})
// workaround for segfault in PHP 7.1 CI :/
->setRequiredPhpVersion(70200)
];

yield 'crud_basic_in_custom_root_namespace' => [MakerTestDetails::createTest(
Expand All @@ -681,7 +683,9 @@ function (string $output, string $directory) {
->assert(function (string $output, string $directory) {
$this->assertStringContainsString('created: src/Controller/SweetFoodController.php', $output);
$this->assertStringContainsString('created: src/Form/SweetFoodType.php', $output);
}),
})
// workaround for segfault in PHP 7.1 CI :/
->setRequiredPhpVersion(70200)
];

yield 'crud_repository' => [MakerTestDetails::createTest(
Expand Down Expand Up @@ -713,7 +717,9 @@ function (string $output, string $directory) {
->assert(function (string $output, string $directory) {
$this->assertStringContainsString('created: src/Controller/SweetFoodController.php', $output);
$this->assertStringContainsString('created: src/Form/SweetFoodType.php', $output);
}),
})
// workaround for segfault in PHP 7.1 CI :/
->setRequiredPhpVersion(70200)
];

yield 'registration_form_entity_guard_authenticate' => [MakerTestDetails::createTest(
Expand All @@ -729,7 +735,12 @@ function (string $output, string $directory) {
])
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeRegistrationFormEntity')
->configureDatabase()
->updateSchemaAfterCommand(),
->updateSchemaAfterCommand()
// workaround for a strange behavior where, every other
// test run, the UniqueEntity would not be seen, because
// the the validation cache was out of date. The cause
// is currently unknown, so this workaround was added
->addPostMakeCommand('php bin/console cache:clear --env=test')
];

// sanity check on all the interactive questions
Expand Down Expand Up @@ -757,7 +768,10 @@ function (string $output, string $directory) {
])
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeRegistrationFormEntity')
->configureDatabase()
->updateSchemaAfterCommand(),
->updateSchemaAfterCommand()
// workaround for strange failure - see test case
// registration_form_entity_guard_authenticate for details
->addPostMakeCommand('php bin/console cache:clear --env=test')
];

yield 'auth_login_form_user_entity_with_encoder_logout' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class RegistrationFormTest extends WebTestCase
{
public function testRegistrationSuccessful()
{
self::bootKernel();
$client = static::createClient();

/** @var EntityManager $em */
$em = self::$kernel->getContainer()
->get('doctrine')
->getManager();
$em->createQuery('DELETE FROM App\\Entity\\User u')
->execute();

$client = static::createClient();
$crawler = $client->request('GET', '/register');
$form = $crawler->selectButton('Register')->form();
$form['registration_form[email]'] = '[email protected]';
Expand All @@ -34,7 +34,8 @@ public function testRegistrationSuccessful()

public function testRegistrationValidationError()
{
self::bootKernel();
$client = static::createClient();

/** @var EntityManager $em */
$em = self::$kernel->getContainer()
->get('doctrine')
Expand All @@ -47,7 +48,6 @@ public function testRegistrationValidationError()
$em->persist($user);
$em->flush();

$client = static::createClient();
$crawler = $client->request('GET', '/register');
$form = $crawler->selectButton('Register')->form();
$form['registration_form[email]'] = '[email protected]';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class GeneratedEntityTest extends WebTestCase
{
public function testGeneratedEntity()
{
self::bootKernel();
$client = static::createClient();

/** @var EntityManager $em */
$em = self::$kernel->getContainer()
->get('doctrine')
Expand All @@ -31,7 +32,6 @@ public function testGeneratedEntity()
$em->flush();

// login then access a protected page
$client = static::createClient();
$client->request('GET', '/[email protected]');

$this->assertSame(302, $client->getResponse()->getStatusCode());
Expand Down