Skip to content

Commit cb5afbf

Browse files
authored
Make Abstract{Migration,Seed}::getAdapter return non-null (#2261)
1 parent 1ffe298 commit cb5afbf

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/Phinx/Migration/AbstractMigration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ public function setAdapter(AdapterInterface $adapter): MigrationInterface
103103
/**
104104
* @inheritDoc
105105
*/
106-
public function getAdapter(): ?AdapterInterface
106+
public function getAdapter(): AdapterInterface
107107
{
108+
if (!isset($this->adapter)) {
109+
throw new RuntimeException('Cannot access `adapter` it has not been set');
110+
}
111+
108112
return $this->adapter;
109113
}
110114

src/Phinx/Seed/AbstractSeed.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Phinx\Db\Adapter\AdapterInterface;
1212
use Phinx\Db\Table;
13+
use RuntimeException;
1314
use Symfony\Component\Console\Input\InputInterface;
1415
use Symfony\Component\Console\Output\OutputInterface;
1516

@@ -98,6 +99,10 @@ public function setAdapter(AdapterInterface $adapter): SeedInterface
9899
*/
99100
public function getAdapter(): AdapterInterface
100101
{
102+
if (!isset($this->adapter)) {
103+
throw new RuntimeException('Cannot access `adapter` it has not been set');
104+
}
105+
101106
return $this->adapter;
102107
}
103108

tests/Phinx/Migration/AbstractMigrationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public function testAdapterMethods()
1919
->getMock();
2020

2121
// test methods
22-
$this->assertNull($migrationStub->getAdapter());
22+
$this->expectException(RuntimeException::class);
23+
$migrationStub->getAdapter();
2324
$migrationStub->setAdapter($adapterStub);
2425
$this->assertInstanceOf(
2526
'Phinx\Db\Adapter\AdapterInterface',

tests/Phinx/Seed/AbstractSeedTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Test\Phinx\Seed;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use RuntimeException;
8+
9+
class AbstractSeedTest extends TestCase
10+
{
11+
public function testAdapterMethods()
12+
{
13+
// stub migration
14+
$migrationStub = $this->getMockForAbstractClass('\Phinx\Seed\AbstractSeed', ['mockenv', 20230102030405]);
15+
16+
// stub adapter
17+
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
18+
->setConstructorArgs([[]])
19+
->getMock();
20+
21+
// test methods
22+
$this->expectException(RuntimeException::class);
23+
$migrationStub->getAdapter();
24+
$migrationStub->setAdapter($adapterStub);
25+
$this->assertInstanceOf(
26+
'Phinx\Db\Adapter\AdapterInterface',
27+
$migrationStub->getAdapter()
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)