Permalink
Browse files

add some tests for scb_install_table()

  • Loading branch information...
1 parent 4aec8ec commit 245d93066916652aea06dd4e250b1bb47a2c9597 @scribu committed Apr 4, 2013
Showing with 132 additions and 1 deletion.
  1. +1 −1 scb
  2. +131 −0 tests/test-table.php
2 scb
@@ -1 +1 @@
-Subproject commit 5113202960a29ef7a8ee2100763eff9f4807a22c
+Subproject commit 2eb0f52094ce5e118a2dce5ddbc5f95c69f8c802
View
131 tests/test-table.php
@@ -0,0 +1,131 @@
+<?php
+
+class WPDB_Mock {
+
+ public $_query_log;
+
+ function query( $sql ) {
+ $this->_query_log .= $sql;
+ }
+
+ function __construct( $wpdb ) {
+ $this->wpdb = $wpdb;
+ }
+
+ function __get( $key ) {
+ return $this->wpdb->$key;
+ }
+
+ function __isset( $key ) {
+ return isset( $this->wpdb->$key );
+ }
+
+ function __call( $method, $args ) {
+ return call_user_func_array( array( $this->wpdb, $method ), $args );
+ }
+}
+
+
+class scb_Constraint_StringContains extends PHPUnit_Framework_Constraint {
+
+ /**
+ * @var string
+ */
+ protected $needle;
+
+ /**
+ * @param string $className
+ */
+ public function __construct( $needle )
+ {
+ $this->needle = $needle;
+ }
+
+ /**
+ * Evaluates the constraint for parameter $other. Returns TRUE if the
+ * constraint is met, FALSE otherwise.
+ *
+ * @param mixed $other Value or object to evaluate.
+ * @return bool
+ */
+ protected function matches( $haystack )
+ {
+ return false !== strpos( $haystack, $this->needle );
+ }
+
+ /**
+ * Returns the description of the failure
+ *
+ * The beginning of failure messages is "Failed asserting that" in most
+ * cases. This method should return the second part of that sentence.
+ *
+ * @param mixed $other Evaluated value or object.
+ * @return string
+ */
+ protected function failureDescription($other)
+ {
+ return sprintf(
+ '%s contains "%s"',
+
+ PHPUnit_Util_Type::shortenedExport($other),
+ $this->needle
+ );
+ }
+
+ /**
+ * Returns a string representation of the constraint.
+ *
+ * @return string
+ */
+ public function toString()
+ {
+ return sprintf(
+ 'contains "%s"',
+
+ $this->needle
+ );
+ }
+}
+
+
+class TableTest extends PHPUnit_Framework_TestCase {
+
+ function setUp() {
+ parent::setUp();
+
+ global $wpdb;
+
+ $wpdb = new WPDB_Mock( $wpdb );
+ }
+
+ function tearDown() {
+ parent::tearDown();
+
+ global $wpdb;
+
+ $wpdb = $wpdb->wpdb;
+ }
+
+ function test_install_table() {
+ scb_register_table( 'foo' );
+
+ scb_install_table( 'foo', 'ID int(20)' );
+ $this->assertQueryContains( 'ID int(20)' );
+
+ scb_install_table( 'foo', 'ID int(20)', 'delete_first' );
+ $this->assertQueryContains( 'ID int(20)' );
+
+ $table_opts = 'ENGINE InnoDB';
+ scb_install_table( 'foo', 'ID int(20)', array( 'table_options' => $table_opts ) );
+ $this->assertQueryContains( 'ID int(20)' );
+ $this->assertQueryContains( $table_opts );
+ }
+
+ function assertQueryContains( $str ) {
+ global $wpdb;
+
+ $constraint = new scb_Constraint_StringContains( $str );
+ $this->assertThat( $wpdb->_query_log, $constraint );
+ }
+}
+

0 comments on commit 245d930

Please sign in to comment.