diff --git a/.travis.yml b/.travis.yml index f54f092..a58d062 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ php: - 7.2 - 7.3 - 7.4 + - 8.0snapshot env: matrix: diff --git a/composer.json b/composer.json index a4fb0b3..912edfd 100644 --- a/composer.json +++ b/composer.json @@ -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": [ { diff --git a/tests/ClickatellHandlerTest.php b/tests/ClickatellHandlerTest.php index 503c383..4e1b30b 100644 --- a/tests/ClickatellHandlerTest.php +++ b/tests/ClickatellHandlerTest.php @@ -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'); } @@ -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; } @@ -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; } @@ -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() @@ -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; } @@ -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() @@ -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) diff --git a/tests/PlivoHandlerTest.php b/tests/PlivoHandlerTest.php index 453809e..2b2c3a0 100644 --- a/tests/PlivoHandlerTest.php +++ b/tests/PlivoHandlerTest.php @@ -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'); } @@ -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; } @@ -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; } @@ -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() @@ -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; } @@ -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() @@ -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) diff --git a/tests/TestCase.php b/tests/TestCase.php index c6a071f..fcd28f7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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 @@ -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); + } + } } diff --git a/tests/TwilioHandlerTest.php b/tests/TwilioHandlerTest.php index 613f623..dd30d2c 100644 --- a/tests/TwilioHandlerTest.php +++ b/tests/TwilioHandlerTest.php @@ -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'); } @@ -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; } @@ -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; } @@ -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() @@ -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; } @@ -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() @@ -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)