@@ -16,59 +16,28 @@ abstract class RenamedPKTest extends BaseTest
16
16
{
17
17
use TableTrait;
18
18
19
- public function setUp (): void
20
- {
21
- parent ::setUp ();
22
-
23
- $ this ->makeTable (
24
- 'simple_entity ' ,
25
- [
26
- 'identity_id ' => 'primary ' ,
27
- 'identity_key ' => 'integer,nullable ' ,
28
- ]
29
- );
30
-
31
- $ this ->orm = $ this ->withSchema (
32
- new Schema (
33
- [
34
- Identity::class => [
35
- Schema::ROLE => 'simple_entity ' ,
36
- Schema::DATABASE => 'default ' ,
37
- Schema::TABLE => 'simple_entity ' ,
38
- Schema::MAPPER => Mapper::class,
39
- Schema::PRIMARY_KEY => 'id ' ,
40
- Schema::COLUMNS => [
41
- 'id ' => 'identity_id ' ,
42
- 'key ' => 'identity_key ' ,
43
- ],
44
- Schema::TYPECAST => [
45
- 'id ' => 'int ' ,
46
- 'key ' => 'int ' ,
47
- ],
48
- Schema::SCHEMA => [],
49
- Schema::RELATIONS => []
50
- ]
51
- ]
52
- )
53
- );
54
- }
55
-
56
19
public function testCreateEmpty (): void
57
20
{
21
+ $ this ->createTable1 ();
22
+ $ this ->orm = $ this ->withSchema (new Schema ($ this ->getSchemaArray1 ()));
23
+
58
24
$ u = new Identity ();
59
25
60
- ( new Transaction ( $ this ->orm ))-> persist ($ u)-> run ( );
26
+ $ this ->save ($ u );
61
27
62
28
$ this ->assertIsInt ($ u ->getId ());
63
29
}
64
30
65
31
public function testCreateWithPredefinedId (): void
66
32
{
33
+ $ this ->createTable1 ();
34
+ $ this ->orm = $ this ->withSchema (new Schema ($ this ->getSchemaArray1 ()));
35
+
67
36
$ u = new Identity ();
68
37
$ u ->setId (2 );
69
38
$ u ->setKey (42 );
70
39
71
- ( new Transaction ( $ this ->orm ))-> persist ($ u)-> run ( );
40
+ $ this ->save ($ u );
72
41
73
42
$ s = new Select ($ this ->orm ->withHeap (new Heap ()), Identity::class);
74
43
$ data = $ s ->fetchData ();
@@ -77,4 +46,80 @@ public function testCreateWithPredefinedId(): void
77
46
$ this ->assertSame (2 , $ u ->getId ());
78
47
$ this ->assertSame (42 , $ u ->getKey ());
79
48
}
49
+
50
+ public function testChangePK (): void
51
+ {
52
+ $ this ->createTable2 ();
53
+ $ this ->orm = $ this ->withSchema (new Schema ($ this ->getSchemaArray1 ()));
54
+
55
+ $ u = new Identity ();
56
+ $ u ->setId (5 );
57
+ $ u ->setKey (42 );
58
+
59
+ $ this ->save ($ u );
60
+
61
+ $ this ->orm = $ this ->orm ->withHeap (new Heap ());
62
+ $ data = (new Select ($ this ->orm , Identity::class))
63
+ ->fetchAll ();
64
+ $ this ->assertSame (1 , count ($ data ));
65
+ $ u = $ data [0 ];
66
+ $ u ->setId (8 );
67
+
68
+ $ this ->captureWriteQueries ();
69
+ $ this ->save ($ u );
70
+ $ this ->assertNumWrites (1 );
71
+
72
+ $ this ->orm = $ this ->orm ->withHeap (new Heap ());
73
+ $ data = (new Select ($ this ->orm , Identity::class))
74
+ ->fetchAll ();
75
+
76
+ $ this ->assertSame (1 , count ($ data ));
77
+ $ this ->assertSame (8 , $ data [0 ]->getId ());
78
+ }
79
+
80
+ private function createTable2 (): void
81
+ {
82
+ $ this ->makeTable (
83
+ 'simple_entity ' ,
84
+ [
85
+ 'identity_id ' => 'bigInteger ' ,
86
+ 'identity_key ' => 'integer,nullable ' ,
87
+ ]
88
+ );
89
+ }
90
+
91
+ private function createTable1 (): void
92
+ {
93
+ $ this ->makeTable (
94
+ 'simple_entity ' ,
95
+ [
96
+ 'identity_id ' => 'primary ' ,
97
+ 'identity_key ' => 'integer,nullable ' ,
98
+ ]
99
+ );
100
+ }
101
+
102
+ private function getSchemaArray1 (): array
103
+ {
104
+
105
+ return [
106
+ Identity::class => [
107
+ Schema::ROLE => 'simple_entity ' ,
108
+ Schema::DATABASE => 'default ' ,
109
+ Schema::TABLE => 'simple_entity ' ,
110
+ Schema::MAPPER => Mapper::class,
111
+ Schema::PRIMARY_KEY => 'id ' ,
112
+ Schema::COLUMNS => [
113
+ 'id ' => 'identity_id ' ,
114
+ 'key ' => 'identity_key ' ,
115
+ ],
116
+ Schema::TYPECAST => [
117
+ 'id ' => 'int ' ,
118
+ 'key ' => 'int ' ,
119
+ ],
120
+ Schema::SCHEMA => [],
121
+ Schema::RELATIONS => []
122
+ ]
123
+ ];
124
+ }
80
125
}
0 commit comments