Skip to content

Commit

Permalink
Fix PHPStan flagged issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Apr 17, 2023
1 parent 24161b2 commit 712bcc6
Showing 1 changed file with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,38 @@ class DatabaseMigrationRepositoryTest extends TestCase
{
use MockeryPHPUnitIntegration;

protected Builder | \Mockery\MockInterface $schemaBuilder;
protected QueryBuilder | \Mockery\MockInterface $queryBuilder;
protected Connection | \Mockery\MockInterface $connection;
protected Manager | \Mockery\MockInterface $capsule;
protected Builder $schemaBuilder;
protected QueryBuilder $queryBuilder;
protected Connection $connection;
protected Manager $capsule;

public function setUp(): void
{
parent::setUp();

// Create mock objects and their common expectations
$this->schemaBuilder = Mockery::mock(Builder::class)
/** @var Builder $schemaBuilder */
$schemaBuilder = Mockery::mock(Builder::class)
->shouldReceive('hasTable')->with('Migrafoo')->andReturn(true)->byDefault()
->getMock();
$this->queryBuilder = Mockery::mock(QueryBuilder::class);
$this->connection = Mockery::mock(Connection::class)
$this->schemaBuilder = $schemaBuilder;

/** @var QueryBuilder */
$queryBuilder = Mockery::mock(QueryBuilder::class);
$this->queryBuilder = $queryBuilder;

/** @var Connection */
$connection = Mockery::mock(Connection::class)
->shouldReceive('getSchemaBuilder')->andReturn($this->schemaBuilder)
->shouldReceive('table')->with('Migrafoo')->andReturn($this->queryBuilder)
->getMock();
$this->capsule = Mockery::mock(Manager::class)
$this->connection = $connection;

/** @var Manager */
$capsule = Mockery::mock(Manager::class)
->shouldReceive('getConnection')->andReturn($this->connection)
->getMock();
$this->capsule = $capsule;
}

protected function getRepo(): DatabaseMigrationRepository
Expand All @@ -66,7 +77,7 @@ public function testConstructor(): DatabaseMigrationRepository
$repository = $this->getRepo();

// Make first assertion about class creation.
$this->assertInstanceOf(MigrationRepositoryInterface::class, $repository);
$this->assertInstanceOf(MigrationRepositoryInterface::class, $repository); // @phpstan-ignore-line

return $repository;
}
Expand All @@ -93,7 +104,7 @@ public function testTableNameGetterAndSetter(DatabaseMigrationRepository $reposi
public function testGetConnection(DatabaseMigrationRepository $repository): void
{
$this->assertNull($repository->getConnectionName());
$this->assertInstanceOf(Connection::class, $repository->getConnection());
$this->assertInstanceOf(Connection::class, $repository->getConnection()); // @phpstan-ignore-line
}

/**
Expand All @@ -106,7 +117,7 @@ public function testSetConnectionName(DatabaseMigrationRepository $repository):
{
$repository->setConnectionName('foo');
$this->assertSame('foo', $repository->getConnectionName());
$this->assertInstanceOf(Connection::class, $repository->getConnection());
$this->assertInstanceOf(Connection::class, $repository->getConnection()); // @phpstan-ignore-line
}

/**
Expand All @@ -116,7 +127,7 @@ public function testSetConnectionName(DatabaseMigrationRepository $repository):
*/
public function testGetSchemaBuilder(DatabaseMigrationRepository $repository): void
{
$this->assertInstanceOf(Builder::class, $repository->getSchemaBuilder());
$this->assertInstanceOf(Builder::class, $repository->getSchemaBuilder()); // @phpstan-ignore-line
}

/**
Expand All @@ -126,7 +137,7 @@ public function testGetSchemaBuilder(DatabaseMigrationRepository $repository): v
*/
public function testGetTable(DatabaseMigrationRepository $repository): void
{
$this->assertInstanceOf(QueryBuilder::class, $repository->getTable());
$this->assertInstanceOf(QueryBuilder::class, $repository->getTable()); // @phpstan-ignore-line
}

/**
Expand All @@ -135,6 +146,7 @@ public function testGetTable(DatabaseMigrationRepository $repository): void
public function testRepositoryCreation(): void
{
// Set mock expectations
// @phpstan-ignore-next-line
$this->schemaBuilder
->shouldReceive('hasTable')->with('Migrafoo')->once()->andReturn(true)
->shouldReceive('create')->with('Migrafoo', \Closure::class)->once()
Expand All @@ -157,10 +169,11 @@ public function testRepositoryCreation(): void
*/
public function testGetTableNoExist(): void
{
// @phpstan-ignore-next-line
$this->schemaBuilder
->shouldReceive('hasTable')->once()->andReturn(false)
->shouldReceive('create')->with('Migrafoo', \Closure::class)->once();
$this->assertInstanceOf(QueryBuilder::class, $this->getRepo()->getTable());
$this->assertInstanceOf(QueryBuilder::class, $this->getRepo()->getTable()); // @phpstan-ignore-line
}

/**
Expand All @@ -169,6 +182,7 @@ public function testGetTableNoExist(): void
public function testRepositoryHasTableFalse(): void
{
// Set mock expectations
// @phpstan-ignore-next-line
$this->schemaBuilder
->shouldReceive('hasTable')->with('Migrafoo')->once()->andReturn(false);

Expand All @@ -185,6 +199,7 @@ public function testRepositoryHasTableThrowException(): void
{
// Set mock expectations
$exception = Mockery::mock(QueryException::class);
// @phpstan-ignore-next-line
$this->schemaBuilder
->shouldReceive('hasTable')->with('Migrafoo')->once()->andThrow($exception);

Expand All @@ -200,6 +215,7 @@ public function testRepositoryHasTableThrowException(): void
public function testGetLastBatchNumberAndGetNextBatchNumber(): void
{
// Set mock expectations
// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('max')->twice()->with('batch')->andReturn(3);

Expand All @@ -218,6 +234,7 @@ public function testGetLastBatchNumberAndGetNextBatchNumber(): void
public function testGetLastBatchNumberForEmptyTable(): void
{
// Set mock expectations
// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('max')->once()->with('batch')->andReturn(null);

Expand All @@ -237,6 +254,7 @@ public function testGetMigrationsAndList(): void
['migration' => 'bar']
]);

// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('orderBy')->once()->with('id', 'asc')->andReturn($this->queryBuilder)
->shouldReceive('get')->once()->andReturn($result);
Expand All @@ -258,6 +276,7 @@ public function testGetMigrationsWithStepsAndDesc(): void
['migration' => 'foo'],
]);

// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('max')->once()->with('batch')->andReturn(3)
->shouldReceive('orderBy')->once()->with('id', 'desc')->andReturn($this->queryBuilder)
Expand All @@ -277,8 +296,9 @@ public function testGetMigrationsWithStepsAndDesc(): void
public function testGet(): void
{
// Set mock expectations
$result = new \stdClass(['migration' => 'foo', 'batch' => 1]);
$result = new \stdClass();

// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('where')->with('migration', 'foo')->twice()->andReturn($this->queryBuilder)
->shouldReceive('first')->once()->andReturn($result)
Expand All @@ -290,7 +310,7 @@ public function testGet(): void
// This is mostly mock expectations check. See Integration test
$this->assertTrue($repository->has('foo'));
$migration = $repository->get('foo');
$this->assertIsObject($migration);
$this->assertIsObject($migration); // @phpstan-ignore-line
$this->assertSame($result, $migration);
}

Expand All @@ -299,6 +319,7 @@ public function testGet(): void
*/
public function testGetWithNull(): void
{
// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('where')->with('migration', 'foo')->twice()->andReturn($this->queryBuilder)
->shouldReceive('first')->once()->andReturn(null)
Expand All @@ -321,6 +342,7 @@ public function testLast(): void
['migration' => 'bar', 'batch' => 2],
]);

// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('max')->once()->with('batch')->andReturn(2)
->shouldReceive('where')->once()->with('batch', 2)->andReturn($this->queryBuilder)
Expand All @@ -338,18 +360,20 @@ public function testLast(): void
*/
public function testLogAndDelete(): void
{
// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('insert')->once()->with(['migration' => 'foo', 'batch' => 2])->andReturn(true)
->shouldReceive('where')->once()->with('migration', 'foobar')->andReturn($this->queryBuilder)
->shouldReceive('delete')->once();
$repository = $this->getRepo();

$this->assertTrue($repository->log('foo', 2));
$this->assertNull($repository->remove('foobar'));
$this->assertNull($repository->remove('foobar')); // @phpstan-ignore-line
}

public function testLogNoBatchNumber(): void
{
// @phpstan-ignore-next-line
$this->queryBuilder
->shouldReceive('insert')->once()->with(['migration' => 'foo', 'batch' => 2])->andReturn(true)
->shouldReceive('max')->once()->with('batch')->andReturn(1);
Expand Down

0 comments on commit 712bcc6

Please sign in to comment.