Completed
Push — master ( 00092a...39103d )
by wiese
86:17 queued 21:06
created

invalidReturnDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\PaymentContext\Tests\Integration\DataAccess;
6
7
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\McpCreditCardService;
8
use WMDE\Fundraising\Frontend\PaymentContext\Infrastructure\CreditCardExpiry;
9
use WMDE\Fundraising\Frontend\PaymentContext\Infrastructure\CreditCardExpiryFetchingException;
10
11
/**
12
 * @covers \WMDE\Fundraising\Frontend\PaymentContext\DataAccess\McpCreditCardService
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class McpCreditCardServiceTest extends \PHPUnit\Framework\TestCase {
18
19
	const ACCESS_KEY = 'pink fluffy unicorns';
20
	const CUSTOMER_ID = '31333333333333333337';
21
22
	const EXPIRY_MONTH = 5;
23
	const EXPIRY_YEAR = 2020;
24
25
	const VALID_RETURN_DATA = [
26
		'expiryMonth' => self::EXPIRY_MONTH,
27
		'expiryYear' => self::EXPIRY_YEAR,
28
	];
29
30
	public function testMicroPaymentServiceGetsCalledWithAccessKeyAndCustomerId(): void {
31
		$microPaymentServiceMock = $this->getMicroPaymentServiceTestDouble();
32
33
		$microPaymentServiceMock->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in IMcpCreditcardService_v1_5.

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...
34
			->method( 'creditcardDataGet' )
35
			->with(
36
				$this->equalTo( self::ACCESS_KEY ),
37
				$this->equalTo( true ),
38
				$this->equalTo( self::CUSTOMER_ID )
39
			)
40
			->willReturn( self::VALID_RETURN_DATA );
41
42
		$creditCardService = new McpCreditCardService( $microPaymentServiceMock, self::ACCESS_KEY, true );
0 ignored issues
show
Bug introduced by
It seems like $microPaymentServiceMock defined by $this->getMicroPaymentServiceTestDouble() on line 31 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...dService::__construct() does only seem to accept object<IMcpCreditcardSer...<TNvpServiceDispatcher>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
		$creditCardService->getExpirationDate( self::CUSTOMER_ID );
44
	}
45
46
	/**
47
	 * @return \PHPUnit_Framework_MockObject_MockObject|\IMcpCreditcardService_v1_5
48
	 */
49
	private function getMicroPaymentServiceTestDouble() {
50
		return $this->createMock( \IMcpCreditcardService_v1_5::class );
51
	}
52
53
	public function testWhenValidDataIsReturned_creditCardExpiryIsCreated(): void {
54
		$microPaymentServiceStub = $this->getMicroPaymentServiceTestDouble();
55
56
		$microPaymentServiceStub->expects( $this->any() )
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in IMcpCreditcardService_v1_5.

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...
57
			->method( 'creditcardDataGet' )
58
			->willReturn( self::VALID_RETURN_DATA );
59
60
		$creditCardService = new McpCreditCardService( $microPaymentServiceStub, self::ACCESS_KEY, true );
0 ignored issues
show
Bug introduced by
It seems like $microPaymentServiceStub defined by $this->getMicroPaymentServiceTestDouble() on line 54 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...dService::__construct() does only seem to accept object<IMcpCreditcardSer...<TNvpServiceDispatcher>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
62
		$this->assertEquals(
63
			new CreditCardExpiry( self::EXPIRY_MONTH, self::EXPIRY_YEAR ),
64
			$creditCardService->getExpirationDate( self::CUSTOMER_ID )
65
		);
66
	}
67
68
	/**
69
	 * @dataProvider invalidReturnDataProvider
70
	 */
71
	public function testWhenInvalidDataIsReturned_exceptionIsThrown( array $invalidReturnData ): void {
72
		$microPaymentServiceStub = $this->getMicroPaymentServiceTestDouble();
73
74
		$microPaymentServiceStub->expects( $this->any() )
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in IMcpCreditcardService_v1_5.

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...
75
			->method( 'creditcardDataGet' )
76
			->willReturn( $invalidReturnData );
77
78
		$creditCardService = new McpCreditCardService( $microPaymentServiceStub, self::ACCESS_KEY, true );
0 ignored issues
show
Bug introduced by
It seems like $microPaymentServiceStub defined by $this->getMicroPaymentServiceTestDouble() on line 72 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, WMDE\Fundraising\Fronten...dService::__construct() does only seem to accept object<IMcpCreditcardSer...<TNvpServiceDispatcher>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79
80
		$this->expectException( CreditCardExpiryFetchingException::class );
81
		$creditCardService->getExpirationDate( self::CUSTOMER_ID );
82
	}
83
84
	public function invalidReturnDataProvider(): array {
85
		return [
86
			[ [
87
				'expiryMonth' => 'potato',
88
				'expiryYear' => self::EXPIRY_YEAR,
89
			] ],
90
			[ [
91
				'expiryMonth' => 0,
92
				'expiryYear' => self::EXPIRY_YEAR,
93
			] ],
94
			[ [
95
				'expiryMonth' => 13,
96
				'expiryYear' => self::EXPIRY_YEAR,
97
			] ]
98
		];
99
	}
100
101
}