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

EmailReporterTest::testNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 14
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\EmailReporter;
7
use Overwatch\UserBundle\DataFixtures\ORM\UserFixtures;
8
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase;
9
10
/**
11
 * EmailReporterTest
12
 * Functional Test for the operation of the EmailReporter
13
 */
14
class EmailReporterTest extends DatabaseAwareTestCase
15
{
16
    const FROM_MAIL = '[email protected]';
17
    
18
    private $reporter;
19
    private $mailerSpy;
20
21
    public function setUp()
22
    {
23
        parent::setUp();
24
        
25
        $this->reporter = new EmailReporter(
26
            $this->getMockedEnvironment(),
27
            [
28
                'enabled'     => true,
29
                'report_from' => self::FROM_MAIL
30
            ]
31
        );
32
    }
33
    
34
    public function testNotification()
35
    {
36
        $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...
37
        
38
        $this->reporter->notify($result);
39
        $this->assertCount(1, $this->mailerSpy->getInvocations());
40
        
41
        $message = $this->mailerSpy->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...
42
        $this->assertEquals(
43
            $result->getTest()->getName() . ' ' . $result->getStatus(),
44
            $message->getSubject()
45
        );
46
        
47
        
48
        $this->assertEquals([self::FROM_MAIL => null], $message->getFrom());
49
        $this->assertEquals([UserFixtures::$users['user-1']->getEmail() => null], $message->getTo());
50
        $this->assertContains($result->getTest()->getName(), $message->getBody());
51
        $this->assertContains($result->getStatus(), $message->getBody());
52
        $this->assertContains($result->getInfo(), $message->getBody());
53
        $this->assertContains($result->getCreatedAt()->format('F j, Y H:i'), $message->getBody());
54
    }
55
    
56
    public function testDisabled()
57
    {
58
        $reporter = new EmailReporter(
59
            $this->getMockedEnvironment(),
60
            [
61
                'enabled'     => false,
62
                'report_from' => self::FROM_MAIL
63
            ]
64
        );
65
        
66
        $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...
67
        
68
        $reporter->notify($result);
69
        $this->assertCount(0, $this->mailerSpy->getInvocations());
70
    }
71
    
72
    private function getMockedEnvironment()
73
    {
74
        $mailer = $this->getMockBuilder('\Swift_Mailer')
75
            ->disableOriginalConstructor()
76
            ->getMock();
77
        $mailer
78
            ->expects($this->mailerSpy = $this->any())
79
            ->method('send')
80
            ->willReturn(1)
81
        ;
82
                
83
        $customContainer = $this->getContainer();
84
        $customContainer->set('swiftmailer.mailer.default', $mailer);
85
        
86
        return $customContainer;
87
    }
88
}
89