Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

tests/unit/MailerTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Tests;
3
4
use org\bovigo\vfs\vfsStream as Stream;
5
use org\bovigo\vfs\vfsStreamFile as StreamFile;
6
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
7
use Redaxscript\Mailer;
8
9
/**
10
 * MailerTest
11
 *
12
 * @since 2.2.0
13
 *
14
 * @package Redaxscript
15
 * @category Tests
16
 * @author Henry Ruhs
17
 *
18
 * @covers Redaxscript\Mailer
19
 *
20
 * @requires OS Linux
21
 */
22
23
class MailerTest extends TestCaseAbstract
24
{
25
	/**
26
	 * setUp
27
	 *
28
	 * @since 3.1.0
29
	 */
30
31
	public function setUp() : void
32
	{
33
		parent::setUp();
34
		$optionArray = $this->getOptionArray();
35
		$installer = $this->installerFactory();
36
		$installer->init();
37
		$installer->rawCreate();
38
		$installer->insertSettings($optionArray);
39
		Stream::setup('root');
40
		$file = new StreamFile('attachment.zip');
41
		StreamWrapper::getRoot()->addChild($file);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface org\bovigo\vfs\vfsStreamContent as the method addChild() does only exist in the following implementations of said interface: org\bovigo\vfs\DotDirectory, org\bovigo\vfs\vfsStreamDirectory.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
42
	}
43
44
	/**
45
	 * tearDown
46
	 *
47
	 * @since 3.1.0
48
	 */
49
50
	public function tearDown() : void
51
	{
52
		$this->dropDatabase();
53
	}
54
55
	/**
56
	 * testSend
57
	 *
58
	 * @since 2.2.0
59
	 *
60
	 * @param array $toArray
61
	 * @param array $fromArray
62
	 * @param string $subject
63
	 * @param string|array $body
64
	 *
65
	 * @dataProvider providerAutoloader
66
	 */
67
68
	public function testSend(array $toArray = [], array $fromArray = [], string $subject = null, $body = null) : void
69
	{
70
		/* setup */
71
72
		$mailer = new Mailer();
73
		$mailer->init($toArray, $fromArray, $subject, $body);
74
75
		/* actual */
76
77
		$actual = $mailer->send();
78
79
		/* compare */
80
81
		$this->assertTrue($actual);
82
	}
83
84
	/**
85
	 * testSendAttachment
86
	 *
87
	 * @since 3.1.0
88
	 *
89
	 * @param array $toArray
90
	 * @param array $fromArray
91
	 * @param string $subject
92
	 * @param string|array $body
93
	 *
94
	 * @requires OS Linux
95
	 *
96
	 * @dataProvider providerAutoloader
97
	 */
98
99
	public function testSendAttachment(array $toArray = [], array $fromArray = [], string $subject = null, $body = null) : void
100
	{
101
		/* setup */
102
103
		$attachmentArray =
104
		[
105
			Stream::url('root' . DIRECTORY_SEPARATOR . 'attachment.zip')
106
		];
107
		$mailer = new Mailer();
108
		$mailer->init($toArray, $fromArray, $subject, $body, $attachmentArray);
109
110
		/* actual */
111
112
		$actual = $mailer->send();
113
114
		/* compare */
115
116
		$this->assertTrue($actual);
117
	}
118
}
119