Skip to content

Commit 3308bf8

Browse files
committed
Tests: use contributte/tester, add Toolkit::test(), add native type hints, cleanup bootstrap
1 parent 3793f14 commit 3308bf8

14 files changed

Lines changed: 71 additions & 47 deletions

tests/.coveralls.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/.gitignore

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/Cases/E2E/BooksTest.phpt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ use Contributte\Nextras\Orm\QueryObject\Queryable;
66
use Contributte\Nextras\Orm\QueryObject\QueryObjectContextAwareManager;
77
use Contributte\Nextras\Orm\QueryObject\QueryObjectManager;
88
use Nette\DI\Container;
9+
use Nextras\Dbal\Drivers\Exception\ConnectionException;
910
use Nextras\Dbal\IConnection;
1011
use Nextras\Dbal\Result\Result;
1112
use Nextras\Orm\Collection\ICollection;
1213
use Tester\Assert;
14+
use Tester\Environment;
1315
use Tester\TestCase;
1416
use Tests\Mocks\Model\Book\AllBooksExecutableQueryObject;
1517
use Tests\Mocks\Model\Book\AllBooksQueryObject;
@@ -19,6 +21,15 @@ use Tests\Mocks\Model\User\User;
1921

2022
$container = require_once __DIR__ . '/../../bootstrap.container.php';
2123

24+
/** @var IConnection $connection */
25+
$connection = $container->getByType(IConnection::class);
26+
27+
try {
28+
$connection->connect();
29+
} catch (ConnectionException $e) {
30+
Environment::skip('MySQL connection not available: ' . $e->getMessage());
31+
}
32+
2233
final class BooksTest extends TestCase
2334
{
2435

@@ -124,8 +135,8 @@ final class BooksTest extends TestCase
124135
$sql = file_get_contents(__DIR__ . '/../../Fixtures/mysql.sql');
125136
assert($sql !== false);
126137

127-
foreach (array_filter(array_map('trim', explode(';', $sql))) as $query) {
128-
$connection->query('%raw', $query);
138+
foreach (array_filter(array_map('trim', explode(';', $sql))) as $statement) {
139+
$connection->query('%raw', $statement);
129140
}
130141
}
131142

tests/Cases/Exceptions.phpt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,37 @@
22

33
use Contributte\Nextras\Orm\QueryObject\Exception\InvalidHydrationModeException;
44
use Contributte\Nextras\Orm\QueryObject\Exception\InvalidObjectCreationException;
5+
use Contributte\Tester\Toolkit;
56
use Tester\Assert;
67

78
require_once __DIR__ . '/../bootstrap.php';
89

9-
test('InvalidHydrationModeException extends LogicException', function (): void {
10+
// Test: InvalidHydrationModeException extends LogicException
11+
Toolkit::test(static function (): void {
1012
$exception = new InvalidHydrationModeException('Test message');
1113

1214
Assert::type(LogicException::class, $exception);
1315
Assert::same('Test message', $exception->getMessage());
1416
});
1517

16-
test('InvalidObjectCreationException extends LogicException', function (): void {
18+
// Test: InvalidObjectCreationException extends LogicException
19+
Toolkit::test(static function (): void {
1720
$exception = new InvalidObjectCreationException('Creation failed');
1821

1922
Assert::type(LogicException::class, $exception);
2023
Assert::same('Creation failed', $exception->getMessage());
2124
});
2225

23-
test('InvalidHydrationModeException can be thrown and caught', function (): void {
24-
Assert::exception(function (): void {
26+
// Test: InvalidHydrationModeException can be thrown and caught
27+
Toolkit::test(static function (): void {
28+
Assert::exception(static function (): void {
2529
throw new InvalidHydrationModeException('Invalid hydration mode "99"');
2630
}, InvalidHydrationModeException::class, 'Invalid hydration mode "99"');
2731
});
2832

29-
test('InvalidObjectCreationException can be thrown and caught', function (): void {
30-
Assert::exception(function (): void {
33+
// Test: InvalidObjectCreationException can be thrown and caught
34+
Toolkit::test(static function (): void {
35+
Assert::exception(static function (): void {
3136
throw new InvalidObjectCreationException('Created object must be typed of QueryObject');
3237
}, InvalidObjectCreationException::class, 'Created object must be typed of QueryObject');
3338
});

tests/Cases/ExecutableQueryObject.phpt

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

33
use Contributte\Nextras\Orm\QueryObject\ExecutableQueryObject;
4+
use Contributte\Tester\Toolkit;
45
use Nextras\Dbal\Connection;
56
use Nextras\Dbal\QueryBuilder\QueryBuilder;
67
use Nextras\Dbal\Result\Result;
@@ -48,7 +49,8 @@ class TestExecutableQueryObjectWithPostResult extends ExecutableQueryObject
4849

4950
}
5051

51-
test('ExecutableQueryObject builds query via fetch method', function (): void {
52+
// Test: ExecutableQueryObject builds query via fetch method
53+
Toolkit::test(static function (): void {
5254
$connection = Mockery::mock(Connection::class);
5355
$queryBuilder = Mockery::mock(QueryBuilder::class);
5456

@@ -70,7 +72,8 @@ test('ExecutableQueryObject builds query via fetch method', function (): void {
7072
Mockery::close();
7173
});
7274

73-
test('ExecutableQueryObject execute method uses connection', function (): void {
75+
// Test: ExecutableQueryObject execute method uses connection
76+
Toolkit::test(static function (): void {
7477
$connection = Mockery::mock(Connection::class);
7578
$queryBuilder = Mockery::mock(QueryBuilder::class);
7679
$result = Mockery::mock(Result::class);

tests/Cases/QueryObject.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php declare(strict_types = 1);
22

3+
use Contributte\Tester\Toolkit;
34
use Nextras\Dbal\Platforms\IPlatform;
45
use Nextras\Dbal\QueryBuilder\QueryBuilder;
56
use Tester\Assert;
67
use Tests\Mocks\SimpleQueryObject;
78

89
require_once __DIR__ . '/../bootstrap.php';
910

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

tests/Cases/QueryObjectContextAwareManager.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Contributte\Nextras\Orm\QueryObject\Exception\InvalidObjectCreationException;
44
use Contributte\Nextras\Orm\QueryObject\QueryObject;
55
use Contributte\Nextras\Orm\QueryObject\QueryObjectContextAwareManager;
6+
use Contributte\Tester\Toolkit;
67
use Nette\DI\Container;
78
use Nextras\Dbal\Connection;
89
use Nextras\Dbal\QueryBuilder\QueryBuilder;
@@ -12,7 +13,8 @@ use Tests\Mocks\SimpleQueryObject;
1213

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

15-
test('QueryObjectContextAwareManager create returns QueryObject', function (): void {
16+
// Test: QueryObjectContextAwareManager create returns QueryObject
17+
Toolkit::test(static function (): void {
1618
$queryObject = new SimpleQueryObject();
1719

1820
$container = Mockery::mock(Container::class);
@@ -30,7 +32,8 @@ test('QueryObjectContextAwareManager create returns QueryObject', function (): v
3032
Mockery::close();
3133
});
3234

33-
test('QueryObjectContextAwareManager create throws InvalidObjectCreationException for non-QueryObject', function (): void {
35+
// Test: QueryObjectContextAwareManager create throws InvalidObjectCreationException for non-QueryObject
36+
Toolkit::test(static function (): void {
3437
$nonQueryObject = new stdClass();
3538

3639
$container = Mockery::mock(Container::class);
@@ -41,14 +44,15 @@ test('QueryObjectContextAwareManager create throws InvalidObjectCreationExceptio
4144

4245
$manager = new QueryObjectContextAwareManager($container);
4346

44-
Assert::exception(function () use ($manager): void {
47+
Assert::exception(static function () use ($manager): void {
4548
$manager->create(stdClass::class);
4649
}, InvalidObjectCreationException::class);
4750

4851
Mockery::close();
4952
});
5053

51-
test('QueryObjectContextAwareManager fetch returns Result', function (): void {
54+
// Test: QueryObjectContextAwareManager fetch returns Result
55+
Toolkit::test(static function (): void {
5256
$connection = Mockery::mock(Connection::class);
5357
$queryBuilder = Mockery::mock(QueryBuilder::class);
5458
$result = Mockery::mock(Result::class);

tests/Cases/QueryObjectExtension.phpt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
use Contributte\Nextras\Orm\QueryObject\DI\NextrasQueryObjectExtension;
44
use Contributte\Nextras\Orm\QueryObject\QueryObjectManager;
5+
use Contributte\Tester\Environment;
6+
use Contributte\Tester\Toolkit;
57
use Nette\DI\Compiler;
68
use Nette\DI\Container;
79
use Nette\DI\ContainerLoader;
810
use Tester\Assert;
911

1012
require_once __DIR__ . '/../bootstrap.php';
1113

12-
test('NextrasQueryObjectExtension registers QueryObjectManager service', function (): void {
13-
$loader = new ContainerLoader(TEMP_DIR);
14-
$class = $loader->load(function (Compiler $compiler): void {
14+
Toolkit::test(static function (): void {
15+
$loader = new ContainerLoader(Environment::getTestDir());
16+
$class = $loader->load(static function (Compiler $compiler): void {
1517
$compiler->addExtension('nextrasqueryobject', new NextrasQueryObjectExtension());
1618
}, microtime());
1719

tests/Cases/Queryable.phpt

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

33
use Contributte\Nextras\Orm\QueryObject\Queryable;
4+
use Contributte\Tester\Toolkit;
45
use Tester\Assert;
56

67
require_once __DIR__ . '/../bootstrap.php';
78

8-
test('Queryable interface defines HYDRATION_RESULTSET constant', function (): void {
9+
// Test: Queryable interface defines HYDRATION_RESULTSET constant
10+
Toolkit::test(static function (): void {
911
Assert::same(1, Queryable::HYDRATION_RESULTSET);
1012
});
1113

12-
test('Queryable interface defines HYDRATION_ENTITY constant', function (): void {
14+
// Test: Queryable interface defines HYDRATION_ENTITY constant
15+
Toolkit::test(static function (): void {
1316
Assert::same(2, Queryable::HYDRATION_ENTITY);
1417
});

tests/Cases/TRepositoryQueryable.phpt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
use Contributte\Nextras\Orm\QueryObject\Exception\InvalidHydrationModeException;
44
use Contributte\Nextras\Orm\QueryObject\Queryable;
55
use Contributte\Nextras\Orm\QueryObject\Repository\TRepositoryQueryable;
6+
use Contributte\Tester\Toolkit;
67
use Nextras\Dbal\Connection;
78
use Nextras\Dbal\QueryBuilder\QueryBuilder;
89
use Nextras\Dbal\Result\Result;
10+
use Nextras\Orm\Collection\EmptyCollection;
911
use Nextras\Orm\Collection\ICollection;
1012
use Nextras\Orm\Mapper\Dbal\DbalMapper;
1113
use Tester\Assert;
1214
use Tests\Mocks\SimpleQueryObject;
13-
use Tests\Mocks\StubCollection;
1415

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

@@ -26,7 +27,8 @@ class TestRepository
2627

2728
}
2829

29-
test('TRepositoryQueryable injectConnection stores connection', function (): void {
30+
// Test: TRepositoryQueryable injectConnection stores connection
31+
Toolkit::test(static function (): void {
3032
$connection = Mockery::mock(Connection::class);
3133

3234
$repo = new TestRepository();
@@ -42,7 +44,8 @@ test('TRepositoryQueryable injectConnection stores connection', function (): voi
4244
Mockery::close();
4345
});
4446

45-
test('TRepositoryQueryable fetch with HYDRATION_RESULTSET returns Result', function (): void {
47+
// Test: TRepositoryQueryable fetch with HYDRATION_RESULTSET returns Result
48+
Toolkit::test(static function (): void {
4649
$connection = Mockery::mock(Connection::class);
4750
$queryBuilder = Mockery::mock(QueryBuilder::class);
4851
$result = Mockery::mock(Result::class);
@@ -85,11 +88,13 @@ test('TRepositoryQueryable fetch with HYDRATION_RESULTSET returns Result', funct
8588
Mockery::close();
8689
});
8790

88-
test('TRepositoryQueryable fetch with HYDRATION_ENTITY returns ICollection', function (): void {
91+
// Test: TRepositoryQueryable fetch with HYDRATION_ENTITY returns ICollection
92+
Toolkit::test(static function (): void {
8993
$connection = Mockery::mock(Connection::class);
9094
$queryBuilder = Mockery::mock(QueryBuilder::class);
9195
$mapper = Mockery::mock(DbalMapper::class);
92-
$collection = new StubCollection();
96+
97+
$collection = new EmptyCollection();
9398

9499
$queryBuilder->shouldReceive('select')
95100
->with('*')
@@ -122,7 +127,8 @@ test('TRepositoryQueryable fetch with HYDRATION_ENTITY returns ICollection', fun
122127
Mockery::close();
123128
});
124129

125-
test('TRepositoryQueryable fetch throws InvalidHydrationModeException for invalid mode', function (): void {
130+
// Test: TRepositoryQueryable fetch throws InvalidHydrationModeException for invalid mode
131+
Toolkit::test(static function (): void {
126132
$connection = Mockery::mock(Connection::class);
127133
$queryBuilder = Mockery::mock(QueryBuilder::class);
128134

@@ -145,7 +151,7 @@ test('TRepositoryQueryable fetch throws InvalidHydrationModeException for invali
145151

146152
$queryObject = new SimpleQueryObject();
147153

148-
Assert::exception(function () use ($repo, $queryObject): void {
154+
Assert::exception(static function () use ($repo, $queryObject): void {
149155
$repo->fetch($queryObject, 999);
150156
}, InvalidHydrationModeException::class, 'Invalid hydration mode "999"');
151157

0 commit comments

Comments
 (0)