Completed
Pull Request — master (#686)
by Jeroen De
45:27
created

GetInTouchUseCaseTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 79
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A newGetInTouchUseCase() 0 7 1
A testGivenValidRequest_theUserIsNotified() 0 10 1
A newRequest() 0 9 1
A newSucceedingValidator() 0 8 1
A testGivenValidParameters_theyAreContainedInTheEmailToOperator() 0 17 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Tests\Integration\ApplicationContext\UseCases\GetInTouch;
6
7
use WMDE\Fundraising\Frontend\ApplicationContext\UseCases\GetInTouch\GetInTouchRequest;
8
use WMDE\Fundraising\Frontend\ApplicationContext\UseCases\GetInTouch\GetInTouchUseCase;
9
use WMDE\Fundraising\Frontend\Infrastructure\OperatorMailer;
10
use WMDE\Fundraising\Frontend\Infrastructure\TemplateBasedMailer;
11
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Model\EmailAddress;
12
use WMDE\Fundraising\Frontend\Tests\Fixtures\TemplateBasedMailerSpy;
13
use WMDE\Fundraising\Frontend\Validation\GetInTouchValidator;
14
use WMDE\Fundraising\Frontend\Validation\ValidationResult;
15
16
/**
17
 * @covers WMDE\Fundraising\Frontend\ApplicationContext\UseCases\GetInTouch\GetInTouchUseCase
18
 *
19
 * @license GNU GPL v2+
20
 * @author Kai Nissen < [email protected] >
21
 */
22
class GetInTouchUseCaseTest extends \PHPUnit_Framework_TestCase {
23
24
	const INQUIRER_FIRST_NAME = 'Curious';
25
	const INQUIRER_LAST_NAME = 'Guy';
26
	const INQUIRER_EMAIL_ADDRESS = '[email protected]';
27
	const INQUIRY_SUBJECT = 'Please let me know';
28
	const INQUIRY_MESSAGE = 'What is it you do?';
29
30
	private $validator;
31
32
	/** @var OperatorMailer|\PHPUnit_Framework_MockObject_MockObject */
33
	private $operatorMailer;
34
35
	/** @var TemplateBasedMailerSpy */
36
	private $userMailer;
37
38
	public function setUp() {
39
		$this->validator = $this->newSucceedingValidator();
40
		$this->operatorMailer = $this->createMock( OperatorMailer::class );
41
		$this->userMailer = new TemplateBasedMailerSpy( $this );
42
	}
43
44
	private function newGetInTouchUseCase(): GetInTouchUseCase {
45
		return new GetInTouchUseCase(
46
			$this->validator,
47
			$this->operatorMailer,
48
			$this->userMailer
49
		);
50
	}
51
52
	public function testGivenValidParameters_theyAreContainedInTheEmailToOperator() {
53
		$this->operatorMailer->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in WMDE\Fundraising\Fronten...tructure\OperatorMailer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
			->method( 'sendMailToOperator' )
55
			->with(
56
				$this->equalTo( new EmailAddress( self::INQUIRER_EMAIL_ADDRESS ) ),
57
				$this->equalTo( [
58
					'firstName' => self::INQUIRER_FIRST_NAME,
59
					'lastName' => self::INQUIRER_LAST_NAME,
60
					'emailAddress' => self::INQUIRER_EMAIL_ADDRESS,
61
					'subject' => self::INQUIRY_SUBJECT,
62
					'message' => self::INQUIRY_MESSAGE
63
				] )
64
			);
65
66
		$useCase = $this->newGetInTouchUseCase();
67
		$useCase->processContactRequest( $this->newRequest() );
68
	}
69
70
	public function testGivenValidRequest_theUserIsNotified() {
71
		$useCase = $this->newGetInTouchUseCase();
72
		$useCase->processContactRequest( $this->newRequest() );
73
74
		$this->userMailer->assertCalledOnce();
75
		$this->userMailer->assertCalledOnceWith(
76
			new EmailAddress( self::INQUIRER_EMAIL_ADDRESS ),
77
			[]
78
		);
79
	}
80
81
	private function newRequest() {
82
		return new GetInTouchRequest(
83
			self::INQUIRER_FIRST_NAME,
84
			self::INQUIRER_LAST_NAME,
85
			self::INQUIRER_EMAIL_ADDRESS,
86
			self::INQUIRY_SUBJECT,
87
			self::INQUIRY_MESSAGE
88
		);
89
	}
90
91
	private function newSucceedingValidator() {
92
		$validator = $this->getMockBuilder( GetInTouchValidator::class )
93
			->disableOriginalConstructor()
94
			->getMock();
95
		$validator->method( 'validate' )->willReturn( new ValidationResult() );
96
97
		return $validator;
98
	}
99
100
}
101