Completed
Push — master ( cbd317...231b94 )
by Henry
09:43
created

tests/phpunit/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
21
class MailerTest extends TestCaseAbstract
22
{
23
	/**
24
	 * setUp
25
	 *
26
	 * @since 3.1.0
27
	 */
28
29
	public function setUp()
30
	{
31
		parent::setUp();
32
		$optionArray =
33
		[
34
			'adminName' => 'Test',
35
			'adminUser' => 'test',
36
			'adminPassword' => 'test',
37
			'adminEmail' => '[email protected]'
38
		];
39
		$installer = $this->installerFactory();
40
		$installer->init();
41
		$installer->rawCreate();
42
		$installer->insertSettings($optionArray);
43
		Stream::setup('root');
44
		$file = new StreamFile('attachment.zip');
45
		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...
46
	}
47
48
	/**
49
	 * tearDown
50
	 *
51
	 * @since 3.1.0
52
	 */
53
54
	public function tearDown()
55
	{
56
		$this->dropDatabase();
57
	}
58
59
	/**
60
	 * testSend
61
	 *
62
	 * @since 2.2.0
63
	 *
64
	 * @param array $toArray
65
	 * @param array $fromArray
66
	 * @param string $subject
67
	 * @param string|array $body
68
	 *
69
	 * @dataProvider providerAutoloader
70
	 */
71
72
	public function testSend(array $toArray = [], array $fromArray = [], string $subject = null, $body = null)
73
	{
74
		/* setup */
75
76
		$mailer = new Mailer();
77
		$mailer->init($toArray, $fromArray, $subject, $body);
78
79
		/* actual */
80
81
		$actual = $mailer->send();
82
83
		/* compare */
84
85
		$this->assertTrue($actual);
86
	}
87
88
	/**
89
	 * testSendAttachment
90
	 *
91
	 * @since 3.1.0
92
	 *
93
	 * @param array $toArray
94
	 * @param array $fromArray
95
	 * @param string $subject
96
	 * @param string|array $body
97
	 *
98
	 * @requires OS Linux
99
	 *
100
	 * @dataProvider providerAutoloader
101
	 */
102
103
	public function testSendAttachment(array $toArray = [], array $fromArray = [], string $subject = null, $body = null)
104
	{
105
		/* setup */
106
107
		$attachmentArray =
108
		[
109
			Stream::url('root' . DIRECTORY_SEPARATOR . 'attachment.zip')
110
		];
111
		$mailer = new Mailer();
112
		$mailer->init($toArray, $fromArray, $subject, $body, $attachmentArray);
113
114
		/* actual */
115
116
		$actual = $mailer->send();
117
118
		/* compare */
119
120
		$this->assertTrue($actual);
121
	}
122
}
123