Skip to content

Commit

Permalink
[Http-Foundation] fix: test + coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
alli83 committed Nov 10, 2022
1 parent 3ba0401 commit a62248e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

final class PdoSessionHandlerSchemaSubscriber extends AbstractSchemaSubscriber
{
private array $pdoSessionHandlers;
private iterable $pdoSessionHandlers;

/**
* @param iterable<mixed, PdoSessionHandler> $pdoSessionHandlers
Expand All @@ -31,7 +31,7 @@ public function postGenerateSchema(GenerateSchemaEventArgs $event): void
$connection = $event->getEntityManager()->getConnection();

foreach ($this->pdoSessionHandlers as $pdoSessionHandler) {
$pdoSessionHandler->configureSchema($event->getSchema(), $connection, $this->getIsSameDatabaseChecker($connection));
$pdoSessionHandler->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ public function updateExistingToken(PersistentTokenInterface $token, #[\Sensitiv

/**
* Adds the Table to the Schema if "remember me" uses this Connection.
*
* @param \Closure $isSameDatabase
*/
public function configureSchema(Schema $schema, Connection $forConnection/* , \Closure $isSameDatabase */): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public function testPostGenerateSchemaPdo()
$event = new GenerateSchemaEventArgs($entityManager, $schema);

$pdoSessionHandler = $this->createMock(PdoSessionHandler::class);
$someFunction = function () { return true; };
$pdoSessionHandler->expects($this->once())
->method('configureSchema')
->with($schema, $dbalConnection);
->with($schema, $someFunction);

$subscriber = new PdoSessionHandlerSchemaSubscriber($pdoSessionHandler);
$subscriber = new PdoSessionHandlerSchemaSubscriber([$pdoSessionHandler]);
$subscriber->postGenerateSchema($event);
}
}
3 changes: 0 additions & 3 deletions src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public function createTable(): void
}
}

/**
* @param \Closure $isSameDatabase
*/
public function configureSchema(Schema $schema, Connection $forConnection/* , \Closure $isSameDatabase */): void
{
if ($schema->hasTable($this->table)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;

Expand Down Expand Up @@ -179,7 +178,7 @@ public function __construct(\PDO|string $pdoOrDsn = null, array $options = [])
$this->ttl = $options['ttl'] ?? null;
}

public function configureSchema(Schema $schema, Connection $connection, \Closure $isSameDatabase): void
public function configureSchema(Schema $schema, \Closure $isSameDatabase): void
{
if ($schema->hasTable($this->table) || !$isSameDatabase($this->getConnection()->exec(...))) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;

use Doctrine\DBAL\Schema\Schema;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;

/**
* @requires extension pdo_sqlite
*
* @group time-sensitive
*/
class PdoSessionHandlerTest extends TestCase
Expand Down Expand Up @@ -326,6 +328,38 @@ public function testUrlDsn($url, $expectedDsn, $expectedUser = null, $expectedPa
}
}

public function testConfigureSchemaDifferentDatabase()
{
$someFunction = function () { return false; };
$schema = new Schema();

$pdoSessionHandler = new PdoSessionHandler($this->getMemorySqlitePdo());
$pdoSessionHandler->configureSchema($schema, $someFunction);
$this->assertFalse($schema->hasTable('sessions'));
}

public function testConfigureSchemaSameDatabase()
{
$someFunction = function () { return true; };
$schema = new Schema();

$pdoSessionHandler = new PdoSessionHandler($this->getMemorySqlitePdo());
$pdoSessionHandler->configureSchema($schema, $someFunction);
$this->assertTrue($schema->hasTable('sessions'));
}

public function testConfigureSchemaTableExistsPdo()
{
$schema = new Schema();
$schema->createTable('sessions');

$pdoSessionHandler = new PdoSessionHandler($this->getMemorySqlitePdo());
$someFunction = function () { return true; };
$pdoSessionHandler->configureSchema($schema, $someFunction);
$table = $schema->getTable('sessions');
$this->assertEmpty($table->getColumns(), 'The table was not overwritten');
}

public function provideUrlDsnPairs()
{
yield ['mysql://localhost/test', 'mysql:host=localhost;dbname=test;'];
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"symfony/http-kernel": "^5.4.12|^6.0.12|^6.1.4",
"symfony/mime": "^5.4|^6.0",
"symfony/expression-language": "^5.4|^6.0",
"symfony/rate-limiter": "^5.2|^6.0"
"symfony/rate-limiter": "^5.2|^6.0",
"doctrine/dbal": "^2.13.1|^3.0"
},
"conflict": {
"symfony/cache": "<6.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,32 +408,35 @@ public function providePlatformSql(): iterable
public function testConfigureSchema()
{
$driverConnection = $this->getDBALConnectionMock();
$someFunction = function () { return true; };
$schema = new Schema();

$connection = new Connection(['table_name' => 'queue_table'], $driverConnection);
$connection->configureSchema($schema, $driverConnection);
$connection->configureSchema($schema, $driverConnection, $someFunction);
$this->assertTrue($schema->hasTable('queue_table'));
}

public function testConfigureSchemaDifferentDbalConnection()
{
$driverConnection = $this->getDBALConnectionMock();
$driverConnection2 = $this->getDBALConnectionMock();
$someFunction = function () { return false; };
$schema = new Schema();

$connection = new Connection([], $driverConnection);
$connection->configureSchema($schema, $driverConnection2);
$connection->configureSchema($schema, $driverConnection2, $someFunction);
$this->assertFalse($schema->hasTable('messenger_messages'));
}

public function testConfigureSchemaTableExists()
{
$driverConnection = $this->getDBALConnectionMock();
$someFunction = function () { return true; };
$schema = new Schema();
$schema->createTable('messenger_messages');

$connection = new Connection([], $driverConnection);
$connection->configureSchema($schema, $driverConnection);
$connection->configureSchema($schema, $driverConnection, $someFunction);
$table = $schema->getTable('messenger_messages');
$this->assertEmpty($table->getColumns(), 'The table was not overwritten');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public function setup(): void

/**
* Adds the Table to the Schema if this transport uses this connection.
*
* @param \Closure $isSameDatabase
*/
public function configureSchema(Schema $schema, DbalConnection $forConnection/* , \Closure $isSameDatabase */): void
{
Expand Down

0 comments on commit a62248e

Please sign in to comment.