TitleArrayFromResultTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @author Addshore
  4. * @covers TitleArrayFromResult
  5. */
  6. class TitleArrayFromResultTest extends PHPUnit\Framework\TestCase {
  7. use MediaWikiCoversValidator;
  8. private function getMockResultWrapper( $row = null, $numRows = 1 ) {
  9. $resultWrapper = $this->getMockBuilder( Wikimedia\Rdbms\ResultWrapper::class )
  10. ->disableOriginalConstructor();
  11. $resultWrapper = $resultWrapper->getMock();
  12. $resultWrapper->expects( $this->atLeastOnce() )
  13. ->method( 'current' )
  14. ->will( $this->returnValue( $row ) );
  15. $resultWrapper->expects( $this->any() )
  16. ->method( 'numRows' )
  17. ->will( $this->returnValue( $numRows ) );
  18. return $resultWrapper;
  19. }
  20. private function getRowWithTitle( $namespace = 3, $title = 'foo' ) {
  21. $row = new stdClass();
  22. $row->page_namespace = $namespace;
  23. $row->page_title = $title;
  24. return $row;
  25. }
  26. private function getTitleArrayFromResult( $resultWrapper ) {
  27. return new TitleArrayFromResult( $resultWrapper );
  28. }
  29. /**
  30. * @covers TitleArrayFromResult::__construct
  31. */
  32. public function testConstructionWithFalseRow() {
  33. $row = false;
  34. $resultWrapper = $this->getMockResultWrapper( $row );
  35. $object = $this->getTitleArrayFromResult( $resultWrapper );
  36. $this->assertEquals( $resultWrapper, $object->res );
  37. $this->assertSame( 0, $object->key );
  38. $this->assertEquals( $row, $object->current );
  39. }
  40. /**
  41. * @covers TitleArrayFromResult::__construct
  42. */
  43. public function testConstructionWithRow() {
  44. $namespace = 0;
  45. $title = 'foo';
  46. $row = $this->getRowWithTitle( $namespace, $title );
  47. $resultWrapper = $this->getMockResultWrapper( $row );
  48. $object = $this->getTitleArrayFromResult( $resultWrapper );
  49. $this->assertEquals( $resultWrapper, $object->res );
  50. $this->assertSame( 0, $object->key );
  51. $this->assertInstanceOf( Title::class, $object->current );
  52. $this->assertEquals( $namespace, $object->current->mNamespace );
  53. $this->assertEquals( $title, $object->current->mTextform );
  54. }
  55. public static function provideNumberOfRows() {
  56. return [
  57. [ 0 ],
  58. [ 1 ],
  59. [ 122 ],
  60. ];
  61. }
  62. /**
  63. * @dataProvider provideNumberOfRows
  64. * @covers TitleArrayFromResult::count
  65. */
  66. public function testCountWithVaryingValues( $numRows ) {
  67. $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper(
  68. $this->getRowWithTitle(),
  69. $numRows
  70. ) );
  71. $this->assertEquals( $numRows, $object->count() );
  72. }
  73. /**
  74. * @covers TitleArrayFromResult::current
  75. */
  76. public function testCurrentAfterConstruction() {
  77. $namespace = 0;
  78. $title = 'foo';
  79. $row = $this->getRowWithTitle( $namespace, $title );
  80. $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $row ) );
  81. $this->assertInstanceOf( Title::class, $object->current() );
  82. $this->assertEquals( $namespace, $object->current->mNamespace );
  83. $this->assertEquals( $title, $object->current->mTextform );
  84. }
  85. public function provideTestValid() {
  86. return [
  87. [ $this->getRowWithTitle(), true ],
  88. [ false, false ],
  89. ];
  90. }
  91. /**
  92. * @dataProvider provideTestValid
  93. * @covers TitleArrayFromResult::valid
  94. */
  95. public function testValid( $input, $expected ) {
  96. $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $input ) );
  97. $this->assertEquals( $expected, $object->valid() );
  98. }
  99. // @todo unit test for key()
  100. // @todo unit test for next()
  101. // @todo unit test for rewind()
  102. }