Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/49612


Change subject: Add unit tests, demonstrating how to stub out deps.
......................................................................

Add unit tests, demonstrating how to stub out deps.

Physikerwelt asked for some guidance on how to write good unit tests for
classes that depend on external resources. I wrote a few to serve as examples
for additional tests. Because they have an ulterior didactic purpose, the
comments are a bit more verbose than I would otherwise like, but despite that
the tests are good enough to merit being merged.

Change-Id: Ifa97eec1a68fb68b4744d1e5b192b410afe5ef68
---
A tests/MathTexvcTest.php
1 file changed, 117 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math 
refs/changes/12/49612/1

diff --git a/tests/MathTexvcTest.php b/tests/MathTexvcTest.php
new file mode 100644
index 0000000..fd12178
--- /dev/null
+++ b/tests/MathTexvcTest.php
@@ -0,0 +1,117 @@
+<?php
+/**
+ * PHPUnit tests for MathTexvc.
+ *
+ * @group Extensions
+ * @group Math
+ */
+
+/**
+ * @covers MathTexvc
+ */
+class MathTexvcTest extends MediaWikiTestCase {
+
+       /**
+        * Tests behavior of render() upon a cache hit.
+        * If the rendered object exists in the database cache, MathTexvc
+        * just generates HTML from it, and skips shelling out to texvc
+        * entirely.
+        * @covers MathTexvc::render
+        */
+       function testRenderCacheHit() {
+               global $wgMathCheckFiles;
+
+               // Disable file checks. (This is permissable, because PHPUnit
+               // will backup / restore global state on test setup / teardown.)
+               $wgMathCheckFiles = false;
+
+               // Create a MathTexvc mock, replacing methods 'readFromDB',
+               // 'callTexvc', and 'doHTMLRender' with test doubles.
+               $texvc = $this->getMockBuilder( 'MathTexvc' )
+                       ->setMethods( array( 'readFromDB', 'callTexvc', 
'doHTMLRender' ) )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               // When we call render() below, MathTexvc will ...
+
+               // ... first check if the item exists in the database cache:
+               $texvc->expects( $this->once() )
+                       ->method( 'readFromDB' )
+                       ->with()
+                       ->will( $this->returnValue( true ) );
+
+               // ... if cache lookup succeeded, it won't shell out to texvc:
+               $texvc->expects( $this->never() )
+                       ->method( 'callTexvc' );
+
+               // ... instead, MathTexvc will skip to HTML generation:
+               $texvc->expects( $this->once() )
+                       ->method( 'doHTMLRender' );
+
+               $texvc->render();
+       }
+
+       /**
+        * Test behavior of render() upon cache miss.
+        * If the rendered object is not in the cache, MathTexvc will shell
+        * out to texvc to generate it. If texvc succeeds, it'll use the
+        * result to generate HTML.
+        * @covers MathTexvc::render
+        */
+       function testRenderCacheMiss() {
+               $texvc = $this->getMockBuilder( 'MathTexvc' )
+                       ->setMethods( array( 'readCache', 'callTexvc', 
'doHTMLRender' ) )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               // When we call render() below, MathTexvc will ...
+
+               // ... first look up the item in cache:
+               $texvc->expects( $this->once() )
+                       ->method( 'readCache' )
+                       ->will( $this->returnValue( false ) );
+
+               // ... on cache miss, MathTexvc will shell out to texvc:
+               $texvc->expects( $this->once() )
+                       ->method( 'callTexvc' )
+                       ->will( $this->returnValue( MW_TEXVC_SUCCESS ) );
+
+               // ... if texvc succeeds, MathTexvc will generate HTML:
+               $texvc->expects( $this->once() )
+                       ->method( 'doHTMLRender' );
+
+               $texvc->render();
+       }
+
+       /**
+        * Test behavior of render() when texvc fails.
+        * If texvc returns a value other than MW_TEXVC_SUCCESS, render()
+        * returns the error object and does not attempt to generate HTML.
+        * @covers MathTexvc::render
+        */
+       function testRenderTexvcFailure() {
+               $texvc = $this->getMockBuilder( 'MathTexvc' )
+                       ->setMethods( array( 'readCache', 'callTexvc', 
'doHTMLRender' ) )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               // When we call render() below, MathTexvc will ...
+
+               // ... first look up the item in cache:
+               $texvc->expects( $this->any() )
+                       ->method( 'readCache' )
+                       ->will( $this->returnValue( false ) );
+
+               // ... on cache miss, shell out to texvc:
+               $texvc->expects( $this->once() )
+                       ->method( 'callTexvc' )
+                       ->will( $this->returnValue( 'error' ) );
+
+               // ... if texvc fails, render() will not generate HTML:
+               $texvc->expects( $this->never() )
+                       ->method( 'doHTMLRender' );
+
+               // ... it will return the error result instead:
+               $this->assertEquals( $texvc->render(), 'error' );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/49612
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifa97eec1a68fb68b4744d1e5b192b410afe5ef68
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to