'Foo', 'text' => 'foo', 'is_meeting' => false, 'user_id' => 1, ] ]; /** @var TestLogger */ protected $log; /** @var Response|MockObject */ protected $response; /** @var Request */ protected $request; /** * @covers \Engelsystem\Controllers\Admin\NewsController::edit */ public function testEditHtmlWarning() { $this->request->attributes->set('id', 1); $this->response->expects($this->once()) ->method('withView') ->willReturnCallback(function ($view, $data) { $this->assertEquals('pages/news/edit.twig', $view); /** @var Collection $warnings */ $warnings = $data['warnings']; $this->assertNotEmpty($data['news']); $this->assertTrue($warnings->isNotEmpty()); $this->assertEquals('news.edit.contains-html', $warnings->first()); return $this->response; }); $this->addUser(); /** @var NewsController $controller */ $controller = $this->app->make(NewsController::class); $controller->edit($this->request); } /** * @covers \Engelsystem\Controllers\Admin\NewsController::__construct * @covers \Engelsystem\Controllers\Admin\NewsController::edit */ public function testEdit() { $this->request->attributes->set('id', 1); $this->response->expects($this->once()) ->method('withView') ->willReturnCallback(function ($view, $data) { $this->assertEquals('pages/news/edit.twig', $view); /** @var Collection $warnings */ $warnings = $data['warnings']; $this->assertNotEmpty($data['news']); $this->assertTrue($warnings->isEmpty()); return $this->response; }); $this->auth->expects($this->once()) ->method('can') ->with('admin_news_html') ->willReturn(true); /** @var NewsController $controller */ $controller = $this->app->make(NewsController::class); $controller->edit($this->request); } /** * @covers \Engelsystem\Controllers\Admin\NewsController::save */ public function testSaveCreateInvalid() { /** @var NewsController $controller */ $controller = $this->app->make(NewsController::class); $controller->setValidator(new Validator()); $this->expectException(ValidationException::class); $controller->save($this->request); } /** * @return array */ public function saveCreateEditProvider(): array { return [ ['Some test', true, true, 'Some test'], ['Some test', false, false, 'Some test'], ['Some test', false, true, 'Some test', 1], ['Some test', true, false, 'Some test', 1], ]; } /** * @covers \Engelsystem\Controllers\Admin\NewsController::save * @dataProvider saveCreateEditProvider * * @param string $text * @param bool $isMeeting * @param bool $canEditHtml * @param string $result * @param int|null $id */ public function testSaveCreateEdit( string $text, bool $isMeeting, bool $canEditHtml, string $result, int $id = null ) { $this->request->attributes->set('id', $id); $id = $id ?: 2; $body = [ 'title' => 'Some Title', 'text' => $text, ]; if ($isMeeting) { $body['is_meeting'] = '1'; } $this->request = $this->request->withParsedBody($body); $this->addUser(); $this->auth->expects($this->once()) ->method('can') ->with('admin_news_html') ->willReturn($canEditHtml); $this->response->expects($this->once()) ->method('redirectTo') ->with('http://localhost/news/' . $id) ->willReturn($this->response); /** @var NewsController $controller */ $controller = $this->app->make(NewsController::class); $controller->setValidator(new Validator()); $controller->save($this->request); $this->assertTrue($this->log->hasInfoThatContains('Updated')); /** @var Session $session */ $session = $this->app->get('session'); $messages = $session->get('messages'); $this->assertEquals('news.edit.success', $messages[0]); $news = (new News())->find($id); $this->assertEquals($result, $news->text); $this->assertEquals($isMeeting, (bool)$news->is_meeting); } /** * @covers \Engelsystem\Controllers\Admin\NewsController::save */ public function testSaveDelete() { $this->request->attributes->set('id', 1); $this->request = $this->request->withParsedBody([ 'title' => '.', 'text' => '.', 'delete' => '1', ]); $this->response->expects($this->once()) ->method('redirectTo') ->with('http://localhost/news') ->willReturn($this->response); /** @var NewsController $controller */ $controller = $this->app->make(NewsController::class); $controller->setValidator(new Validator()); $controller->save($this->request); $this->assertTrue($this->log->hasInfoThatContains('Deleted')); /** @var Session $session */ $session = $this->app->get('session'); $messages = $session->get('messages'); $this->assertEquals('news.delete.success', $messages[0]); } /** * Setup environment */ public function setUp(): void { parent::setUp(); $this->initDatabase(); $this->request = Request::create('http://localhost'); $this->app->instance('request', $this->request); $this->app->instance(Request::class, $this->request); $this->app->instance(ServerRequestInterface::class, $this->request); $this->response = $this->createMock(Response::class); $this->app->instance(Response::class, $this->response); $this->log = new TestLogger(); $this->app->instance(LoggerInterface::class, $this->log); $this->app->instance('session', new Session(new MockArraySessionStorage())); $this->auth = $this->createMock(Authenticator::class); $this->app->instance(Authenticator::class, $this->auth); $this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class); $this->app->instance('config', new Config()); (new News([ 'title' => 'Foo', 'text' => 'foo', 'is_meeting' => false, 'user_id' => 1, ]))->save(); } /** * Creates a new user */ protected function addUser() { $user = new User([ 'name' => 'foo', 'password' => '', 'email' => '', 'api_key' => '', 'last_login_at' => null, ]); $user->forceFill(['id' => 42]); $user->save(); $this->auth->expects($this->any()) ->method('user') ->willReturn($user); } }