|
3 | 3 | namespace MintyPHP\Tests\Core; |
4 | 4 |
|
5 | 5 | use MintyPHP\Core\DB; |
| 6 | +use MintyPHP\Core\Cache; |
6 | 7 | use MintyPHP\Core\Orm; |
7 | 8 | use PHPUnit\Framework\TestCase; |
8 | 9 |
|
|
16 | 17 | class OrmTest extends TestCase |
17 | 18 | { |
18 | 19 | private static DB $db; |
| 20 | + private static Cache $cache; |
19 | 21 | private static Orm $orm; |
20 | 22 |
|
21 | 23 | public static function setUpBeforeClass(): void |
22 | 24 | { |
23 | 25 | // Create database connection |
24 | 26 | self::$db = new DB(null, 'mintyphp_test', 'mintyphp_test', 'mintyphp_test', null, null); |
25 | 27 |
|
| 28 | + // Create cache instance |
| 29 | + self::$cache = new Cache('mintyphp_test_', 'localhost:11211'); |
| 30 | + |
26 | 31 | // Create Core ORM instance |
27 | | - self::$orm = new Orm(self::$db); |
| 32 | + self::$orm = new Orm(self::$db, self::$cache); |
28 | 33 | } |
29 | 34 |
|
30 | 35 | public function testDropPostsBefore(): void |
@@ -202,6 +207,73 @@ public function testSelectPostNotFound(): void |
202 | 207 | * @depends testInsertPosts |
203 | 208 | * @depends testUpdatePosts |
204 | 209 | */ |
| 210 | + public function testPathQueryWithoutHints(): void |
| 211 | + { |
| 212 | + // Test automatic path inference - posts with their users |
| 213 | + $result = self::$orm->path( |
| 214 | + 'SELECT p.id, p.slug, u.id, u.username |
| 215 | + FROM posts p |
| 216 | + JOIN users u ON p.user_id = u.id |
| 217 | + WHERE p.id <= ? |
| 218 | + ORDER BY p.id', |
| 219 | + [2] |
| 220 | + ); |
| 221 | + |
| 222 | + // Should return nested structure with users as objects within posts array |
| 223 | + $this->assertCount(2, $result); |
| 224 | + assert(is_array($result[0]), 'result[0] is not an array'); |
| 225 | + $this->assertEquals(1, $result[0]['id']); |
| 226 | + $this->assertEquals('2014-08-test1', $result[0]['slug']); |
| 227 | + $this->assertArrayHasKey('u', $result[0]); |
| 228 | + $this->assertIsArray($result[0]['u']); |
| 229 | + $this->assertEquals(1, $result[0]['u']['id']); |
| 230 | + $this->assertEquals('test1', $result[0]['u']['username']); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * @depends testDropPostsBefore |
| 235 | + * @depends testDropUsersBefore |
| 236 | + * @depends testCreateUsers |
| 237 | + * @depends testCreatePosts |
| 238 | + * @depends testInsertUsers |
| 239 | + * @depends testInsertPosts |
| 240 | + * @depends testUpdatePosts |
| 241 | + */ |
| 242 | + public function testPathQueryWithHints(): void |
| 243 | + { |
| 244 | + // Test with custom path hints for cleaner structure |
| 245 | + $result = self::$orm->path( |
| 246 | + 'SELECT p.id, p.slug, p.title, u.id, u.username |
| 247 | + FROM posts p |
| 248 | + JOIN users u ON p.user_id = u.id |
| 249 | + WHERE p.id = ?', |
| 250 | + [1], |
| 251 | + [ |
| 252 | + 'p' => '$', |
| 253 | + 'u' => '$.author' |
| 254 | + ] |
| 255 | + ); |
| 256 | + |
| 257 | + // Should return single object with custom 'author' field |
| 258 | + $this->assertEquals(1, $result['id']); |
| 259 | + $this->assertEquals('2014-08-test1', $result['slug']); |
| 260 | + $this->assertEquals('test', $result['title']); |
| 261 | + $this->assertArrayHasKey('author', $result); |
| 262 | + $this->assertIsArray($result['author']); |
| 263 | + $this->assertEquals(1, $result['author']['id']); |
| 264 | + $this->assertEquals('test1', $result['author']['username']); |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * @depends testDropPostsBefore |
| 269 | + * @depends testDropUsersBefore |
| 270 | + * @depends testCreateUsers |
| 271 | + * @depends testCreatePosts |
| 272 | + * @depends testInsertUsers |
| 273 | + * @depends testInsertPosts |
| 274 | + * @depends testUpdatePosts |
| 275 | + * @depends testDeletePosts |
| 276 | + */ |
205 | 277 | public function testDeletePosts(): void |
206 | 278 | { |
207 | 279 | $result = self::$orm->delete('posts', 1); |
|
0 commit comments