浏览代码

GitterLog sending

mscherer 3 年之前
父节点
当前提交
4f56966f2d

+ 9 - 5
phpunit.xml.dist

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<phpunit bootstrap="tests/bootstrap.php">
+<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
+
 	<php>
 		<ini name="memory_limit" value="-1"/>
 		<ini name="apc.enable_cli" value="1"/>
@@ -7,11 +8,13 @@
 		<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
 		<ini name="error_reporting" value="16383"/>
 	</php>
+
 	<testsuites>
 		<testsuite name="tools">
 			<directory>tests/</directory>
 		</testsuite>
 	</testsuites>
+
 	<listeners>
 		<listener class="Cake\TestSuite\Fixture\FixtureInjector">
 			<arguments>
@@ -19,9 +22,10 @@
 			</arguments>
 		</listener>
 	</listeners>
-	<filter>
-		<whitelist>
+
+	<coverage>
+		<include>
 			<directory suffix=".php">src/</directory>
-		</whitelist>
-	</filter>
+		</include>
+	</coverage>
 </phpunit>

+ 2 - 1
src/Model/Behavior/BitmaskedBehavior.php

@@ -191,10 +191,11 @@ class BitmaskedBehavior extends Behavior {
 	/**
 	 * @param \Cake\Event\EventInterface $event
 	 * @param \Cake\Datasource\EntityInterface $entity
+	 * @param \ArrayObject $data
 	 * @param \ArrayObject $options
 	 * @return void
 	 */
-	public function afterMarshal(EventInterface $event, EntityInterface $entity, ArrayObject $options) {
+	public function afterMarshal(EventInterface $event, EntityInterface $entity, ArrayObject $data, ArrayObject $options) {
 		if ($this->_config['on'] !== 'afterMarshal') {
 			return;
 		}

+ 68 - 0
src/Utility/GitterLog.php

@@ -0,0 +1,68 @@
+<?php
+
+namespace Tools\Utility;
+
+use Cake\Core\Configure;
+use Cake\Http\Client;
+use InvalidArgumentException;
+use Psr\Log\LogLevel;
+
+/**
+ * Wrapper class to log data into Gitter API.
+ *
+ * e.g simple post: curl -d message=hello your_url
+ * e.g error levels: curl -d message=oops -d level=error your_url
+ * e.g markdown: curl --data-urlencode "message=_markdown_ is fun" your_url
+ *
+ * Uses {@link \Cake\Http\Client} to make the API call.
+ */
+class GitterLog {
+
+	/**
+	 * @var string
+	 */
+	protected const URL = 'https://webhooks.gitter.im/e/%s';
+
+	/**
+	 * @param string $message
+	 * @param string|null $level
+	 *
+	 * @return void
+	 */
+	public function write(string $message, ?string $level = null): void {
+		$url = sprintf(static::URL, Configure::readOrFail('Gitter.key'));
+
+		$data = [
+			'message' => $message,
+		];
+		if ($level !== null) {
+			$levelString = $this->levelString($level);
+			$data['level'] = $levelString;
+		}
+
+		$options = [];
+		$client = $this->getClient();
+		$client->post($url, $data, $options);
+	}
+
+	/**
+	 * @return \Cake\Http\Client
+	 */
+	protected function getClient(): Client {
+		return new Client();
+	}
+
+	/**
+	 * @param string $level
+	 *
+	 * @return string
+	 */
+	protected function levelString(string $level): string {
+		if (!in_array($level, [LogLevel::ERROR, LogLevel::INFO], true)) {
+			throw new InvalidArgumentException('Only levels `info` and `error`are allowed.');
+		}
+
+		return $level;
+	}
+
+}

+ 1 - 1
tests/TestCase/Utility/FileLogTest.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace Tools\Test\Utility;
+namespace Tools\Test\TestCase\Utility;
 
 use Shim\TestSuite\TestCase;
 use Tools\Utility\FileLog;

+ 55 - 0
tests/TestCase/Utility/GitterLogTest.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace Tools\Test\TestCase\Utility;
+
+use Cake\Core\Configure;
+use Cake\Http\Client;
+use Cake\Http\Client\Request;
+use Psr\Log\LogLevel;
+use Shim\TestSuite\TestCase;
+use Tools\Utility\GitterLog;
+
+/**
+ * GitterLogTest class
+ */
+class GitterLogTest extends TestCase {
+
+	/**
+	 * @return void
+	 */
+	public function setUp(): void {
+		parent::setUp();
+
+		$key = env('GITTER_KEY') ?: '123';
+		Configure::write('Gitter.key', $key);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function tearDown(): void {
+		parent::tearDown();
+
+		Configure::delete('Gitter.key');
+	}
+
+	/**
+	 * testLogsIntoDefaultFile method
+	 *
+	 * @return void
+	 */
+	public function testLogsIntoDefaultFile(): void {
+		$mockClient = $this->getMockBuilder(Client::class)->onlyMethods(['send'])->getMock();
+
+		$callback = function(Request $value) {
+			return (string)$value->getBody() === 'message=Test%3A+It+%2Aworks%2A+with+some+error+%5Bmarkup%5D%28https%3A%2F%2Fmy-url.com%29%21&level=error';
+		};
+		$mockClient->expects($this->once())->method('send')->with($this->callback($callback));
+
+		$gitterLog = $this->getMockBuilder(GitterLog::class)->onlyMethods(['getClient'])->getMock();
+		$gitterLog->expects($this->once())->method('getClient')->willReturn($mockClient);
+
+		$gitterLog->write('Test: It *works* with some error [markup](https://my-url.com)!', LogLevel::ERROR);
+	}
+
+}