SkinFactoryTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. class SkinFactoryTest extends MediaWikiTestCase {
  3. /**
  4. * @covers SkinFactory::register
  5. */
  6. public function testRegister() {
  7. $factory = new SkinFactory();
  8. $factory->register( 'fallback', 'Fallback', function () {
  9. return new SkinFallback();
  10. } );
  11. $this->assertTrue( true ); // No exception thrown
  12. $this->setExpectedException( InvalidArgumentException::class );
  13. $factory->register( 'invalid', 'Invalid', 'Invalid callback' );
  14. }
  15. /**
  16. * @covers SkinFactory::makeSkin
  17. */
  18. public function testMakeSkinWithNoBuilders() {
  19. $factory = new SkinFactory();
  20. $this->setExpectedException( SkinException::class );
  21. $factory->makeSkin( 'nobuilderregistered' );
  22. }
  23. /**
  24. * @covers SkinFactory::makeSkin
  25. */
  26. public function testMakeSkinWithInvalidCallback() {
  27. $factory = new SkinFactory();
  28. $factory->register( 'unittest', 'Unittest', function () {
  29. return true; // Not a Skin object
  30. } );
  31. $this->setExpectedException( UnexpectedValueException::class );
  32. $factory->makeSkin( 'unittest' );
  33. }
  34. /**
  35. * @covers SkinFactory::makeSkin
  36. */
  37. public function testMakeSkinWithValidCallback() {
  38. $factory = new SkinFactory();
  39. $factory->register( 'testfallback', 'TestFallback', function () {
  40. return new SkinFallback();
  41. } );
  42. $skin = $factory->makeSkin( 'testfallback' );
  43. $this->assertInstanceOf( Skin::class, $skin );
  44. $this->assertInstanceOf( SkinFallback::class, $skin );
  45. $this->assertEquals( 'fallback', $skin->getSkinName() );
  46. }
  47. /**
  48. * @covers Skin::__construct
  49. * @covers Skin::getSkinName
  50. */
  51. public function testGetSkinName() {
  52. $skin = new SkinFallback();
  53. $this->assertEquals( 'fallback', $skin->getSkinName(), 'Default' );
  54. $skin = new SkinFallback( 'testname' );
  55. $this->assertEquals( 'testname', $skin->getSkinName(), 'Constructor argument' );
  56. }
  57. /**
  58. * @covers SkinFactory::getSkinNames
  59. */
  60. public function testGetSkinNames() {
  61. $factory = new SkinFactory();
  62. // A fake callback we can use that will never be called
  63. $callback = function () {
  64. // NOP
  65. };
  66. $factory->register( 'skin1', 'Skin1', $callback );
  67. $factory->register( 'skin2', 'Skin2', $callback );
  68. $names = $factory->getSkinNames();
  69. $this->assertArrayHasKey( 'skin1', $names );
  70. $this->assertArrayHasKey( 'skin2', $names );
  71. $this->assertEquals( 'Skin1', $names['skin1'] );
  72. $this->assertEquals( 'Skin2', $names['skin2'] );
  73. }
  74. }