Skip to content

Commit 90d31e5

Browse files
committed
Tests: update tests and mocks for nextras 5.x API changes
- Use IPlatform instead of IDriver for QueryBuilder constructor - Use DbalMapper instead of removed Mapper class - Add StubCollection to replace Mockery ICollection mock - Update E2E test for removed FileImporter utility https://claude.ai/code/session_01VHxTGscLF36vtGBcPXfThi
1 parent e915b27 commit 90d31e5

7 files changed

Lines changed: 165 additions & 28 deletions

File tree

tests/Cases/E2E/BooksTest.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use Contributte\Nextras\Orm\QueryObject\QueryObjectManager;
88
use Nette\DI\Container;
99
use Nextras\Dbal\IConnection;
1010
use Nextras\Dbal\Result\Result;
11-
use Nextras\Dbal\Utils\FileImporter;
1211
use Nextras\Orm\Collection\ICollection;
1312
use Tester\Assert;
1413
use Tester\TestCase;
@@ -122,7 +121,12 @@ final class BooksTest extends TestCase
122121
{
123122
/** @var IConnection $connection */
124123
$connection = $this->container->getByType(IConnection::class);
125-
FileImporter::executeFile($connection, __DIR__ . '/../../Fixtures/mysql.sql');
124+
$sql = file_get_contents(__DIR__ . '/../../Fixtures/mysql.sql');
125+
assert($sql !== false);
126+
127+
foreach (array_filter(array_map('trim', explode(';', $sql))) as $query) {
128+
$connection->query('%raw', $query);
129+
}
126130
}
127131

128132
}

tests/Cases/QueryObject.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php declare(strict_types = 1);
22

3-
use Nextras\Dbal\Drivers\Mysqli\MysqliDriver;
3+
use Nextras\Dbal\Platforms\IPlatform;
44
use Nextras\Dbal\QueryBuilder\QueryBuilder;
55
use Tester\Assert;
66
use Tests\Mocks\SimpleQueryObject;
77

88
require_once __DIR__ . '/../bootstrap.php';
99

1010
test('QueryObject builds query via fetch method', function (): void {
11+
$platform = Mockery::mock(IPlatform::class);
1112
$qo = new SimpleQueryObject();
12-
$qb = $qo->fetch(new QueryBuilder(new MysqliDriver()));
13+
$qb = $qo->fetch(new QueryBuilder($platform));
1314

1415
Assert::type(QueryBuilder::class, $qb);
1516
Assert::equal('SELECT [*] FROM [foobar]', $qb->getQuerySql());
17+
18+
Mockery::close();
1619
});

tests/Cases/TRepositoryQueryable.phpt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use Nextras\Dbal\Connection;
77
use Nextras\Dbal\QueryBuilder\QueryBuilder;
88
use Nextras\Dbal\Result\Result;
99
use Nextras\Orm\Collection\ICollection;
10-
use Nextras\Orm\Mapper\Mapper;
10+
use Nextras\Orm\Mapper\Dbal\DbalMapper;
1111
use Tester\Assert;
1212
use Tests\Mocks\SimpleQueryObject;
13+
use Tests\Mocks\StubCollection;
1314

1415
require_once __DIR__ . '/../bootstrap.php';
1516

@@ -21,7 +22,7 @@ class TestRepository
2122

2223
use TRepositoryQueryable;
2324

24-
public Mapper $mapper;
25+
public DbalMapper $mapper;
2526

2627
}
2728

@@ -87,13 +88,8 @@ test('TRepositoryQueryable fetch with HYDRATION_RESULTSET returns Result', funct
8788
test('TRepositoryQueryable fetch with HYDRATION_ENTITY returns ICollection', function (): void {
8889
$connection = Mockery::mock(Connection::class);
8990
$queryBuilder = Mockery::mock(QueryBuilder::class);
90-
$mapper = Mockery::mock(Mapper::class);
91-
92-
// Suppress deprecation warnings from ICollection (PHP 8.4 issue with implicit nullable params)
93-
$previousErrorReporting = error_reporting();
94-
error_reporting($previousErrorReporting & ~E_DEPRECATED);
95-
$collection = Mockery::mock(ICollection::class);
96-
error_reporting($previousErrorReporting);
91+
$mapper = Mockery::mock(DbalMapper::class);
92+
$collection = new StubCollection();
9793

9894
$queryBuilder->shouldReceive('select')
9995
->with('[*]')

tests/Mocks/Model/Book/BookMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Tests\Mocks\Model\Book;
44

5-
use Nextras\Orm\Mapper\Mapper;
5+
use Nextras\Orm\Mapper\Dbal\DbalMapper;
66

7-
final class BookMapper extends Mapper
7+
final class BookMapper extends DbalMapper
88
{
99

1010
}

tests/Mocks/Model/User/UserMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Tests\Mocks\Model\User;
44

5-
use Nextras\Orm\Mapper\Mapper;
5+
use Nextras\Orm\Mapper\Dbal\DbalMapper;
66

7-
final class UserMapper extends Mapper
7+
final class UserMapper extends DbalMapper
88
{
99

1010
}

tests/Mocks/SimpleConnection.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44

55
use Mockery;
66
use Nextras\Dbal\Connection;
7-
use Nextras\Dbal\Drivers\IDriver;
8-
use Nextras\Dbal\Drivers\Mysqli\MysqliDriver;
7+
use Nextras\Dbal\Platforms\IPlatform;
98
use Nextras\Dbal\QueryBuilder\QueryBuilder;
109

1110
final class SimpleConnection extends Connection
1211
{
1312

14-
public function getDriver(): IDriver
15-
{
16-
$driver = Mockery::mock(MysqliDriver::class)->makePartial();
17-
$driver->shouldReceive('connect')->andReturn(true);
18-
19-
return $driver;
20-
}
21-
2213
public function createQueryBuilder(): QueryBuilder
2314
{
24-
return new QueryBuilder($this->getDriver());
15+
$platform = Mockery::mock(IPlatform::class);
16+
17+
return new QueryBuilder($platform);
2518
}
2619

2720
}

tests/Mocks/StubCollection.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Mocks;
4+
5+
use EmptyIterator;
6+
use Iterator;
7+
use Nextras\Orm\Collection\ICollection;
8+
use Nextras\Orm\Collection\MemoryCollection;
9+
use Nextras\Orm\Entity\IEntity;
10+
use Nextras\Orm\Mapper\IRelationshipMapper;
11+
use RuntimeException;
12+
13+
/**
14+
* @implements ICollection<IEntity>
15+
*/
16+
final class StubCollection implements ICollection
17+
{
18+
19+
/**
20+
* @param array<string, mixed>|array<mixed> $conds
21+
*/
22+
public function getBy(array $conds): ?IEntity
23+
{
24+
return null;
25+
}
26+
27+
/**
28+
* @param array<string, mixed>|array<mixed> $conds
29+
*/
30+
public function getByChecked(array $conds): IEntity
31+
{
32+
throw new RuntimeException();
33+
}
34+
35+
public function getById(mixed $id): ?IEntity
36+
{
37+
return null;
38+
}
39+
40+
public function getByIdChecked(mixed $id): IEntity
41+
{
42+
throw new RuntimeException();
43+
}
44+
45+
/**
46+
* @param array<mixed> $conds
47+
*/
48+
public function findBy(array $conds): ICollection
49+
{
50+
return $this;
51+
}
52+
53+
/**
54+
* @param string|array<string, string>|list<mixed> $expression
55+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
56+
*/
57+
public function orderBy($expression, string $direction = self::ASC): ICollection
58+
{
59+
return $this;
60+
}
61+
62+
public function resetOrderBy(): ICollection
63+
{
64+
return $this;
65+
}
66+
67+
public function limitBy(int $limit, int|null $offset = null): ICollection
68+
{
69+
return $this;
70+
}
71+
72+
public function fetch(): ?IEntity
73+
{
74+
return null;
75+
}
76+
77+
public function fetchChecked(): IEntity
78+
{
79+
throw new RuntimeException();
80+
}
81+
82+
/**
83+
* @return list<IEntity>
84+
*/
85+
public function fetchAll(): array
86+
{
87+
return [];
88+
}
89+
90+
/**
91+
* @return array<int|string, mixed>
92+
*/
93+
public function fetchPairs(string|null $key = null, string|null $value = null): array
94+
{
95+
return [];
96+
}
97+
98+
/**
99+
* @return Iterator<int, IEntity>
100+
*/
101+
public function getIterator(): Iterator
102+
{
103+
return new EmptyIterator();
104+
}
105+
106+
public function count(): int
107+
{
108+
return 0;
109+
}
110+
111+
public function countStored(): int
112+
{
113+
return 0;
114+
}
115+
116+
public function toMemoryCollection(): MemoryCollection
117+
{
118+
throw new RuntimeException();
119+
}
120+
121+
public function setRelationshipMapper(IRelationshipMapper|null $mapper): ICollection
122+
{
123+
return $this;
124+
}
125+
126+
public function getRelationshipMapper(): ?IRelationshipMapper
127+
{
128+
return null;
129+
}
130+
131+
public function setRelationshipParent(IEntity $parent): ICollection
132+
{
133+
return $this;
134+
}
135+
136+
public function subscribeOnEntityFetch(callable $callback): void
137+
{
138+
// stub, intentionally empty
139+
}
140+
141+
}

0 commit comments

Comments
 (0)