Skip to content

Commit

Permalink
Merge pull request #6 from tortuetorche/next
Browse files Browse the repository at this point in the history
PHP 8.0 support 🚀
  • Loading branch information
tylercd100 authored Dec 3, 2020
2 parents efb43ce + 62cdce8 commit 206b7fd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 70 deletions.
14 changes: 4 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: php

php:
- 5.5.9
- 5.5
- 5.6
- 7.0
- hhvm
- 7.2
- 7.3
- 7.4
- 8.0snapshot

env:
matrix:
Expand All @@ -18,8 +17,3 @@ before_script:

script:
- vendor/bin/phpunit
- if [ "$TRAVIS_PHP_VERSION" == "5.5.9" ] || [ "$TRAVIS_PHP_VERSION" == "5.5" ] || [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then vendor/bin/phpunit --coverage-clover build/logs/clover.xml; fi

after_script:
- if [ "$TRAVIS_PHP_VERSION" == "5.5.9" ] || [ "$TRAVIS_PHP_VERSION" == "5.5" ] || [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" == "5.5.9" ] || [ "$TRAVIS_PHP_VERSION" == "5.5" ] || [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi
9 changes: 5 additions & 4 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": "^5.4|^7.0",
"monolog/monolog": "^1.12 || ^2.0"
"php": "^7.2|^8.0",
"monolog/monolog": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0|^5.0"
"phpunit/phpunit": "^8.2.3|^9.0"
},
"authors": [
{
Expand Down
7 changes: 4 additions & 3 deletions src/Handler/SMSHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tylercd100\Monolog\Handler;

use Exception;
use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\SocketHandler;
use Monolog\Logger;
use Tylercd100\Monolog\Formatter\SMSFormatter;
Expand Down Expand Up @@ -81,7 +82,7 @@ public function __construct($authToken, $authId, $fromNumber, $toNumber, $level
* @param array $record
* @return string
*/
protected function generateDataStream($record)
protected function generateDataStream(array $record) :string
{
$content = $this->buildContent($record);
return $this->buildHeader($content) . $content;
Expand Down Expand Up @@ -133,7 +134,7 @@ private function buildHeader($content)
*
* @param array $record
*/
protected function write(array $record)
protected function write(array $record) :void
{
parent::write($record);
$this->closeSocket();
Expand All @@ -142,7 +143,7 @@ protected function write(array $record)
/**
* {@inheritdoc}
*/
protected function getDefaultFormatter()
protected function getDefaultFormatter(): FormatterInterface
{
return new SMSFormatter();
}
Expand Down
31 changes: 15 additions & 16 deletions tests/ClickatellHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ class ClickatellHandlerTest extends TestCase

/** @var ClickatellHandler */
private $handler;

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,18 +90,17 @@ 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)
{
$constructorArgs = array($authToken, $fromNumber, $toNumber, Logger::DEBUG, true, true, $host, $version);
$this->res = fopen('php://memory', 'a');
$this->handler = $this->getMock(
'\Tylercd100\Monolog\Handler\ClickatellHandler',
array('fsockopen', 'streamSetTimeout', 'closeSocket'),
$constructorArgs
);
$this->handler = $this->getMockBuilder(ClickatellHandler::class)
->setMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
->setConstructorArgs($constructorArgs)
->getMock();

$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
Expand Down
33 changes: 16 additions & 17 deletions tests/PlivoHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ class PlivoHandlerTest extends TestCase
private $res;
/** @var PlivoHandler */
private $handler;

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,18 +90,17 @@ 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)
{
$constructorArgs = array($authToken, $authId, $fromNumber, $toNumber, Logger::DEBUG, true, true, $host, $version);
$this->res = fopen('php://memory', 'a');
$this->handler = $this->getMock(
'\Tylercd100\Monolog\Handler\PlivoHandler',
array('fsockopen', 'streamSetTimeout', 'closeSocket'),
$constructorArgs
);
$this->handler = $this->getMockBuilder(PlivoHandler::class)
->setMethods(['fsockopen', 'streamSetTimeout', 'closeSocket'])
->setConstructorArgs($constructorArgs)
->getMock();

$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
Expand Down
43 changes: 40 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +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 @@ -50,11 +53,45 @@ protected function getMultipleRecords()
*/
protected function getIdentityFormatter()
{
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
$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);
}
}
}
Loading

0 comments on commit 206b7fd

Please sign in to comment.