tests: Add PHPUnit tests for methods in MagicWordFactory::class
authorAlangi Derick <alangiderick@gmail.com>
Mon, 19 Nov 2018 19:07:30 +0000 (20:07 +0100)
committerAlangi Derick <alangiderick@gmail.com>
Tue, 18 Dec 2018 08:03:46 +0000 (09:03 +0100)
* testGetContentLanguage() - covers the getContentLanguage() method.
* testGet() - covers the get() method in the MagicWordFactory class.
* testGetVariableIDs() - covers the getVariableIDs method.
* testGetSubstIDs() - covers the getSubstIDs() method.
* testGetCacheTTL() - covers the getCacheTTL() method. This covers
  both paths when there is a valid and an invalid caching hint.
* makeMagicWordFactory() - a helper function for creating an object
  of the MagicWordFactory class.
* testGetDoubleUnderscoreArray() - covers getDoubleUnderscoreArray()
  method and also calls newArray() under the hood so that is also
  covered.

Change-Id: I5d2166f155e31900cb40c22fb976e81d0d545627

tests/phpunit/includes/MagicWordFactoryTest.php [new file with mode: 0644]

diff --git a/tests/phpunit/includes/MagicWordFactoryTest.php b/tests/phpunit/includes/MagicWordFactoryTest.php
new file mode 100644 (file)
index 0000000..ec7f2ab
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * @covers \MagicWordFactory
+ *
+ * @author Derick N. Alangi
+ */
+class MagicWordFactoryTest extends MediaWikiTestCase {
+       /**
+        * Make magic word factory
+        */
+       private function makeMagicWordFactory( $contLang ) {
+               if ( $contLang === null ) {
+                       $mwf = new MagicWordFactory( Language::factory( 'en' ) );
+                       return $mwf;
+               }
+               return new MagicWordFactory( $contLang );
+       }
+
+       public function testGetContentLanguage() {
+               $contLang = Language::factory( 'en' );
+
+               $magicWordFactory = $this->makeMagicWordFactory( $contLang );
+               $mwfActual = $magicWordFactory->getContentLanguage();
+
+               $this->assertSame( $contLang, $mwfActual );
+       }
+
+       public function testGetMagicWord() {
+               $magicWordIdValid = 'pageid';
+               $magicWordFactory = $this->makeMagicWordFactory( null );
+               $mwActual = $magicWordFactory->get( $magicWordIdValid );
+               $contLang = $magicWordFactory->getContentLanguage();
+               $expected = new MagicWord( $magicWordIdValid, [ 'PAGEID' ], false, $contLang );
+
+               $this->assertEquals( $expected, $mwActual );
+       }
+
+       public function testGetInvalidMagicWord() {
+               $magicWordFactory = $this->makeMagicWordFactory( null );
+
+               $this->setExpectedException( MWException::class );
+               \Wikimedia\suppressWarnings();
+               try {
+                       $magicWordFactory->get( 'invalid magic word' );
+               } finally {
+                       \Wikimedia\restoreWarnings();
+               }
+       }
+
+       public function testGetVariableIDs() {
+               $magicWordFactory = $this->makeMagicWordFactory( null );
+               $varIds = $magicWordFactory->getVariableIDs();
+
+               $this->assertContainsOnly( 'string', $varIds );
+               $this->assertNotEmpty( $varIds );
+               $this->assertInternalType( 'array', $varIds );
+       }
+
+       public function testGetSubstIDs() {
+               $magicWordFactory = $this->makeMagicWordFactory( null );
+               $substIds = $magicWordFactory->getSubstIDs();
+
+               $this->assertContainsOnly( 'string', $substIds );
+               $this->assertNotEmpty( $substIds );
+               $this->assertInternalType( 'array', $substIds );
+       }
+
+       /**
+        * Test both valid and invalid caching hints paths
+        */
+       public function testGetCacheTTL() {
+               $magicWordFactory = $this->makeMagicWordFactory( null );
+               $actual = $magicWordFactory->getCacheTTL( 'localday' );
+
+               $this->assertSame( 3600, $actual );
+
+               $actual = $magicWordFactory->getCacheTTL( 'currentmonth' );
+               $this->assertSame( 86400, $actual );
+
+               $actual = $magicWordFactory->getCacheTTL( 'invalid' );
+               $this->assertSame( -1, $actual );
+       }
+
+       public function testGetDoubleUnderscoreArray() {
+               $magicWordFactory = $this->makeMagicWordFactory( null );
+               $actual = $magicWordFactory->getDoubleUnderscoreArray();
+
+               $this->assertInstanceOf( MagicWordArray::class, $actual );
+       }
+}