Skip to content

Commit

Permalink
Merge pull request #99 from Xon/patch-1
Browse files Browse the repository at this point in the history
Unit Test updates (php7.2 fixes & enable for php 7.3 when it comes out)
  • Loading branch information
colinmollenhour authored Mar 13, 2018
2 parents f5aa18c + f3f0bec commit 6477dac
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ php:
- 7.0
- 7.1
- 7.2
- nightly
allow_failures:
- php: nightly


install:
- yes '' | pecl install -f redis
Expand Down
6 changes: 3 additions & 3 deletions tests/CredisClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testAlias()
$this->assertEquals($config['port'],$this->cluster->client($offset)->getPort());
}
$alias = "non-existent-alias";
$this->expectException('CredisException',"Client $alias does not exist.");
$this->setExpectedExceptionShim('CredisException',"Client $alias does not exist.");
$this->cluster->client($alias);
}
public function testMasterSlave()
Expand Down Expand Up @@ -92,7 +92,7 @@ public function testMasterSlave()
$this->waitForSlaveReplication();
$this->assertEquals('value',$this->cluster->client('slave')->get('key'));
$this->assertEquals('value',$this->cluster->get('key'));
$this->expectException('CredisException');
$this->setExpectedExceptionShim('CredisException');
$this->assertFalse($this->cluster->client('slave')->set('key2','value'));
}
public function testMasterWithoutSlavesAndWriteOnlyFlag()
Expand Down Expand Up @@ -185,7 +185,7 @@ public function testCredisClientInstancesInConstructor()
$this->cluster = new Credis_Cluster(array($two,$three,$four),2,$this->useStandalone);
$this->assertTrue($this->cluster->set('key','value'));
$this->assertEquals('value',$this->cluster->get('key'));
$this->expectException('CredisException','Server should either be an array or an instance of Credis_Client');
$this->setExpectedExceptionShim('CredisException','Server should either be an array or an instance of Credis_Client');
new Credis_Cluster(array(new stdClass()),2,$this->useStandalone);
}
public function testSetMasterClient()
Expand Down
16 changes: 8 additions & 8 deletions tests/CredisSentinelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testMasterClient()
$master = $this->sentinel->getMasterClient($this->sentinelConfig->clustername);
$this->assertInstanceOf('Credis_Client',$master);
$this->assertEquals($this->redisConfig[0]['port'],$master->getPort());
$this->expectException('CredisException','Master not found');
$this->setExpectedExceptionShim('CredisException','Master not found');
$this->sentinel->getMasterClient('non-existing-cluster');
}
public function testMasters()
Expand Down Expand Up @@ -73,7 +73,7 @@ public function testMaster()
$this->assertEquals($this->sentinelConfig->clustername,$master[1]);
$this->assertEquals($this->redisConfig[0]['port'],$master[5]);

$this->expectException('CredisException','No such master with that name');
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->master('non-existing-cluster');
}
public function testSlaveClient()
Expand All @@ -84,7 +84,7 @@ public function testSlaveClient()
foreach($slaves as $slave){
$this->assertInstanceOf('Credis_Client',$slave);
}
$this->expectException('CredisException','No such master with that name');
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->getSlaveClients('non-existing-cluster');
}
public function testSlaves()
Expand All @@ -100,12 +100,12 @@ public function testSlaves()
$this->assertInternalType('array',$slaves);
$this->assertCount(0,$slaves);

$this->expectException('CredisException','No such master with that name');
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->slaves('non-existing-cluster');
}
public function testNonExistingClusterNameWhenCreatingSlaves()
{
$this->expectException('CredisException','No such master with that name');
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->createSlaveClients('non-existing-cluster');
}
public function testCreateCluster()
Expand All @@ -116,7 +116,7 @@ public function testCreateCluster()
$cluster = $this->sentinel->createCluster($this->sentinelConfig->clustername,0,1,false);
$this->assertInstanceOf('Credis_Cluster',$cluster);
$this->assertCount(2,$cluster->clients());
$this->expectException('CredisException','The master is down');
$this->setExpectedExceptionShim('CredisException','The master is down');
$this->sentinel->createCluster($this->sentinelConfig->downclustername);
}
public function testGetCluster()
Expand Down Expand Up @@ -155,7 +155,7 @@ public function testGetHostAndPort()
$host = 'localhost';
$port = '123456';

$client = $this->createMock('\Credis_Client');
$client = $this->createMockShim('\Credis_Client');
$sentinel = new Credis_Sentinel($client);

$client->expects($this->once())->method('getHost')->willReturn($host);
Expand All @@ -166,7 +166,7 @@ public function testGetHostAndPort()
}
public function testNonExistingMethod()
{
$this->expectException('CredisException','Unknown sentinel subcommand \'bla\'');
$this->setExpectedExceptionShim('CredisException','Unknown sentinel subcommand \'bla\'');
$this->sentinel->bla();
}
}
2 changes: 1 addition & 1 deletion tests/CredisStandaloneClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testMasterSlave()
$this->waitForSlaveReplication();
$this->assertEquals('value',$this->cluster->client('slave')->get('key'));
$this->assertEquals('value',$this->cluster->get('key'));
$this->expectException('CredisException','READONLY You can\'t write against a read only slave.');
$this->setExpectedExceptionShim('CredisException','READONLY You can\'t write against a read only slave.');
$this->cluster->client('slave')->set('key2','value');
}
}
2 changes: 1 addition & 1 deletion tests/CredisStandaloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testStandAloneArgumentsExtra()

public function testStandAloneMultiPipelineThrowsException()
{
$this->expectException('CredisException','A pipeline is already in use and only one pipeline is supported.');
$this->setExpectedExceptionShim('CredisException','A pipeline is already in use and only one pipeline is supported.');
$this->credis->pipeline()->pipeline();
}
}
8 changes: 4 additions & 4 deletions tests/CredisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ public function testGettersAndSetters()
$this->credis->forceStandalone();
}
$this->credis->setMaxConnectRetries(1);
$this->expectException('CredisException','Connection to Redis localhost:12345 failed after 2 failures.');
$this->setExpectedExceptionShim('CredisException','Connection to Redis localhost:12345 failed after 2 failures.');
$this->credis->connect();
}

Expand Down Expand Up @@ -664,7 +664,7 @@ public function testConnectionStringsSocket()
public function testInvalidTcpConnectionstring()
{
$this->credis->close();
$this->expectException('CredisException','Invalid host format; expected tcp://host[:port][/persistence_identifier]');
$this->setExpectedExceptionShim('CredisException','Invalid host format; expected tcp://host[:port][/persistence_identifier]');
$this->credis = new Credis_Client('tcp://'.$this->redisConfig[0]['host'] . ':abc');
if ($this->useStandalone) {
$this->credis->forceStandalone();
Expand All @@ -674,7 +674,7 @@ public function testInvalidTcpConnectionstring()
public function testInvalidUnixSocketConnectionstring()
{
$this->credis->close();
$this->expectException('CredisException','Invalid unix socket format; expected unix:///path/to/redis.sock');
$this->setExpectedExceptionShim('CredisException','Invalid unix socket format; expected unix:///path/to/redis.sock');
$this->credis = new Credis_Client('unix://path/to/redis.sock');
if ($this->useStandalone) {
$this->credis->forceStandalone();
Expand All @@ -685,7 +685,7 @@ public function testForceStandAloneAfterEstablishedConnection()
{
$this->credis->connect();
if ( ! $this->useStandalone) {
$this->expectException('CredisException','Cannot force Credis_Client to use standalone PHP driver after a connection has already been established.');
$this->setExpectedExceptionShim('CredisException','Cannot force Credis_Client to use standalone PHP driver after a connection has already been established.');
}
$this->credis->forceStandalone();
$this->assertTrue(true);
Expand Down
20 changes: 13 additions & 7 deletions tests/CredisTestCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,29 @@ public static function tearDownAfterClass()
@copy('redis-sentinel.conf.bak','redis-sentinel.conf');
}
}



//
/**
* php 7.2 compat fix, as directly polyfilling for older PHPUnit causes a function signature compatibility issue
* This is due to the defined return type
*
* Polyfill for older PHPUnit
*/
public function createMock($class)
protected function createMockShim($originalClassName)
{
if (method_exists($this, 'getMock')) {
return $this->getMock($class);
return $this->getMock($originalClassName);
} else {
return parent::createMock($class);
return parent::createMock($originalClassName);
}
}

/**
* Polyfill for older PHPUnit
* php 7.2 compat fix, as directly polyfilling for older PHPUnit causes a function signature compatibility issue
* This is due to the defined return type
*/
public function expectException($class, $message = NULL, $code = NULL)
public function setExpectedExceptionShim($class, $message = NULL, $code = NULL)
{
if (method_exists($this, 'setExpectedException')) {
$this->setExpectedException($class, $message, $code);
Expand Down

0 comments on commit 6477dac

Please sign in to comment.