@@ -40,4 +40,94 @@ public function testQueryWithFragmentsAndReturning(): void
4040 $ this ->assertSameQuery (static ::QUERY_WITH_RETURNING_FRAGMENT , $ upsert );
4141 $ this ->assertSameParameters (['adam@email.com ' , 'Adam ' , 100 , null ], $ upsert );
4242 }
43+
44+ public function testReturningSingleValueFromDatabase (): void
45+ {
46+ $ schema = $ this ->schema ('foo ' );
47+ $ schema ->primary ('id ' );
48+ $ schema ->string ('name ' )->nullable (false );
49+ $ schema ->string ('email ' , 64 )->nullable (false );
50+ $ schema ->integer ('balance ' )->defaultValue (0 );
51+ $ schema ->index (['email ' ])->unique (true );
52+ $ schema ->save ();
53+
54+ $ table = $ this ->database ->table ('foo ' );
55+
56+ $ this ->assertTrue ($ table ->exists ());
57+ $ this ->assertSame (0 , $ table ->count ());
58+
59+ $ email = $ table ->upsert ()
60+ ->conflicts ('email ' )
61+ ->values ([
62+ 'email ' => 'adam@email.com ' ,
63+ 'name ' => 'Adam ' ,
64+ 'balance ' => 100 ,
65+ ])
66+ ->returning ('email ' )
67+ ->run ();
68+
69+ $ this ->assertSame ('adam@email.com ' , $ email );
70+ }
71+
72+ public function testReturningMultipleValuesFromDatabase (): void
73+ {
74+ $ schema = $ this ->schema ('foo ' );
75+ $ schema ->primary ('id ' );
76+ $ schema ->string ('name ' )->nullable (false );
77+ $ schema ->string ('email ' , 64 )->nullable (false );
78+ $ schema ->integer ('balance ' )->defaultValue (0 );
79+ $ schema ->index (['email ' ])->unique (true );
80+ $ schema ->save ();
81+
82+ $ table = $ this ->database ->table ('foo ' );
83+
84+ $ this ->assertTrue ($ table ->exists ());
85+ $ this ->assertSame (0 , $ table ->count ());
86+
87+ $ result = $ table ->upsert ()
88+ ->conflicts ('email ' )
89+ ->values ([
90+ 'email ' => 'adam@email.com ' ,
91+ 'name ' => 'Adam ' ,
92+ 'balance ' => 100 ,
93+ ])
94+ ->returning ('email ' , 'name ' , 'balance ' )
95+ ->run ();
96+
97+ $ this ->assertSame ('adam@email.com ' , $ result ['email ' ]);
98+ $ this ->assertSame ('Adam ' , $ result ['name ' ]);
99+ $ this ->assertSame ('100 ' , $ result ['balance ' ]);
100+ }
101+
102+ public function testEmptyStringReturnedWithoutPrimaryKeyAndReturningValues (): void
103+ {
104+ $ schema = $ this ->schema ('bar ' );
105+ $ schema ->string ('name ' )->nullable (false );
106+ $ schema ->string ('email ' , 64 )->nullable (false );
107+ $ schema ->integer ('balance ' )->defaultValue (0 );
108+ $ schema ->index (['email ' ])->unique (true );
109+ $ schema ->save ();
110+
111+ $ table = $ this ->database ->table ('bar ' );
112+
113+ $ this ->assertTrue ($ table ->exists ());
114+ $ this ->assertSame (0 , $ table ->count ());
115+
116+ $ result = $ table ->upsert ()
117+ ->conflicts ('email ' )
118+ ->values ([
119+ 'email ' => 'adam@email.com ' ,
120+ 'name ' => 'Adam ' ,
121+ 'balance ' => 100 ,
122+ ])
123+ ->run ();
124+
125+ $ this ->assertSame ('' , $ result );
126+ $ this ->assertEquals (
127+ [
128+ ['email ' => 'adam@email.com ' , 'name ' => 'Adam ' , 'balance ' => 100 ],
129+ ],
130+ $ table ->select ()->fetchAll (),
131+ );
132+ }
43133}
0 commit comments