Skip to content

Commit

Permalink
Test eklendi.
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanevrens committed Feb 9, 2020
1 parent 6911de8 commit 1801826
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
/vendor
.idea
.phpunit.result.cache
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.7/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Karma Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./Karma/</directory>
</whitelist>
</filter>
</phpunit>
142 changes: 141 additions & 1 deletion tests/ContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?php
<?php namespace Karma\Tests;

use DI\NotFoundException;
use Karma\AppFactory;
use Karma\Container;
use Karma\ContainerBuilder;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\App;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Slim\Routing\RouteCollector;
use Slim\Routing\RouteResolver;

final class ContainerTest extends TestCase
{
Expand All @@ -13,11 +21,18 @@ final class ContainerTest extends TestCase
*/
private $container;

/**
* @var App
*/
private $app;

protected function setUp(): void
{
parent::setUp();

$this->container = ContainerBuilder::build();

$this->app = AppFactory::create($this->container);
}

public function testRequest()
Expand All @@ -35,4 +50,129 @@ public function testResponse()
$this->container->get('response')
);
}

/**
* Test `get()` returns custum item
*/
public function testCustomItem()
{
$container = ContainerBuilder::build(Container::class, [
'DB_NAME' => 'testdb'
]);
$this->assertEquals('testdb', $container->get('DB_NAME'));
}

/**
* Test `get()` returns overrided item
*/
public function testOverridedItem()
{
$container = ContainerBuilder::build(Container::class, [
'settings.httpVersion' => '1.2'
]);
$this->assertEquals('1.2', $container->get('settings.httpVersion'));
}

/**
* Test `get()` throws error if item does not exist
*/
public function testGetWithValueNotFoundError()
{
$this->expectException(NotFoundException::class);

$this->container->get('foo');
}

/**
* Test `get()` throws something that is a ContainerExpception - typically a NotFoundException, when there is a DI
* config error
*/
public function testGetWithDiConfigErrorThrownAsContainerValueNotFoundException()
{
$this->expectException(NotFoundException::class);

$container = ContainerBuilder::build();
$container['foo'] = function (ContainerInterface $container) {
return $container->get('doesnt-exist');
};
$container->get('foo');
}

/**
* Test `get()` recasts \InvalidArgumentException as ContainerInterop-compliant exceptions when an error is present
* in the DI config
*/
public function testGetWithDiConfigErrorThrownAsInvalidArgumentException()
{
$this->expectException(NotFoundException::class);

$container = ContainerBuilder::build();
$container['foo'] = function (ContainerInterface $container) {
return $container['doesnt-exist'];
};
$container->get('foo');
}

/**
* Test `get()` does not recast exceptions which are thrown in a factory closure
*/
public function testGetWithErrorThrownByFactoryClosure()
{
$this->expectException(\InvalidArgumentException::class);

$invokable = $this->getMockBuilder('StdClass')->setMethods(['__invoke'])->getMock();

/** @var Callable $invokable */
$invokable->expects($this->any())
->method('__invoke')
->will($this->throwException(new \InvalidArgumentException()));

$container = ContainerBuilder::build();
$container['foo'] = function (ContainerInterface $container) use ($invokable) {
call_user_func($invokable);
};
$container->get('foo');
}

/**
* Test container has request
*/
public function testGetRequest()
{
$this->assertInstanceOf(RequestInterface::class, $this->container['request']);
}

/**
* Test container has response
*/
public function testGetResponse()
{
$this->assertInstanceOf(ResponseInterface::class, $this->container['response']);
}

/**
* Test container has router
*/
public function testGetRouter()
{
$this->assertInstanceOf(RouteResolver::class, $this->app->getRouteResolver());
}

//Test __isset
public function testMagicIssetMethod()
{
$this->assertEquals(false, $this->container->__isset('notfoundkey'));
}

//test __get
public function testMagicGetMethod()
{
$this->container->set('settings.httpVersion', '1.2');
$this->assertSame('1.2', $this->container->__get('settings.httpVersion'));
}

public function testRouteCacheDisabledByDefault()
{
$this->assertNull($this->app->getRouteCollector()->getCacheFile());
}
}
14 changes: 14 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// Set timezone
date_default_timezone_set('UTC');

// Prevent session cookies
ini_set('session.use_cookies', 0);

// Enable Composer autoloader
/** @var \Composer\Autoload\ClassLoader $autoloader */
$autoloader = require dirname(__DIR__) . '/vendor/autoload.php';

// Register test classes
$autoloader->addPsr4('Karma\Tests\\', __DIR__);

0 comments on commit 1801826

Please sign in to comment.