From: Alangi Derick Date: Mon, 19 Nov 2018 19:07:30 +0000 (+0100) Subject: tests: Add PHPUnit tests for methods in MagicWordFactory::class X-Git-Tag: 1.34.0-rc.0~3231^2 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=e2de7663bc210f274f9e794fe665e34c3356af49;p=lhc%2Fweb%2Fwiklou.git tests: Add PHPUnit tests for methods in MagicWordFactory::class * 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 --- diff --git a/tests/phpunit/includes/MagicWordFactoryTest.php b/tests/phpunit/includes/MagicWordFactoryTest.php new file mode 100644 index 0000000000..ec7f2abdaf --- /dev/null +++ b/tests/phpunit/includes/MagicWordFactoryTest.php @@ -0,0 +1,91 @@ +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 ); + } +}