Skip to content

Commit

Permalink
Make Abstract{Migration,Seed}::getAdapter return non-null (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin authored Jan 23, 2024
1 parent 1ffe298 commit cb5afbf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Phinx/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ public function setAdapter(AdapterInterface $adapter): MigrationInterface
/**
* @inheritDoc
*/
public function getAdapter(): ?AdapterInterface
public function getAdapter(): AdapterInterface
{
if (!isset($this->adapter)) {
throw new RuntimeException('Cannot access `adapter` it has not been set');
}

return $this->adapter;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Phinx/Seed/AbstractSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Table;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -98,6 +99,10 @@ public function setAdapter(AdapterInterface $adapter): SeedInterface
*/
public function getAdapter(): AdapterInterface
{
if (!isset($this->adapter)) {
throw new RuntimeException('Cannot access `adapter` it has not been set');
}

return $this->adapter;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Phinx/Migration/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function testAdapterMethods()
->getMock();

// test methods
$this->assertNull($migrationStub->getAdapter());
$this->expectException(RuntimeException::class);
$migrationStub->getAdapter();
$migrationStub->setAdapter($adapterStub);
$this->assertInstanceOf(
'Phinx\Db\Adapter\AdapterInterface',
Expand Down
30 changes: 30 additions & 0 deletions tests/Phinx/Seed/AbstractSeedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Test\Phinx\Seed;

use PHPUnit\Framework\TestCase;
use RuntimeException;

class AbstractSeedTest extends TestCase
{
public function testAdapterMethods()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Seed\AbstractSeed', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
->setConstructorArgs([[]])
->getMock();

// test methods
$this->expectException(RuntimeException::class);
$migrationStub->getAdapter();
$migrationStub->setAdapter($adapterStub);
$this->assertInstanceOf(
'Phinx\Db\Adapter\AdapterInterface',
$migrationStub->getAdapter()
);
}
}

0 comments on commit cb5afbf

Please sign in to comment.