@@ -103,4 +103,60 @@ public function test_current_schema_version_is_not_autoloaded() {
103103 'The schema version should not be autoloaded. '
104104 );
105105 }
106+
107+ /**
108+ * Test that the generated database schema contains the expected indexes.
109+ *
110+ * @dataProvider table_index_provider()
111+ */
112+ public function test_database_indexes ( $ non_unique , $ key_name , $ column_name ) {
113+ global $ wpdb ;
114+
115+ WC_Custom_Order_Table_Install::activate ();
116+
117+ $ table = wc_custom_order_table ()->get_table_name ();
118+ $ indexes = $ wpdb ->get_results ( "SHOW INDEX FROM $ table " , ARRAY_A );
119+ $ search = array (
120+ 'Non_unique ' => $ non_unique ,
121+ 'Key_name ' => $ key_name ,
122+ 'Column_name ' => $ column_name ,
123+ );
124+
125+ // Find the index by name.
126+ foreach ( $ indexes as $ index ) {
127+ if ( $ index ['Key_name ' ] !== $ key_name ) {
128+ continue ;
129+ }
130+
131+ $ this ->assertEquals (
132+ $ non_unique ,
133+ $ index ['Non_unique ' ],
134+ sprintf (
135+ 'Did not match expected non-uniqueness (Received %d, expected %d ' ,
136+ $ non_unique ,
137+ $ index ['Non_unique ' ]
138+ )
139+ );
140+
141+ $ this ->assertEquals (
142+ $ column_name ,
143+ $ index ['Column_name ' ],
144+ sprintf ( 'Expected index "%s" on column %s. ' , $ key_name , $ column_name )
145+ );
146+
147+ // We've checked the index we've come to check, return early.
148+ return ;
149+ }
150+
151+ $ this ->fail ( sprintf ( 'Could not find an index with name "%s". ' , $ key_name ) );
152+ }
153+
154+ public function table_index_provider () {
155+ return array (
156+ 'Primary key ' => array ( 0 , 'PRIMARY ' , 'order_id ' ),
157+ 'Order key ' => array ( 0 , 'order_key ' , 'order_key ' ),
158+ 'Customer ID ' => array ( 1 , 'customer_id ' , 'customer_id ' ),
159+ 'Order total ' => array ( 1 , 'order_total ' , 'total ' ),
160+ );
161+ }
106162}
0 commit comments