Skip to content

Commit faf92c8

Browse files
committed
Add setPreserveSequence to Mirror and test invalid sequence validation
- Add setPreserveSequence delegation to Mirror.php - Add test case for invalid $sequence value throwing StructureException
1 parent b0586bd commit faf92c8

File tree

3 files changed

+262
-0
lines changed

3 files changed

+262
-0
lines changed

TESTING_STEPS.md

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
# Testing Steps
2+
3+
This document outlines how to set up, run tests, and use code quality tools for the Utopia Database library.
4+
5+
## Prerequisites
6+
7+
- Docker and Docker Compose installed
8+
- Composer (optional, for local dependency management)
9+
10+
## 1. Install Dependencies
11+
12+
```bash
13+
composer install --prefer-dist
14+
```
15+
16+
Or using Docker:
17+
18+
```bash
19+
docker run --rm -v "$(pwd)":/app -w /app composer:latest install --prefer-dist --ignore-platform-reqs
20+
```
21+
22+
## 2. Build Docker Images
23+
24+
```bash
25+
composer build
26+
```
27+
28+
Or directly:
29+
30+
```bash
31+
docker compose build
32+
```
33+
34+
## 3. Start Services
35+
36+
Start all database containers and the test environment:
37+
38+
```bash
39+
composer start
40+
```
41+
42+
Or directly:
43+
44+
```bash
45+
docker compose up -d
46+
```
47+
48+
This starts the following services:
49+
50+
| Service | Container Name | Port | Description |
51+
|-------------------|------------------------|-------|--------------------------|
52+
| PostgreSQL | utopia-postgres | 8701 | Primary PostgreSQL |
53+
| PostgreSQL Mirror | utopia-postgres-mirror | 8702 | PostgreSQL replica |
54+
| MariaDB | utopia-mariadb | 8703 | Primary MariaDB |
55+
| MariaDB Mirror | utopia-mariadb-mirror | 8704 | MariaDB replica |
56+
| MySQL | utopia-mysql | 8706 | Primary MySQL |
57+
| MySQL Mirror | utopia-mysql-mirror | 8707 | MySQL replica |
58+
| Redis | utopia-redis | 8708 | Primary Redis |
59+
| Redis Mirror | utopia-redis-mirror | 8709 | Redis replica |
60+
| MongoDB | utopia-mongo | 9706 | MongoDB with replica set |
61+
| Adminer | utopia-adminer | 8700 | Database admin UI |
62+
| Mongo Express | mongo-express | 8083 | MongoDB admin UI |
63+
64+
## 4. Run Tests
65+
66+
Run the full test suite:
67+
68+
```bash
69+
composer test
70+
```
71+
72+
Or directly:
73+
74+
```bash
75+
docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml
76+
```
77+
78+
### Run Specific Tests
79+
80+
Run a specific test file:
81+
82+
```bash
83+
docker compose exec tests vendor/bin/phpunit --filter DatabaseTest
84+
```
85+
86+
Run a specific test method:
87+
88+
```bash
89+
docker compose exec tests vendor/bin/phpunit --filter testCreateDocument
90+
```
91+
92+
## 5. Code Quality Tools
93+
94+
### Linting (Pint - PSR-12)
95+
96+
Check code style:
97+
98+
```bash
99+
composer lint
100+
```
101+
102+
Or directly:
103+
104+
```bash
105+
docker compose exec tests php -d memory_limit=2G ./vendor/bin/pint --test
106+
```
107+
108+
### Format Code
109+
110+
Auto-fix code style issues:
111+
112+
```bash
113+
composer format
114+
```
115+
116+
Or directly:
117+
118+
```bash
119+
docker compose exec tests php -d memory_limit=2G ./vendor/bin/pint
120+
```
121+
122+
### Static Analysis (PHPStan)
123+
124+
Run PHPStan at level 7:
125+
126+
```bash
127+
composer check
128+
```
129+
130+
Or directly:
131+
132+
```bash
133+
docker compose exec tests ./vendor/bin/phpstan analyse --level 7 src tests --memory-limit 2G
134+
```
135+
136+
### Code Coverage
137+
138+
Generate and check code coverage (requires tests to have been run with coverage):
139+
140+
```bash
141+
composer coverage
142+
```
143+
144+
This checks that code coverage is at least 90%.
145+
146+
## 6. Stop Services
147+
148+
```bash
149+
docker compose down
150+
```
151+
152+
To also remove volumes:
153+
154+
```bash
155+
docker compose down -v
156+
```
157+
158+
## Quick Reference
159+
160+
| Command | Description |
161+
|--------------------|--------------------------------------|
162+
| `composer build` | Build Docker images |
163+
| `composer start` | Start all services |
164+
| `composer test` | Run PHPUnit tests |
165+
| `composer lint` | Check code style (PSR-12) |
166+
| `composer format` | Auto-fix code style |
167+
| `composer check` | Run PHPStan static analysis |
168+
| `composer coverage`| Check code coverage (min 90%) |
169+
170+
## Database Credentials
171+
172+
### PostgreSQL
173+
- Host: `postgres` (or `localhost:8701` from host)
174+
- User: `root`
175+
- Password: `password`
176+
- Database: `root`
177+
178+
### MariaDB
179+
- Host: `mariadb` (or `localhost:8703` from host)
180+
- User: `root`
181+
- Password: `password`
182+
183+
### MySQL
184+
- Host: `mysql` (or `localhost:8706` from host)
185+
- User: `root` / `user`
186+
- Password: `password`
187+
- Database: `default`
188+
189+
### MongoDB
190+
- Host: `mongo` (or `localhost:9706` from host)
191+
- User: `root`
192+
- Password: `password`
193+
- Database: `utopia_testing`
194+
195+
### Redis
196+
- Host: `redis` (or `localhost:8708` from host)
197+
- No authentication required
198+
199+
## Troubleshooting
200+
201+
### Services not healthy
202+
203+
If services fail health checks, check their logs:
204+
205+
```bash
206+
docker compose logs postgres
207+
docker compose logs mariadb
208+
docker compose logs mongo
209+
```
210+
211+
### Tests fail to connect
212+
213+
Ensure all services are healthy:
214+
215+
```bash
216+
docker compose ps
217+
```
218+
219+
All services should show `healthy` status.
220+
221+
### Permission issues
222+
223+
If you encounter permission issues with mounted volumes:
224+
225+
```bash
226+
sudo chown -R $USER:$USER .
227+
```

src/Database/Mirror.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ public function setPreserveDates(bool $preserve): static
139139
return $this;
140140
}
141141

142+
public function setPreserveSequence(bool $preserve): static
143+
{
144+
$this->delegate(__FUNCTION__, \func_get_args());
145+
146+
$this->preserveSequence = $preserve;
147+
148+
return $this;
149+
}
150+
142151
public function enableValidation(): static
143152
{
144153
$this->delegate(__FUNCTION__);

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,32 @@ public function testPreserveSequenceUpsert(): void
12861286
// Verify flag was reset after withPreserveSequence
12871287
$this->assertFalse($database->getPreserveSequence());
12881288

1289+
// Test: With preserveSequence=true, invalid $sequence should throw error (SQL adapters only)
1290+
$database->setPreserveSequence(true);
1291+
1292+
try {
1293+
$database->upsertDocuments($collectionName, [
1294+
new Document([
1295+
'$id' => 'doc1',
1296+
'$sequence' => 'abc', // Invalid sequence value
1297+
'$permissions' => [
1298+
Permission::read(Role::any()),
1299+
Permission::update(Role::any()),
1300+
],
1301+
'name' => 'Alice Invalid',
1302+
]),
1303+
]);
1304+
// Schemaless adapters may not validate sequence type, so only fail for schemaful
1305+
if ($database->getAdapter()->getSupportForAttributes()) {
1306+
$this->fail('Expected StructureException for invalid sequence');
1307+
}
1308+
} catch (Throwable $e) {
1309+
if ($database->getAdapter()->getSupportForAttributes()) {
1310+
$this->assertInstanceOf(StructureException::class, $e);
1311+
$this->assertStringContainsString('sequence', $e->getMessage());
1312+
}
1313+
}
1314+
12891315
$database->setPreserveSequence(false);
12901316
$database->deleteCollection($collectionName);
12911317
}

0 commit comments

Comments
 (0)