From 86c0713baa2f616bf1dff6d9dbe0ea68b1c00e91 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 20 Sep 2017 01:09:11 +0200 Subject: Added helpers unit test --- tests/Unit/HelpersTest.php | 152 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/Unit/HelpersTest.php (limited to 'tests/Unit/HelpersTest.php') diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php new file mode 100644 index 00000000..d9782888 --- /dev/null +++ b/tests/Unit/HelpersTest.php @@ -0,0 +1,152 @@ +getAppMock('some.name', $class); + + $this->assertEquals($appMock, app()); + $this->assertEquals($class, app('some.name')); + } + + /** + * @covers \config + */ + public function testConfig() + { + $configMock = $this->getMockBuilder(Config::class) + ->getMock(); + + $this->getAppMock('config', $configMock); + $this->assertEquals($configMock, config()); + + $configMock->expects($this->once()) + ->method('set') + ->with(['foo' => 'bar']); + + $this->assertTrue(config(['foo' => 'bar'])); + + $configMock->expects($this->once()) + ->method('get') + ->with('mail') + ->willReturn(['user' => 'FooBar']); + + $this->assertEquals(['user' => 'FooBar'], config('mail')); + } + + /** + * @covers \request + */ + public function testRequest() + { + $requestMock = $this->getMockBuilder(Request::class) + ->getMock(); + + $this->getAppMock('request', $requestMock); + $this->assertEquals($requestMock, request()); + + $requestMock->expects($this->once()) + ->method('input') + ->with('requestKey') + ->willReturn('requestValue'); + + $this->assertEquals('requestValue', request('requestKey')); + } + + /** + * @covers \session + */ + public function testSession() + { + $sessionMock = $this->getMockBuilder(Session::class) + ->getMock(); + + $this->getAppMock('session', $sessionMock); + $this->assertEquals($sessionMock, session()); + + $sessionMock->expects($this->once()) + ->method('get') + ->with('someKey') + ->willReturn('someValue'); + + $this->assertEquals('someValue', session('someKey')); + } + + /** + * @covers \view + */ + public function testView() + { + $rendererMock = $this->getMockBuilder(Renderer::class) + ->getMock(); + + $this->getAppMock('renderer', $rendererMock); + $this->assertEquals($rendererMock, view()); + + $rendererMock->expects($this->once()) + ->method('render') + ->with('template.name', ['template' => 'data']) + ->willReturn('rendered template'); + + $this->assertEquals('rendered template', view('template.name', ['template' => 'data'])); + } + + /** + * @covers \url + */ + public function testUrl() + { + $urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class) + ->getMock(); + + $this->getAppMock('routing.urlGenerator', $urlGeneratorMock); + $this->assertEquals($urlGeneratorMock, url()); + + $urlGeneratorMock->expects($this->once()) + ->method('to') + ->with('foo/bar', ['param' => 'value']) + ->willReturn('http://lorem.ipsum/foo/bar?param=value'); + + $this->assertEquals('http://lorem.ipsum/foo/bar?param=value', url('foo/bar', ['param' => 'value'])); + } + + /** + * @param string $alias + * @param object $object + * @return Application|\PHPUnit_Framework_MockObject_MockObject + */ + protected function getAppMock($alias, $object) + { + $appMock = $this->getMockBuilder(Container::class) + ->getMock(); + + $appMock->expects($this->atLeastOnce()) + ->method('get') + ->with($alias) + ->willReturn($object); + + /** @var $appMock Application */ + Application::setInstance($appMock); + + return $appMock; + } +} -- cgit v1.2.3-54-g00ecf From 449e2cdd00632acff63bb75c5282c3aa2642b59f Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 25 Sep 2017 00:03:22 +0200 Subject: Added env function, added GitLab CI code coverage config --- .gitlab-ci.yml | 47 ++++++++++++++++++++++++++++++++++++++++++++++ config/config.default.php | 8 ++++---- src/helpers.php | 15 +++++++++++++++ tests/Unit/HelpersTest.php | 14 ++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 .gitlab-ci.yml (limited to 'tests/Unit/HelpersTest.php') diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 00000000..c44507b1 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,47 @@ +image: php + +cache: + paths: + - .composer + +services: + - mysql:5.6 + +variables: + MYSQL_DATABASE: engelsystem + MYSQL_USER: engel + MYSQL_PASSWORD: engelsystem + COMPOSER_HOME: .composer + MYSQL_RANDOM_ROOT_PASSWORD: "yes" + +before_script: + # Install required Packages + - apt-get update -yqq + - apt-get install -yqq git unzip mysql-client + - docker-php-ext-install pdo pdo_mysql gettext + # Install xdebug + - pecl install xdebug + - docker-php-ext-enable xdebug + # MySQL DB + - mysql -h mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/install.sql + - mysql -h mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/update.sql + # Install Composer + - curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer + - /usr/local/bin/composer --no-ansi install + +.test_template: &test_definition + artifacts: + name: "${CI_JOB_NAME}_${CI_PROJECT_ID}_${PHP_VERSION}" + expire_in: 1 week + paths: + - ./coverage/ + coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/' + script: vendor/bin/phpunit --colors=never --coverage-text --coverage-html ./coverage/ + +test:7.0: + image: php:7.0 + <<: *test_definition + +test:7.1: + image: php:7.1 + <<: *test_definition diff --git a/config/config.default.php b/config/config.default.php index c2d742ef..1bad9668 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -5,10 +5,10 @@ return [ // MySQL-Connection Settings 'database' => [ - 'host' => 'localhost', - 'user' => 'root', - 'pw' => '', - 'db' => 'engelsystem', + 'host' => env('MYSQL_HOST', (env('CI', false) ? 'mysql' : 'localhost')), + 'user' => env('MYSQL_USER', 'root'), + 'pw' => env('MYSQL_PASSWORD', ''), + 'db' => env('MYSQL_DATABASE', 'engelsystem'), ], // For accessing stats diff --git a/src/helpers.php b/src/helpers.php index c3c727ec..5a48498a 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -64,6 +64,21 @@ function config_path($path = '') return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); } +/** + * @param string $key + * @param mixed $default + * @return mixed + */ +function env($key, $default = null) +{ + $value = getenv($key); + if ($value === false) { + return $default; + } + + return $value; +} + /** * @param string $key * @param mixed $default diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index d9782888..9ec824af 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -53,6 +53,20 @@ class HelpersTest extends TestCase $this->assertEquals(['user' => 'FooBar'], config('mail')); } + /** + * @covers \env + */ + public function testEnv() + { + putenv('envTestVar=someContent'); + + $env = env('envTestVar'); + $this->assertEquals('someContent', $env); + + $env = env('someRandomEnvVarThatShouldNeverExist', 'someDefaultValue'); + $this->assertEquals('someDefaultValue', $env); + } + /** * @covers \request */ -- cgit v1.2.3-54-g00ecf From e15e86362585f5d00d118653232584ed0920e533 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 31 Oct 2017 14:23:23 +0100 Subject: Added tests for base_path and config_path --- .../DatabaseServiceProviderConnectionTest.php | 2 +- tests/Feature/Logger/EngelsystemLoggerTest.php | 2 +- tests/Feature/Model/LogEntriesModelTest.php | 38 +++++++++++++++++++ tests/Feature/Model/RoomModelTest.php | 40 ++++++++++++++++++++ tests/Feature/model/LogEntriesModelTest.php | 38 ------------------- tests/Feature/model/RoomModelTest.php | 40 -------------------- tests/Unit/ApplicationTest.php | 2 +- tests/Unit/Config/ConfigServiceProviderTest.php | 2 +- tests/Unit/Config/ConfigTest.php | 2 +- .../Unit/Database/DatabaseServiceProviderTest.php | 2 +- .../Exceptions/ExceptionsServiceProviderTest.php | 2 +- tests/Unit/HelpersTest.php | 43 +++++++++++++++++++++- tests/Unit/Logger/LoggerServiceProviderTest.php | 2 +- tests/Unit/Renderer/HtmlEngineTest.php | 2 +- .../Unit/Renderer/RendererServiceProviderTest.php | 2 +- tests/Unit/Renderer/RendererTest.php | 6 ++- tests/Unit/Routing/RoutingServiceProviderTest.php | 2 +- tests/Unit/Routing/UrlGeneratorTest.php | 2 +- 18 files changed, 136 insertions(+), 93 deletions(-) create mode 100644 tests/Feature/Model/LogEntriesModelTest.php create mode 100644 tests/Feature/Model/RoomModelTest.php delete mode 100644 tests/Feature/model/LogEntriesModelTest.php delete mode 100644 tests/Feature/model/RoomModelTest.php (limited to 'tests/Unit/HelpersTest.php') diff --git a/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php b/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php index dd1ce729..636fba2e 100644 --- a/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php +++ b/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php @@ -1,6 +1,6 @@ assertNotFalse(LogEntry_create(LogLevel::WARNING, 'test_LogEntry_create')); + + // There should be one more log entry now + $this->assertEquals(count(LogEntries()), $count + 1); + } + + public function testClearAllLogEntries() + { + LogEntry_create(LogLevel::WARNING, 'test'); + $this->assertTrue(count(LogEntries()) > 0); + + $this->assertNotFalse(LogEntries_clear_all()); + $this->assertCount(0, LogEntries()); + } + + public function tearDown() + { + LogEntries_clear_all(); + } +} diff --git a/tests/Feature/Model/RoomModelTest.php b/tests/Feature/Model/RoomModelTest.php new file mode 100644 index 00000000..20b9e34d --- /dev/null +++ b/tests/Feature/Model/RoomModelTest.php @@ -0,0 +1,40 @@ +room_id = Room_create('test', false, true, ''); + } + + public function test_Room() + { + $this->create_Room(); + + $room = Room($this->room_id); + + $this->assertNotFalse($room); + $this->assertNotNull($room); + $this->assertEquals($room['Name'], 'test'); + + $this->assertNull(Room(-1)); + } + + public function tearDown() + { + if ($this->room_id != null) { + Room_delete($this->room_id); + } + } +} diff --git a/tests/Feature/model/LogEntriesModelTest.php b/tests/Feature/model/LogEntriesModelTest.php deleted file mode 100644 index 6d7b0ebc..00000000 --- a/tests/Feature/model/LogEntriesModelTest.php +++ /dev/null @@ -1,38 +0,0 @@ -assertNotFalse(LogEntry_create(LogLevel::WARNING, 'test_LogEntry_create')); - - // There should be one more log entry now - $this->assertEquals(count(LogEntries()), $count + 1); - } - - public function testClearAllLogEntries() - { - LogEntry_create(LogLevel::WARNING, 'test'); - $this->assertTrue(count(LogEntries()) > 0); - - $this->assertNotFalse(LogEntries_clear_all()); - $this->assertCount(0, LogEntries()); - } - - public function tearDown() - { - LogEntries_clear_all(); - } -} diff --git a/tests/Feature/model/RoomModelTest.php b/tests/Feature/model/RoomModelTest.php deleted file mode 100644 index 96be84a2..00000000 --- a/tests/Feature/model/RoomModelTest.php +++ /dev/null @@ -1,40 +0,0 @@ -room_id = Room_create('test', false, true, ''); - } - - public function test_Room() - { - $this->create_Room(); - - $room = Room($this->room_id); - - $this->assertNotFalse($room); - $this->assertNotNull($room); - $this->assertEquals($room['Name'], 'test'); - - $this->assertNull(Room(-1)); - } - - public function tearDown() - { - if ($this->room_id != null) { - Room_delete($this->room_id); - } - } -} diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php index 78310134..f58483ea 100644 --- a/tests/Unit/ApplicationTest.php +++ b/tests/Unit/ApplicationTest.php @@ -1,6 +1,6 @@ assertEquals($class, app('some.name')); } + /** + * @covers \base_path() + */ + public function testBasePath() + { + /** @var MockObject|Application $app */ + $app = $this->getMockBuilder(Container::class) + ->getMock(); + Application::setInstance($app); + + $app->expects($this->atLeastOnce()) + ->method('get') + ->with('path') + ->willReturn('/foo/bar'); + + $this->assertEquals('/foo/bar', base_path()); + $this->assertEquals('/foo/bar/bla-foo.conf', base_path('bla-foo.conf')); + } + /** * @covers \config */ @@ -53,6 +73,25 @@ class HelpersTest extends TestCase $this->assertEquals(['user' => 'FooBar'], config('mail')); } + /** + * @covers \config_path() + */ + public function testConfigPath() + { + /** @var MockObject|Application $app */ + $app = $this->getMockBuilder(Container::class) + ->getMock(); + Application::setInstance($app); + + $app->expects($this->atLeastOnce()) + ->method('get') + ->with('path.config') + ->willReturn('/foo/conf'); + + $this->assertEquals('/foo/conf', config_path()); + $this->assertEquals('/foo/conf/bar.php', config_path('bar.php')); + } + /** * @covers \env */ @@ -146,7 +185,7 @@ class HelpersTest extends TestCase /** * @param string $alias * @param object $object - * @return Application|\PHPUnit_Framework_MockObject_MockObject + * @return Application|MockObject */ protected function getAppMock($alias, $object) { diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php index 66f63cf4..cef95d5b 100644 --- a/tests/Unit/Logger/LoggerServiceProviderTest.php +++ b/tests/Unit/Logger/LoggerServiceProviderTest.php @@ -1,6 +1,6 @@ getMockForAbstractClass(EngineInterface::class); $nullRenderer->expects($this->atLeastOnce()) @@ -20,6 +22,7 @@ class RendererTest extends TestCase ->willReturn(false); $renderer->addRenderer($nullRenderer); + /** @var MockObject|EngineInterface $mockRenderer */ $mockRenderer = $this->getMockForAbstractClass(EngineInterface::class); $mockRenderer->expects($this->atLeastOnce()) @@ -42,6 +45,7 @@ class RendererTest extends TestCase { $renderer = new Renderer(); + /** @var MockObject|LoggerInterface $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); $loggerMock ->expects($this->once()) diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php index bb2a1d65..dd9441eb 100644 --- a/tests/Unit/Routing/RoutingServiceProviderTest.php +++ b/tests/Unit/Routing/RoutingServiceProviderTest.php @@ -1,6 +1,6 @@ Date: Sun, 12 Nov 2017 15:38:12 +0100 Subject: TestFixes --- tests/Unit/HelpersTest.php | 3 +++ tests/Unit/Http/SessionServiceProviderTest.php | 2 ++ 2 files changed, 5 insertions(+) (limited to 'tests/Unit/HelpersTest.php') diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index 0a8d5d2b..43c29c84 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -11,6 +11,7 @@ use Engelsystem\Routing\UrlGenerator; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface as StorageInterface; class HelpersTest extends TestCase { @@ -130,7 +131,9 @@ class HelpersTest extends TestCase */ public function testSession() { + $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); $sessionMock = $this->getMockBuilder(Session::class) + ->setConstructorArgs([$sessionStorage]) ->getMock(); $this->getAppMock('session', $sessionMock); diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php index 0f17a1af..a78b4f72 100644 --- a/tests/Unit/Http/SessionServiceProviderTest.php +++ b/tests/Unit/Http/SessionServiceProviderTest.php @@ -109,7 +109,9 @@ class SessionServiceProviderTest extends ServiceProviderTest */ private function getSessionMock() { + $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); return $this->getMockBuilder(Session::class) + ->setConstructorArgs([$sessionStorage]) ->setMethods(['start']) ->getMock(); } -- cgit v1.2.3-54-g00ecf