Failed Conditions
Push — master ( b9670c...4ba57f )
by Zac
16:20 queued 37s
created

SmsReporterTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Overwatch\ServiceBundle\Tests\Reporter;
4
5
use Overwatch\ResultBundle\DataFixtures\ORM\TestResultFixtures;
6
use Overwatch\ServiceBundle\Reporter\SmsReporter;
7
use Overwatch\UserBundle\DataFixtures\ORM\UserFixtures;
8
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase;
9
10
/**
11
 * SmsReporterTest
12
 */
13
class SmsReporterTest extends DatabaseAwareTestCase
14
{
15
    const FROM_TEL = '+CCXXXXXXXXXX';
16
    
17
    private $reporter;
18
    private $messageSpy;
19
    
20
    public function setUp()
21
    {
22
        parent::setUp();
23
24
        $twilio = $this->getMockBuilder('Services_Twilio')
25
            ->disableOriginalConstructor()
26
            ->getMock();
27
28
        $twilio->account = $twilio;
29
30
        $messagesMock = $this->getMockBuilder('Services_Twilio_Rest_Messages')
31
            ->disableOriginalConstructor()
32
            ->getMock();
33
        $messagesMock->expects($this->messageSpy = $this->any())
34
            ->method('sendMessage')
35
            ->willReturn(true);
36
37
        $twilio->messages = $messagesMock;
38
39
        $this->reporter = new SmsReporter(
40
            $this->getContainer(),
41
            $this->getTwilioConfig(),
42
            $twilio
43
        );
44
    }
45
    
46
    public function testNotification()
47
    {
48
        $result = $this->em->find("Overwatch\ResultBundle\Entity\TestResult", TestResultFixtures::$results['result-3']->getId());
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Overwatch\ResultBundle\Entity\TestResult does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
49
        
50
        $this->reporter->notify($result);
51
        $this->assertCount(1, $this->messageSpy->getInvocations());
52
        
53
        $message = $this->messageSpy->getInvocations()[0]->parameters[2];
0 ignored issues
show
Bug introduced by
Accessing parameters on the interface PHPUnit_Framework_MockObject_Invocation suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
        $this->assertEquals(
55
            $result->getTest()->getName() . ' ' . $result->getStatus() . ': Me gusta success kid upvoting Obama first world problems.' . PHP_EOL,
56
            $message
57
        );
58
        
59
        
60
        $this->assertEquals(self::FROM_TEL, $this->messageSpy->getInvocations()[0]->parameters[0]);
0 ignored issues
show
Bug introduced by
Accessing parameters on the interface PHPUnit_Framework_MockObject_Invocation suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
61
        $this->assertEquals(UserFixtures::$users['user-1']->getTelephoneNumber(), $this->messageSpy->getInvocations()[0]->parameters[1]);
0 ignored issues
show
Bug introduced by
Accessing parameters on the interface PHPUnit_Framework_MockObject_Invocation suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
62
    }
63
    
64
    public function testDisabled()
65
    {
66
        $reporter = new SmsReporter(
67
            $this->getContainer(),
68
            $this->getTwilioConfig(false)
69
        );
70
        
71
        $result = $this->em->find("Overwatch\ResultBundle\Entity\TestResult", TestResultFixtures::$results['result-3']->getId());
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Overwatch\ResultBundle\Entity\TestResult does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
72
        
73
        $reporter->notify($result);
74
        $this->assertCount(0, $this->messageSpy->getInvocations());
75
    }
76
    
77
    private function getTwilioConfig($enabled = true)
78
    {
79
        return [
80
            'enabled'            => $enabled,
81
            'twilio_account_sid' => 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
82
            'twilio_auth_token'  => 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
83
            'twilio_from_number' => self::FROM_TEL
84
        ];
85
    }
86
}
87