From c22f628a68f92e8ea60929bd424b1cd1d605c3fe Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Sun, 7 Apr 2013 02:01:56 +0200 Subject: [PATCH] additional phpUnitTests adds two new phpUnitTests for the abstract MathRender class and another one that test the basic database access. Therefore the read and write methods have been made changed from protected to public. Change-Id: I77a8b0a4dfe7529b5521ead097ac7b518688ef70 --- MathRenderer.php | 4 +- tests/MathDatabaseTest.php | 101 +++++++++++++++++++++++++++++++++++++ tests/MathRenderTest.php | 46 +++++++++++++++++ 3 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 tests/MathDatabaseTest.php create mode 100644 tests/MathRenderTest.php diff --git a/MathRenderer.php b/MathRenderer.php index e969ad9..2c35aab 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -118,7 +118,7 @@ abstract class MathRenderer { * * @return boolean true if read successfully, false otherwise */ - protected function readFromDB() { + public function readFromDB() { $dbr = wfGetDB( DB_SLAVE ); $rpage = $dbr->selectRow( 'math', @@ -150,7 +150,7 @@ abstract class MathRenderer { /** * Writes rendering entry to database */ - protected function writeDBEntry() { + public function writeDBEntry() { # Now save it back to the DB: if ( !wfReadOnly() ) { $dbw = wfGetDB( DB_MASTER ); diff --git a/tests/MathDatabaseTest.php b/tests/MathDatabaseTest.php new file mode 100644 index 0000000..3e931ae --- /dev/null +++ b/tests/MathDatabaseTest.php @@ -0,0 +1,101 @@ +b"; + const SOME_MATHML = "i⁢ℏ⁢∂t⁡Ψ=H^⁢Ψ<\ci>"; + const SOME_LOG = "Sample Log Text."; + const SOME_STATUSCODE = 2; + const SOME_TIMESTAMP = 1272509157; + const SOME_VALIDXML = true; + const NUM_BASIC_FIELDS = 5; + + /** + * creates a new database connection and a new math renderer + * TODO: Check if there is a way to get database access without creating + * the connection to the datbase explictly + * function addDBData() { + * $this->tablesUsed[] = 'math'; + * } + * was not sufficant. + */ + protected function setup() { + global $wgDebugMath; + parent::setUp(); + // TODO:figure out why this is neccessary + $this->db = wfGetDB( DB_MASTER ); + // Create a new instance of MathSource + $this->renderer = $this->getMockForAbstractClass( 'MathRenderer', array ( self::SOME_TEX ) ); + $this->tablesUsed[] = 'math'; + self::setupTestDB( $this->db, "mathtest" ); + + $wgDebugMath = FALSE; + } + /** + * Checks the tex and hash functions + * @covers MathRenderer::getInputHash() + */ + public function testInputHash() { + $expectedhash = $this->db->encodeBlob( pack( "H32", md5( self::SOME_TEX ) ) ); + $this->assertEquals( $expectedhash, $this->renderer->getInputHash() ); + } + + /** + * Helper function to set the current state of the sample renderer istance to the test values + */ + public function setValues() { + // set some values + $this->renderer->tex = self::SOME_TEX ; + $this->renderer->html = self::SOME_HTML; + $this->renderer->mathml = self::SOME_MATHML; + } + /** + * Checks database access. Writes an etry and reads it back. + * @convers MathRenderer::writeDatabaseEntry() + * @convers MathRenderer::readDatabaseEntry() + */ + public function testDBBasics() { + // ; + $this->setValues(); + $wgDebugMath = false; + + $this->renderer->writeDBEntry(); + + $renderer2 = $this->getMockForAbstractClass( 'MathRenderer', array ( self::SOME_TEX ) ); + $renderer2->readFromDB(); + // comparing the class object does now work due to null values etc. + // $this->assertEquals($this->renderer,$renderer2); + $this->assertEquals( $this->renderer->getTex(), $renderer2->getTex(), "test if tex is the same" ); + $this->assertEquals( $this->renderer->mathml, $renderer2->mathml, "Check MathML encoding" ); + $this->assertEquals( $this->renderer->html, $renderer2->html ); + } + + + + /** + * Checks the creation of the math table without debugging endabled. + * @covers MathHooks::onLoadExtensionSchemaUpdates + */ + public function testBasicCreateTable() { + global $wgDebugMath; + $this->db->dropTable( "math", __METHOD__ ); + $wgDebugMath = false; + $dbu = DatabaseUpdater::newForDB( $this->db ); + $dbu->doUpdates( array( "extensions" ) ); + $this->expectOutputRegex( '/(.*)Creating math table(.*)/' ); + $this->setValues(); + $this->renderer->writeDBEntry(); + $res = $this->db->select( "math", "*" ); + $row = $res->fetchRow(); + $this->assertEquals( sizeof( $row ), 2 * self::NUM_BASIC_FIELDS ); + } + + + +} \ No newline at end of file diff --git a/tests/MathRenderTest.php b/tests/MathRenderTest.php new file mode 100644 index 0000000..846734e --- /dev/null +++ b/tests/MathRenderTest.php @@ -0,0 +1,46 @@ +getMockForAbstractClass( 'MathRenderer', array ( MathDatabaseTest::SOME_TEX ) ); + // check if the TeX input was corretly passed to the class + $this->assertEquals( MathDatabaseTest::SOME_TEX, $renderer->getTex(), "test getTex" ); + } + + /** + * Test behavior of writeCache() when nothing was changed + * @covers MathRenderer::writeCache() + */ + public function testWriteCacheSkip() { + $renderer = $this->getMockBuilder( 'MathRenderer' ) + ->setMethods( array( 'writeDBEntry' , 'render' ) ) + ->disableOriginalConstructor() + ->getMock(); + $renderer->expects( $this->never() ) + ->method( 'writeDBEntry' ); + $renderer->writeCache(); + } + + /** + * Test behavior of writeCache() when values were changed. + * @covers MathRenderer::writeCache() + */ + public function testWriteCache() { + $renderer = $this->getMockBuilder( 'MathRenderer' ) + ->setMethods( array( 'writeDBEntry' , 'render' ) ) + ->disableOriginalConstructor() + ->getMock(); + $renderer->expects( $this->never() ) + ->method( 'writeDBEntry' ); + $renderer->writeCache(); + } +} \ No newline at end of file