Phantom42 has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/404400 )
Change subject: Add PHPUnit tests
......................................................................
Add PHPUnit tests
This adds tests for SendGridHooks.
Please note that SendGridHooks code was changed to make
sendgrid object mockable.
Additionally, UnitTestsList hook was added.
Bug: T183572
Change-Id: Iabbcb66467b180bab7250d27b869037d5bc57f5f
---
M SendGridHooks.php
M extension.json
A tests/phpunit/SendGridHooksTest.php
3 files changed, 133 insertions(+), 10 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SendGrid
refs/changes/00/404400/1
diff --git a/SendGridHooks.php b/SendGridHooks.php
index 1b3947a..d2a1f09 100644
--- a/SendGridHooks.php
+++ b/SendGridHooks.php
@@ -43,17 +43,22 @@
array $to,
MailAddress $from,
$subject,
- $body
+ $body,
+ $sendgrid=null
) {
- $conf = RequestContext::getMain()->getConfig();
+ if ( $sendgrid == null ) {
+ $conf = RequestContext::getMain()->getConfig();
- // Value gotten from "wgSendGridAPIKey" variable from
LocalSettings.php
- $sendgridAPIKey = $conf->get( 'SendGridAPIKey' );
+ // Value gotten from "wgSendGridAPIKey" variable from
LocalSettings.php
+ $sendgridAPIKey = $conf->get( 'SendGridAPIKey' );
- if ( $sendgridAPIKey == "" ) {
- throw new MWException(
- 'Please update your LocalSettings.php with the
correct SendGrid API key.'
- );
+ if ( $sendgridAPIKey == "" ) {
+ throw new MWException(
+ 'Please update your LocalSettings.php
with the correct SendGrid API key.'
+ );
+ }
+
+ $sendgrid = new \SendGrid( $sendgridAPIKey );
}
// Get $to and $from email addresses from the array and
MailAddress object respectively
@@ -61,7 +66,6 @@
$to = new SendGrid\Email( null, $to[0]->address );
$body = new SendGrid\Content( "text/plain", $body );
$mail = new SendGrid\Mail( $from, $subject, $to, $body );
- $sendgrid = new \SendGrid( $sendgridAPIKey );
try {
$sendgrid->client->mail()->send()->post( $mail );
@@ -72,4 +76,30 @@
return false;
}
+ /**
+ * Handler for UnitTestsList hook.
+ * @see http://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
+ * @param array &$files Array of unit test files
+ * @return bool true in all cases
+ */
+ public static function onUnitTestsList( &$files ) {
+ // @codeCoverageIgnoreStart
+ $directoryIterator = new RecursiveDirectoryIterator( __DIR__ .
'/tests/' );
+
+ /**
+ * @var SplFileInfo $fileInfo
+ */
+ $ourFiles = [];
+ foreach ( new RecursiveIteratorIterator( $directoryIterator )
as $fileInfo ) {
+ if ( substr( $fileInfo->getFilename(), -8 ) ===
'Test.php' ) {
+ $ourFiles[] = $fileInfo->getPathname();
+ }
+ }
+
+ $files = array_merge( $files, $ourFiles );
+
+ return true;
+ // @codeCoverageIgnoreEnd
+ }
+
}
diff --git a/extension.json b/extension.json
index 1cde31d..4d3f7ac 100644
--- a/extension.json
+++ b/extension.json
@@ -18,7 +18,8 @@
"Hooks": {
"AlternateUserMailer": [
"SendGridHooks::onAlternateUserMailer"
- ]
+ ],
+ "UnitTestsList": "SendGridHooks::onUnitTestsList"
},
"load_composer_autoloader": true,
"config": {
diff --git a/tests/phpunit/SendGridHooksTest.php
b/tests/phpunit/SendGridHooksTest.php
new file mode 100644
index 0000000..be2f0b5
--- /dev/null
+++ b/tests/phpunit/SendGridHooksTest.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Test for hooks code.
+ *
+ * @file
+ * @license GPL-2.0+
+ */
+
+class SendGridHooksTest extends MediaWikiTestCase {
+
+ /**
+ * Test that onAlternateUserMailer throws Exception if api key is missing.
+ */
+ public function testOnAlternateUserMailerNoApiKey() {
+ $this->setExpectedException(
+ MWException::class,
+ 'Please update your LocalSettings.php with the correct SendGrid
API key.'
+ );
+
+ RequestContext::getMain()->setConfig( new MultiConfig( [
+ new HashConfig( [
+ 'SendGridAPIKey' => '',
+ ] ),
+ ] ) );
+
+ SendGridHooks::onAlternateUserMailer(
+ [ 'SomeHeader' => 'SomeValue' ],
+ [ new MailAddress( '[email protected]' ) ],
+ new MailAddress( '[email protected]' ),
+ 'Some subject',
+ 'Email body'
+ );
+ }
+
+ /**
+ * Test sending mail in onAlternateUserMailer hook.
+ */
+ public function testOnAlternateUserMailer() {
+ $mock = $this->getMockBuilder( 'SendGrid' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $mock->client = $this->getMockBuilder( 'SendGrid\Client' )
+ ->setMethods( array( 'mail', 'send', 'post' ) )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $mock->client->expects( $this->once() )
+ ->method( 'mail' )
+ ->will( $this->returnValue( $mock->client ) );
+ $mock->client->expects( $this->once() )
+ ->method( 'send' )
+ ->will( $this->returnValue( $mock->client ) );
+ $mock->client->expects( $this->once() )
+ ->method( 'post' )
+ ->with( $this->callback( function ( $email ) {
+ $this->assertSame(
+ '[email protected]',
+ $email->from->getEmail()
+ );
+ $this->assertCount( 1, $email->personalization );
+ $this->assertCount( 1, $email->personalization[0]->getTos() );
+ $this->assertSame(
+ '[email protected]',
+ $email->personalization[0]->getTos()[0]->getEmail()
+ );
+ $this->assertSame( 'Some subject', $email->subject );
+ $this->assertCount( 1, $email->contents );
+ $this->assertSame(
+ 'text/plain',
+ $email->contents[0]->getType()
+ );
+ $this->assertSame(
+ 'Email body',
+ $email->contents[0]->getValue()
+ );
+ return true;
+ } ) );
+
+
+ $result = SendGridHooks::onAlternateUserMailer(
+ [ 'SomeHeader' => 'SomeValue' ],
+ [ new MailAddress( '[email protected]' ) ],
+ new MailAddress( '[email protected]' ),
+ 'Some subject',
+ 'Email body',
+ $mock
+ );
+
+ $this->assertSame( false, $result );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/404400
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iabbcb66467b180bab7250d27b869037d5bc57f5f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SendGrid
Gerrit-Branch: master
Gerrit-Owner: Phantom42 <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits