repository = m::mock(NodeRepositoryInterface::class); $this->serverRepository = m::mock(ServerRepositoryInterface::class); $this->translator = m::mock(Translator::class); $this->service = new NodeDeletionService( $this->repository, $this->serverRepository, $this->translator ); } /** * Test that a node is deleted if there are no servers attached to it. */ public function testNodeIsDeletedIfNoServersAreAttached() { $this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() ->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0); $this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1); $this->assertEquals(1, $this->service->handle(1)); } /** * Test that an exception is thrown if servers are attached to the node. */ public function testExceptionIsThrownIfServersAreAttachedToNode() { $this->expectException(DisplayException::class); $this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() ->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(1); $this->translator->shouldReceive('trans')->with('exceptions.node.servers_attached')->once()->andReturnNull(); $this->repository->shouldNotReceive('delete'); $this->service->handle(1); } /** * Test that a model can be passed into the handle function rather than an ID. */ public function testModelCanBePassedToFunctionInPlaceOfNodeId() { $node = factory(Node::class)->make(['id' => 123]); $this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() ->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0); $this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(1); $this->assertEquals(1, $this->service->handle($node)); } }