| 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 | { |
||
| 23 | //test if service url is not valid |
||
| 24 | $user = Mockery::mock(User::class) |
||
| 25 | ->shouldReceive('getAttribute') |
||
| 26 | ->withArgs(['id']) |
||
| 27 | ->andReturn(1) |
||
| 28 | ->shouldReceive('getEloquentModel') |
||
| 29 | ->andReturn(Mockery::self()) |
||
| 30 | ->getMock(); |
||
| 31 | |||
| 32 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
| 33 | ->shouldReceive('getServiceByUrl') |
||
| 34 | ->andReturn(false) |
||
| 35 | ->getMock(); |
||
| 36 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
| 37 | try { |
||
| 38 | app()->make(TicketRepository::class)->applyTicket($user, 'what ever'); |
||
| 39 | } catch (Exception $e) { |
||
| 40 | $this->assertInstanceOf(CasException::class, $e); |
||
| 41 | $this->assertEquals($e->getCasErrorCode(), CasException::INVALID_SERVICE); |
||
| 42 | } |
||
| 43 | |||
| 44 | //test if get available ticket failed |
||
| 45 | $service = Mockery::mock(Service::class) |
||
| 46 | ->shouldReceive('getAttribute') |
||
| 47 | ->withArgs(['id']) |
||
| 48 | ->andReturn(1) |
||
| 49 | ->getMock(); |
||
| 50 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
| 51 | ->shouldReceive('getServiceByUrl') |
||
| 52 | ->andReturn($service) |
||
| 53 | ->getMock(); |
||
| 54 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
| 55 | $ticketRepository = $this->initTicketRepository() |
||
| 56 | ->makePartial() |
||
| 57 | ->shouldAllowMockingProtectedMethods() |
||
| 58 | ->shouldReceive('getAvailableTicket') |
||
| 59 | ->andReturn(false) |
||
| 60 | ->getMock(); |
||
| 61 | |||
| 62 | try { |
||
| 63 | $ticketRepository->applyTicket($user, 'what ever'); |
||
| 64 | } catch (Exception $e) { |
||
| 65 | $this->assertInstanceOf(CasException::class, $e); |
||
| 66 | $this->assertEquals($e->getCasErrorCode(), CasException::INTERNAL_ERROR); |
||
| 67 | $this->assertEquals($e->getMessage(), 'apply ticket failed'); |
||
| 68 | } |
||
| 69 | |||
| 70 | //normal |
||
| 71 | $ticketStr = 'ST-abc'; |
||
| 72 | $serviceUrl = 'what ever'; |
||
| 73 | $service = Mockery::mock(Service::class) |
||
| 74 | ->shouldReceive('getAttribute') |
||
| 75 | ->withArgs(['id']) |
||
| 76 | ->andReturn(1) |
||
| 77 | ->getMock(); |
||
| 78 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
| 79 | ->shouldReceive('getServiceByUrl') |
||
| 80 | ->andReturn($service) |
||
| 81 | ->getMock(); |
||
| 82 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
| 83 | $ticket = Mockery::mock(Ticket::class) |
||
| 84 | ->shouldReceive('newInstance') |
||
| 85 | ->andReturnUsing( |
||
| 86 | function ($param) { |
||
| 87 | $obj = Mockery::mock(); |
||
| 88 | $obj->shouldReceive('user->associate'); |
||
| 89 | $obj->shouldReceive('service->associate'); |
||
| 90 | $obj->shouldReceive('save'); |
||
| 91 | $obj->ticket = $param['ticket']; |
||
| 92 | $obj->service_url = $param['service_url']; |
||
| 93 | |||
| 94 | return $obj; |
||
| 95 | } |
||
| 96 | ) |
||
| 97 | ->getMock(); |
||
| 98 | app()->instance(Ticket::class, $ticket); |
||
| 99 | $ticketRepository = $this->initTicketRepository() |
||
| 100 | ->makePartial() |
||
| 101 | ->shouldAllowMockingProtectedMethods() |
||
| 102 | ->shouldReceive('getAvailableTicket') |
||
| 103 | ->andReturn($ticketStr) |
||
| 104 | ->getMock(); |
||
| 105 | |||
| 106 | $record = $ticketRepository->applyTicket($user, $serviceUrl); |
||
| 107 | $this->assertEquals($ticketStr, $record->ticket); |
||
| 108 | $this->assertEquals($serviceUrl, $record->service_url); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function testGetByTicket() |
||
| 189 |