Daniel Kinzler has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/401505 )

Change subject: Add test for DBConnRef
......................................................................

Add test for DBConnRef

Change-Id: I510b2ed22b3866392592a5df53aa0f3ff6aab750
---
A tests/phpunit/includes/libs/rdbms/database/DBConnRefTest.php
1 file changed, 146 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/05/401505/1

diff --git a/tests/phpunit/includes/libs/rdbms/database/DBConnRefTest.php 
b/tests/phpunit/includes/libs/rdbms/database/DBConnRefTest.php
new file mode 100644
index 0000000..e660265
--- /dev/null
+++ b/tests/phpunit/includes/libs/rdbms/database/DBConnRefTest.php
@@ -0,0 +1,146 @@
+<?php
+
+use Wikimedia\Rdbms\Database;
+use Wikimedia\Rdbms\DBConnRef;
+use Wikimedia\Rdbms\FakeResultWrapper;
+use Wikimedia\Rdbms\IDatabase;
+use Wikimedia\Rdbms\ILoadBalancer;
+use Wikimedia\Rdbms\ResultWrapper;
+
+/**
+ * @covers Wikimedia\Rdbms\DBConnRef
+ */
+class DBConnRefTest extends PHPUnit_Framework_TestCase {
+
+       /**
+        * @return ILoadBalancer
+        */
+       private function getLoadBalancerMock() {
+               $lb = $this->getMock( ILoadBalancer::class );
+
+               $lb->method( 'getConnection' )->willReturnCallback(
+                       function () {
+                               return $this->getDatabaseMock();
+                       }
+               );
+
+               $lb->method( 'getConnectionRef' )->willReturnCallback(
+                       function () use ( $lb ) {
+                               return $this->getDBConnRef( $lb );
+                       }
+               );
+
+               return $lb;
+       }
+
+       /**
+        * @return IDatabase
+        */
+       private function getDatabaseMock() {
+               $db = $this->getMockBuilder( Database::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $db->method( 'select' )->willReturn( new FakeResultWrapper( [] 
) );
+               $db->method( '__toString' )->willReturn( 'MOCK_DB' );
+
+               return $db;
+       }
+
+       /**
+        * @return IDatabase
+        */
+       private function getDBConnRef( ILoadBalancer $lb = null ) {
+               $lb = $lb ?: $this->getLoadBalancerMock();
+               return new DBConnRef( $lb, $this->getDatabaseMock() );
+       }
+
+       public function testConstruct() {
+               $lb = $this->getLoadBalancerMock();
+               $ref = new DBConnRef( $lb, $this->getDatabaseMock() );
+
+               $this->assertInstanceOf( ResultWrapper::class, $ref->select( 
'whatever', '*' ) );
+       }
+
+       public function testConstruct_params() {
+               $lb = $this->getMock( ILoadBalancer::class );
+
+               $lb->expects( $this->once() )
+                       ->method( 'getConnection' )
+                       ->with( DB_MASTER, [ 'test' ], 'dummy', 
ILoadBalancer::CONN_TRX_AUTO )
+                       ->willReturnCallback(
+                               function () {
+                                       return $this->getDatabaseMock();
+                               }
+                       );
+
+               $ref = new DBConnRef(
+                       $lb,
+                       [ DB_MASTER, [ 'test' ], 'dummy', 
ILoadBalancer::CONN_TRX_AUTO ]
+               );
+
+               $this->assertInstanceOf( ResultWrapper::class, $ref->select( 
'whatever', '*' ) );
+       }
+
+       public function testDestruct() {
+               $lb = $this->getLoadBalancerMock();
+
+               $lb->expects( $this->once() )
+                       ->method( 'reuseConnection' );
+
+               $this->innerMethodForTestDestruct( $lb );
+       }
+
+       private function innerMethodForTestDestruct( ILoadBalancer $lb ) {
+               $ref = $lb->getConnectionRef( DB_REPLICA );
+
+               $this->assertInstanceOf( ResultWrapper::class, $ref->select( 
'whatever', '*' ) );
+       }
+
+       public function testConstruct_failure() {
+               $this->setExpectedException( InvalidArgumentException::class, 
'' );
+
+               $lb = $this->getLoadBalancerMock();
+               new DBConnRef( $lb, 17 ); // bad constructor argument
+       }
+
+       public function testGetWikiID() {
+               $lb = $this->getMock( ILoadBalancer::class );
+
+               // getWikiID is optimized to not create a connection
+               $lb->expects( $this->never() )
+                       ->method( 'getConnection' );
+
+               $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
+
+               $this->assertSame( 'dummy', $ref->getWikiID() );
+       }
+
+       public function testGetDomainID() {
+               $lb = $this->getMock( ILoadBalancer::class );
+
+               // getDomainID is optimized to not create a connection
+               $lb->expects( $this->never() )
+                       ->method( 'getConnection' );
+
+               $ref = new DBConnRef( $lb, [ DB_REPLICA, [], 'dummy', 0 ] );
+
+               $this->assertSame( 'dummy', $ref->getDomainID() );
+       }
+
+       public function testSelect() {
+               // select should get passed through normally
+               $ref = $this->getDBConnRef();
+               $this->assertInstanceOf( ResultWrapper::class, $ref->select( 
'whatever', '*' ) );
+       }
+
+       public function testToString() {
+               $ref = $this->getDBConnRef();
+               $this->assertInternalType( 'string', $ref->__toString() );
+
+               $lb = $this->getLoadBalancerMock();
+               $ref = new DBConnRef( $lb, [ DB_MASTER, [], 'test', 0 ] );
+               $this->assertInternalType( 'string', $ref->__toString() );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/401505
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I510b2ed22b3866392592a5df53aa0f3ff6aab750
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to