ParserFactoryTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * @covers ParserFactory
  4. */
  5. class ParserFactoryTest extends MediaWikiTestCase {
  6. /**
  7. * For backwards compatibility, all parameters to the parser constructor are optional and
  8. * default to the appropriate global service, so it's easy to forget to update ParserFactory to
  9. * actually pass the parameters it's supposed to.
  10. */
  11. public function testConstructorArgNum() {
  12. $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
  13. $instanceConstructor = new ReflectionMethod( 'Parser', '__construct' );
  14. // Subtract one for the ParserFactory itself
  15. $this->assertSame( $instanceConstructor->getNumberOfParameters() - 1,
  16. $factoryConstructor->getNumberOfParameters(),
  17. 'Parser and ParserFactory constructors have an inconsistent number of parameters. ' .
  18. 'Did you add a parameter to one and not the other?' );
  19. }
  20. public function testAllArgumentsWerePassed() {
  21. $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
  22. $mocks = [];
  23. foreach ( $factoryConstructor->getParameters() as $param ) {
  24. $type = (string)$param->getType();
  25. if ( $type === 'array' ) {
  26. $val = [ 'porcupines will tell me your secrets' . count( $mocks ) ];
  27. } elseif ( class_exists( $type ) || interface_exists( $type ) ) {
  28. $val = $this->createMock( $type );
  29. } elseif ( $type === '' ) {
  30. // Optimistically assume a string is okay
  31. $val = 'I will de-quill them first' . count( $mocks );
  32. } else {
  33. $this->fail( "Unrecognized parameter type $type in ParserFactory constructor" );
  34. }
  35. $mocks[] = $val;
  36. }
  37. $factory = new ParserFactory( ...$mocks );
  38. $parser = $factory->create();
  39. foreach ( ( new ReflectionObject( $parser ) )->getProperties() as $prop ) {
  40. $prop->setAccessible( true );
  41. foreach ( $mocks as $idx => $mock ) {
  42. if ( $prop->getValue( $parser ) === $mock ) {
  43. unset( $mocks[$idx] );
  44. }
  45. }
  46. }
  47. $this->assertCount( 0, $mocks, 'Not all arguments to the ParserFactory constructor were ' .
  48. 'found in Parser member variables' );
  49. }
  50. }