1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace AutoMapper \Tests \Doctrine ;
6
+
7
+ use AutoMapper \Tests \AutoMapperTestCase ;
8
+ use AutoMapper \Tests \Doctrine \Entity \Book ;
9
+ use Doctrine \DBAL \DriverManager ;
10
+ use Doctrine \ORM \EntityManager ;
11
+ use Doctrine \ORM \EntityManagerInterface ;
12
+ use Doctrine \ORM \ORMSetup ;
13
+ use Doctrine \ORM \Tools \SchemaTool ;
14
+
15
+ /**
16
+ * @author Joel Wurtz <[email protected] >
17
+ */
18
+ class DoctrineTest extends AutoMapperTestCase
19
+ {
20
+ private EntityManagerInterface $ entityManager ;
21
+
22
+ protected function setUp (): void
23
+ {
24
+ parent ::setUp ();
25
+ $ this ->buildDatabase ();
26
+ }
27
+
28
+ private function buildDatabase () {
29
+ // delete the database file
30
+ if (file_exists (__DIR__ . '/db.sqlite ' )) {
31
+ unlink (__DIR__ . '/db.sqlite ' );
32
+ }
33
+
34
+ $ config = ORMSetup::createAttributeMetadataConfiguration (
35
+ paths: [__DIR__ . '/Entity ' ],
36
+ isDevMode: true ,
37
+ );
38
+
39
+ $ connection = DriverManager::getConnection ([
40
+ 'driver ' => 'pdo_sqlite ' ,
41
+ 'path ' => __DIR__ . '/db.sqlite ' ,
42
+ ], $ config );
43
+
44
+ $ entityManager = new EntityManager ($ connection , $ config );
45
+
46
+ // Generate schema
47
+ $ schemaTool = new SchemaTool ($ entityManager );
48
+ $ schemaTool ->createSchema ($ entityManager ->getMetadataFactory ()->getAllMetadata ());
49
+
50
+ $ this ->entityManager = $ entityManager ;
51
+ }
52
+
53
+ public function testAutoMapping (): void
54
+ {
55
+ $ book = new Book ();
56
+
57
+ $ this ->entityManager ->persist ($ book );
58
+ $ this ->entityManager ->flush ();
59
+
60
+ $ this ->assertNotNull ($ book ->id );
61
+
62
+ $ bookArray = $ this ->autoMapper ->map ($ book , 'array ' );
63
+ $ bookArray ['author ' ] = 'John Doe ' ;
64
+
65
+ $ this ->autoMapper ->map ($ bookArray , Book::class);
66
+ $ this ->entityManager ->flush ();
67
+ $ this ->entityManager ->clear ();
68
+
69
+ $ book = $ this ->entityManager ->find (Book::class, $ book ->id );
70
+
71
+ $ this ->assertEquals ('John Doe ' , $ book ->author );
72
+ }
73
+ }
0 commit comments