From e2de7663bc210f274f9e794fe665e34c3356af49 Mon Sep 17 00:00:00 2001 From: Alangi Derick Date: Mon, 19 Nov 2018 20:07:30 +0100 Subject: [PATCH] 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 --- .../phpunit/includes/MagicWordFactoryTest.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/phpunit/includes/MagicWordFactoryTest.php 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 ); + } +} -- 2.20.1