Conditions | 3 |
Paths | 4 |
Total Lines | 89 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | public function testApplyTicket() |
||
23 | { |
||
24 | //test if service url is not valid |
||
25 | $user = Mockery::mock(User::class) |
||
|
|||
26 | ->shouldReceive('getAttribute') |
||
27 | ->withArgs(['id']) |
||
28 | ->andReturn(1) |
||
29 | ->shouldReceive('getEloquentModel') |
||
30 | ->andReturn(Mockery::self()) |
||
31 | ->getMock(); |
||
32 | |||
33 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
34 | ->shouldReceive('getServiceByUrl') |
||
35 | ->andReturn(false) |
||
36 | ->getMock(); |
||
37 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
38 | try { |
||
39 | app()->make(TicketRepository::class)->applyTicket($user, 'what ever'); |
||
40 | } catch (Exception $e) { |
||
41 | $this->assertInstanceOf(CasException::class, $e); |
||
42 | $this->assertEquals($e->getCasErrorCode(), CasException::INVALID_SERVICE); |
||
43 | } |
||
44 | |||
45 | //test if get available ticket failed |
||
46 | $service = Mockery::mock(Service::class) |
||
47 | ->shouldReceive('getAttribute') |
||
48 | ->withArgs(['id']) |
||
49 | ->andReturn(1) |
||
50 | ->getMock(); |
||
51 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
52 | ->shouldReceive('getServiceByUrl') |
||
53 | ->andReturn($service) |
||
54 | ->getMock(); |
||
55 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
56 | $ticketRepository = $this->initTicketRepository() |
||
57 | ->makePartial() |
||
58 | ->shouldAllowMockingProtectedMethods() |
||
59 | ->shouldReceive('getAvailableTicket') |
||
60 | ->andReturn(false) |
||
61 | ->getMock(); |
||
62 | |||
63 | try { |
||
64 | $ticketRepository->applyTicket($user, 'what ever'); |
||
65 | } catch (Exception $e) { |
||
66 | $this->assertInstanceOf(CasException::class, $e); |
||
67 | $this->assertEquals($e->getCasErrorCode(), CasException::INTERNAL_ERROR); |
||
68 | $this->assertEquals($e->getMessage(), 'apply ticket failed'); |
||
69 | } |
||
70 | |||
71 | //normal |
||
72 | $ticketStr = 'ST-abc'; |
||
73 | $serviceUrl = 'what ever'; |
||
74 | $service = Mockery::mock(Service::class) |
||
75 | ->shouldReceive('getAttribute') |
||
76 | ->withArgs(['id']) |
||
77 | ->andReturn(1) |
||
78 | ->getMock(); |
||
79 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
80 | ->shouldReceive('getServiceByUrl') |
||
81 | ->andReturn($service) |
||
82 | ->getMock(); |
||
83 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
84 | $ticket = Mockery::mock(Ticket::class) |
||
85 | ->shouldReceive('newInstance') |
||
86 | ->andReturnUsing( |
||
87 | function ($param) { |
||
88 | $obj = Mockery::mock(); |
||
89 | $obj->shouldReceive('user->associate'); |
||
90 | $obj->shouldReceive('service->associate'); |
||
91 | $obj->shouldReceive('save'); |
||
92 | $obj->ticket = $param['ticket']; |
||
93 | $obj->service_url = $param['service_url']; |
||
94 | |||
95 | return $obj; |
||
96 | } |
||
97 | ) |
||
98 | ->getMock(); |
||
99 | app()->instance(Ticket::class, $ticket); |
||
100 | $ticketRepository = $this->initTicketRepository() |
||
101 | ->makePartial() |
||
102 | ->shouldAllowMockingProtectedMethods() |
||
103 | ->shouldReceive('getAvailableTicket') |
||
104 | ->andReturn($ticketStr) |
||
105 | ->getMock(); |
||
106 | |||
107 | $record = $ticketRepository->applyTicket($user, $serviceUrl); |
||
108 | $this->assertEquals($ticketStr, $record->ticket); |
||
109 | $this->assertEquals($serviceUrl, $record->service_url); |
||
110 | } |
||
111 | |||
190 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.