1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace OpenMage \Tests \Unit \Core \Helper ;
5+
6+ use PHPUnit \Framework \TestCase ;
7+ use \Varien_Db_Adapter_Pdo_Mysql ;
8+
9+ class VarienDbAdapterPdoMysqlTest extends TestCase
10+ {
11+ private Varien_Db_Adapter_Pdo_Mysql $ adapter ;
12+
13+ protected function setUp (): void
14+ {
15+ $ config = [
16+ 'host ' => 'localhost ' ,
17+ 'username ' => 'user ' ,
18+ 'password ' => 'password ' ,
19+ 'dbname ' => 'test_db ' ,
20+ 'type ' => 'pdo_mysql ' ,
21+ 'active ' => '1 ' ,
22+ ];
23+
24+ // Create a mock object for Varien_Db_Adapter_Pdo_Mysql
25+ $ this ->adapter = $ this ->getMockBuilder (Varien_Db_Adapter_Pdo_Mysql::class)
26+ ->disableOriginalConstructor ()
27+ ->getMock ();
28+
29+ // Call the constructor manually with our config
30+ $ reflectedAdapter = new \ReflectionClass (Varien_Db_Adapter_Pdo_Mysql::class);
31+ $ constructor = $ reflectedAdapter ->getConstructor ();
32+ $ constructor ->invoke ($ this ->adapter , $ config );
33+ }
34+
35+ public function testGetHostInfoWithUnixSocket (): void
36+ {
37+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
38+ $ method ->setAccessible (true );
39+
40+ $ fakeSocket = '/var/run/mysqld/mysqld.sock ' ;
41+ $ hostInfo = $ method ->invoke ($ this ->adapter , $ fakeSocket );
42+
43+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_UNIX_SOCKET );
44+ $ this ->assertEquals ($ hostInfo ->getUnixSocket (), $ fakeSocket );
45+ $ this ->assertNull ($ hostInfo ->getHostName ());
46+ $ this ->assertNull ($ hostInfo ->getPort ());
47+ }
48+
49+ public function testGetHostInfoWithIpv4Address (): void
50+ {
51+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
52+ $ method ->setAccessible (true );
53+
54+ $ hostInfo = $ method ->invoke ($ this ->adapter , '192.168.1.1:3306 ' );
55+
56+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV4_ADDRESS );
57+ $ this ->assertEquals ('192.168.1.1 ' , $ hostInfo ->getHostName ());
58+ $ this ->assertEquals ('3306 ' , $ hostInfo ->getPort ());
59+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
60+ }
61+
62+ public function testGetHostInfoWithIpv4AddressWithoutPort (): void
63+ {
64+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
65+ $ method ->setAccessible (true );
66+
67+ $ hostInfo = $ method ->invoke ($ this ->adapter , '192.168.1.1 ' );
68+
69+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV4_ADDRESS );
70+ $ this ->assertEquals ('192.168.1.1 ' , $ hostInfo ->getHostName ());
71+ $ this ->assertNull ($ hostInfo ->getPort ());
72+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
73+ }
74+
75+ public function testGetHostInfoWithHostname (): void
76+ {
77+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
78+ $ method ->setAccessible (true );
79+
80+ $ hostInfo = $ method ->invoke ($ this ->adapter , 'db.example.com:3306 ' );
81+
82+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_HOSTNAME );
83+ $ this ->assertEquals ('db.example.com ' , $ hostInfo ->getHostName ());
84+ $ this ->assertEquals ('3306 ' , $ hostInfo ->getPort ());
85+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
86+ }
87+
88+ public function testGetHostInfoWithHostnameWithoutPort (): void
89+ {
90+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
91+ $ method ->setAccessible (true );
92+
93+ $ hostInfo = $ method ->invoke ($ this ->adapter , 'db.example.com ' );
94+
95+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_HOSTNAME );
96+ $ this ->assertEquals ('db.example.com ' , $ hostInfo ->getHostName ());
97+ $ this ->assertNull ($ hostInfo ->getPort ());
98+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
99+ }
100+
101+ public function testGetHostInfoWithIpv6Address (): void
102+ {
103+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
104+ $ method ->setAccessible (true );
105+
106+ $ hostInfo = $ method ->invoke ($ this ->adapter , '[2001:db8::1]:3306 ' );
107+
108+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS );
109+ $ this ->assertEquals ('2001:db8::1 ' , $ hostInfo ->getHostName ());
110+ $ this ->assertEquals ('3306 ' , $ hostInfo ->getPort ());
111+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
112+ }
113+
114+ public function testGetHostInfoWithIpv6AddressWithoutPort (): void
115+ {
116+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
117+ $ method ->setAccessible (true );
118+
119+ $ hostInfo = $ method ->invoke ($ this ->adapter , '2001:db8::1 ' );
120+
121+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS );
122+ $ this ->assertEquals ('2001:db8::1 ' , $ hostInfo ->getHostName ());
123+ $ this ->assertNull ($ hostInfo ->getPort ());
124+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
125+ }
126+
127+ public function testGetHostInfoWithIpv6AddressWithZoneId (): void
128+ {
129+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
130+ $ method ->setAccessible (true );
131+
132+ $ hostInfo = $ method ->invoke ($ this ->adapter , '[fe80::1%eth0]:3306 ' );
133+
134+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS );
135+ $ this ->assertEquals ('fe80::1%eth0 ' , $ hostInfo ->getHostName ());
136+ $ this ->assertEquals ('3306 ' , $ hostInfo ->getPort ());
137+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
138+ }
139+
140+ public function testGetHostInfoWithIpv6AddressWithZoneIdWithoutPort (): void
141+ {
142+ $ method = new \ReflectionMethod (Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo ' );
143+ $ method ->setAccessible (true );
144+
145+ $ hostInfo = $ method ->invoke ($ this ->adapter , 'fe80::1%eth0 ' );
146+
147+ $ this ->assertEquals ($ hostInfo ->getAddressType (), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS );
148+ $ this ->assertEquals ('fe80::1%eth0 ' , $ hostInfo ->getHostName ());
149+ $ this ->assertNull ($ hostInfo ->getPort ());
150+ $ this ->assertNull ($ hostInfo ->getUnixSocket ());
151+ }
152+ }
0 commit comments