Skip to content

Commit

Permalink
Support PHP 8.0
Browse files Browse the repository at this point in the history
And update tests to support PHPUnit >= 8.0
  • Loading branch information
tortuetorche committed Nov 30, 2020
1 parent 55b84d7 commit 62cdce8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 37 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0snapshot

env:
matrix:
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"Tylercd100\\Monolog\\Tests\\": "tests/"
}
},
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.0",
"php": "^7.2|^8.0",
"monolog/monolog": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0"
"phpunit/phpunit": "^8.2.3|^9.0"
},
"authors": [
{
Expand Down
20 changes: 10 additions & 10 deletions tests/ClickatellHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public function testCanBeInstantiatedAndProvidesDefaultFormatter()
$handler = new ClickatellHandler('token', '+15555555555', '+16666666666');

$this->assertInstanceOf('Tylercd100\\Monolog\\Formatter\\SMSFormatter', $handler->getFormatter());
$this->assertAttributeEquals('token', 'authToken', $handler);
$this->assertAttributeEquals('+15555555555', 'fromNumber', $handler);
$this->assertAttributeEquals('+16666666666', 'toNumber', $handler);
$this->assertEquals('token', $this->accessProtected($handler, 'authToken'));
$this->assertEquals('+15555555555', $this->accessProtected($handler, 'fromNumber'));
$this->assertEquals('+16666666666', $this->accessProtected($handler, 'toNumber'));
}

public function testItThrowsExceptionWhenUsingDifferentVersionOtherThanV1()
{
$this->setExpectedException(Exception::class);
$this->expectException(Exception::class);
$handler = new ClickatellHandler('token', 'auth_id', '+15555555555', '+16666666666', Logger::CRITICAL, true, true, 'twilio.foo.bar', 'v2');
}

Expand All @@ -38,7 +38,7 @@ public function testWriteHeader()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/POST \/messages HTTP\/1.1\\r\\nHost: platform.clickatell.com\\r\\nAuthorization: token\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/messages HTTP\/1.1\\r\\nHost: platform.clickatell.com\\r\\nAuthorization: token\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);

return $content;
}
Expand All @@ -50,7 +50,7 @@ public function testWriteCustomHostHeader()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/POST \/messages HTTP\/1.1\\r\\nHost: twilio.foo.bar\\r\\nAuthorization: token\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/messages HTTP\/1.1\\r\\nHost: twilio.foo.bar\\r\\nAuthorization: token\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);

return $content;
}
Expand All @@ -60,7 +60,7 @@ public function testWriteCustomHostHeader()
*/
public function testWriteContent($content)
{
$this->assertRegexp('/{"content":"test1","to":\["\+16666666666"\],"from":"\+15555555555"}/', $content);
$this->assertMatchesRegularExpression('/{"content":"test1","to":\["\+16666666666"\],"from":"\+15555555555"}/', $content);
}

public function testWriteContentV1WithoutToAndFromNumbers()
Expand All @@ -70,7 +70,7 @@ public function testWriteContentV1WithoutToAndFromNumbers()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/{"content":"test1","to":\[null\]}/', $content);
$this->assertMatchesRegularExpression('/{"content":"test1","to":\[null\]}/', $content);

return $content;
}
Expand All @@ -80,7 +80,7 @@ public function testWriteContentV1WithoutToAndFromNumbers()
*/
public function testWriteContentNotify($content)
{
$this->assertRegexp('/{"content":"test1","to":\["\+16666666666"\],"from":"\+15555555555"}/', $content);
$this->assertMatchesRegularExpression('/{"content":"test1","to":\["\+16666666666"\],"from":"\+15555555555"}/', $content);
}

public function testWriteWithComplexMessage()
Expand All @@ -90,7 +90,7 @@ public function testWriteWithComplexMessage()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/{"content":"Backup of database example finished in 16 minutes\.","to":\["\+16666666666"\],"from":"\+15555555555"}/', $content);
$this->assertMatchesRegularExpression('/{"content":"Backup of database example finished in 16 minutes\.","to":\["\+16666666666"\],"from":"\+15555555555"}/', $content);
}

private function createHandler($authToken = 'token', $fromNumber = '+15555555555', $toNumber = '+16666666666', $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'platform.clickatell.com', $version = ClickatellHandler::API_V1)
Expand Down
22 changes: 11 additions & 11 deletions tests/PlivoHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function testCanBeInstantiatedAndProvidesDefaultFormatter()
$handler = new PlivoHandler('token', 'auth_id', '+15555555555', '+16666666666');

$this->assertInstanceOf('Tylercd100\\Monolog\\Formatter\\SMSFormatter', $handler->getFormatter());
$this->assertAttributeEquals('token', 'authToken', $handler);
$this->assertAttributeEquals('auth_id', 'authId', $handler);
$this->assertAttributeEquals('+15555555555', 'fromNumber', $handler);
$this->assertAttributeEquals('+16666666666', 'toNumber', $handler);
$this->assertEquals('token', $this->accessProtected($handler, 'authToken'));
$this->assertEquals('auth_id', $this->accessProtected($handler, 'authId'));
$this->assertEquals('+15555555555', $this->accessProtected($handler, 'fromNumber'));
$this->assertEquals('+16666666666', $this->accessProtected($handler, 'toNumber'));
}

public function testItThrowsExceptionWhenUsingDifferentVersionOtherThanV1()
{
$this->setExpectedException(Exception::class);
$this->expectException(Exception::class);
$handler = new PlivoHandler('token', 'auth_id', '+15555555555', '+16666666666', Logger::CRITICAL, true, true, 'plivo.foo.bar', 'v2');
}

Expand All @@ -38,7 +38,7 @@ public function testWriteHeader()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/POST \/v1\/Account\/auth_id\/Message\/ HTTP\/1.1\\r\\nHost: api.plivo.com\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/v1\/Account\/auth_id\/Message\/ HTTP\/1.1\\r\\nHost: api.plivo.com\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);

return $content;
}
Expand All @@ -50,7 +50,7 @@ public function testWriteCustomHostHeader()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/POST \/v1\/Account\/auth_id\/Message\/ HTTP\/1.1\\r\\nHost: plivo.foo.bar\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/v1\/Account\/auth_id\/Message\/ HTTP\/1.1\\r\\nHost: plivo.foo.bar\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);

return $content;
}
Expand All @@ -60,7 +60,7 @@ public function testWriteCustomHostHeader()
*/
public function testWriteContent($content)
{
$this->assertRegexp('/{"src":"\+15555555555","dst":"\+16666666666","text":"test1"}/', $content);
$this->assertMatchesRegularExpression('/{"src":"\+15555555555","dst":"\+16666666666","text":"test1"}/', $content);
}

public function testWriteContentV1WithoutToAndFromNumbers()
Expand All @@ -70,7 +70,7 @@ public function testWriteContentV1WithoutToAndFromNumbers()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/{"src":false,"dst":null,"text":"test1"}/', $content);
$this->assertMatchesRegularExpression('/{"src":false,"dst":null,"text":"test1"}/', $content);

return $content;
}
Expand All @@ -80,7 +80,7 @@ public function testWriteContentV1WithoutToAndFromNumbers()
*/
public function testWriteContentNotify($content)
{
$this->assertRegexp('/{"src":"\+15555555555","dst":"\+16666666666","text":"test1"}/', $content);
$this->assertMatchesRegularExpression('/{"src":"\+15555555555","dst":"\+16666666666","text":"test1"}/', $content);
}

public function testWriteWithComplexMessage()
Expand All @@ -90,7 +90,7 @@ public function testWriteWithComplexMessage()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/{"src":"\+15555555555","dst":"\+16666666666","text":"Backup of database example finished in 16 minutes\."}/', $content);
$this->assertMatchesRegularExpression('/{"src":"\+15555555555","dst":"\+16666666666","text":"Backup of database example finished in 16 minutes\."}/', $content);
}

private function createHandler($authToken = 'token', $authId = 'auth_id', $fromNumber = '+15555555555', $toNumber = '+16666666666', $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.plivo.com', $version = PlivoHandler::API_V1)
Expand Down
40 changes: 38 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Tylercd100\Monolog\Tests;

use ReflectionClass;
use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

class TestCase extends \PHPUnit_Framework_TestCase
class TestCase extends PHPUnitTestCase
{
/**
* @return array Record
Expand Down Expand Up @@ -54,8 +56,42 @@ protected function getIdentityFormatter()
$formatter = $this->getMockBuilder(FormatterInterface::class)->getMock();
$formatter->expects($this->any())
->method('format')
->will($this->returnCallback(function ($record) { return $record['message']; }));
->will(
$this->returnCallback(function ($record) {
return $record['message'];
})
);

return $formatter;
}

/**
* Helper to return protected or private property value of an object
*
* @param mixed $object
* @param string $property
* @return mixed
*/
protected function accessProtected($object, $property)
{
$reflection = new ReflectionClass($object);
$propertyReflection = $reflection->getProperty($property);
$propertyReflection->setAccessible(true);

return $propertyReflection->getValue($object);
}

/**
* {@inheritdoc}
*/
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
if (method_exists(PHPUnitTestCase::class, 'assertMatchesRegularExpression')) {
// For PHPUnit >= 9
parent::assertMatchesRegularExpression($pattern, $string, $message);
} else {
// For PHPUnit < 9
static::assertRegExp($pattern, $string, $message);
}
}
}
22 changes: 11 additions & 11 deletions tests/TwilioHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function testCanBeInstantiatedAndProvidesDefaultFormatter()
$handler = new TwilioHandler('token', 'auth_id', '+15555555555', '+16666666666');

$this->assertInstanceOf('Tylercd100\\Monolog\\Formatter\\SMSFormatter', $handler->getFormatter());
$this->assertAttributeEquals('token', 'authToken', $handler);
$this->assertAttributeEquals('auth_id', 'authId', $handler);
$this->assertAttributeEquals('+15555555555', 'fromNumber', $handler);
$this->assertAttributeEquals('+16666666666', 'toNumber', $handler);
$this->assertEquals('token', $this->accessProtected($handler, 'authToken'));
$this->assertEquals('auth_id', $this->accessProtected($handler, 'authId'));
$this->assertEquals('+15555555555', $this->accessProtected($handler, 'fromNumber'));
$this->assertEquals('+16666666666', $this->accessProtected($handler, 'toNumber'));
}

public function testItThrowsExceptionWhenUsingDifferentVersionOtherThanV1()
{
$this->setExpectedException(Exception::class);
$this->expectException(Exception::class);
$handler = new TwilioHandler('token', 'auth_id', '+15555555555', '+16666666666', Logger::CRITICAL, true, true, 'twilio.foo.bar', 'v2');
}

Expand All @@ -38,7 +38,7 @@ public function testWriteHeader()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/POST \/2010-04-01\/Accounts\/auth_id\/Messages\.json HTTP\/1.1\\r\\nHost: api.twilio.com\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/2010-04-01\/Accounts\/auth_id\/Messages\.json HTTP\/1.1\\r\\nHost: api.twilio.com\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);

return $content;
}
Expand All @@ -50,7 +50,7 @@ public function testWriteCustomHostHeader()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/POST \/2010-04-01\/Accounts\/auth_id\/Messages.json HTTP\/1.1\\r\\nHost: twilio.foo.bar\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/2010-04-01\/Accounts\/auth_id\/Messages.json HTTP\/1.1\\r\\nHost: twilio.foo.bar\\r\\nAuthorization: Basic YXV0aF9pZDp0b2tlbg==\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);

return $content;
}
Expand All @@ -60,7 +60,7 @@ public function testWriteCustomHostHeader()
*/
public function testWriteContent($content)
{
$this->assertRegexp('/{"From":"\+15555555555","To":"\+16666666666","Body":"test1"}/', $content);
$this->assertMatchesRegularExpression('/{"From":"\+15555555555","To":"\+16666666666","Body":"test1"}/', $content);
}

public function testWriteContentV1WithoutToAndFromNumbers()
Expand All @@ -70,7 +70,7 @@ public function testWriteContentV1WithoutToAndFromNumbers()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/{"From":false,"To":null,"Body":"test1"}/', $content);
$this->assertMatchesRegularExpression('/{"From":false,"To":null,"Body":"test1"}/', $content);

return $content;
}
Expand All @@ -80,7 +80,7 @@ public function testWriteContentV1WithoutToAndFromNumbers()
*/
public function testWriteContentNotify($content)
{
$this->assertRegexp('/{"From":"\+15555555555","To":"\+16666666666","Body":"test1"}/', $content);
$this->assertMatchesRegularExpression('/{"From":"\+15555555555","To":"\+16666666666","Body":"test1"}/', $content);
}

public function testWriteWithComplexMessage()
Expand All @@ -90,7 +90,7 @@ public function testWriteWithComplexMessage()
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/{"From":"\+15555555555","To":"\+16666666666","Body":"Backup of database example finished in 16 minutes\."}/', $content);
$this->assertMatchesRegularExpression('/{"From":"\+15555555555","To":"\+16666666666","Body":"Backup of database example finished in 16 minutes\."}/', $content);
}

private function createHandler($authToken = 'token', $authId = 'auth_id', $fromNumber = '+15555555555', $toNumber = '+16666666666', $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.twilio.com', $version = TwilioHandler::API_V1)
Expand Down

0 comments on commit 62cdce8

Please sign in to comment.