diff options
author | Marvin Borner | 2018-06-08 20:03:25 +0200 |
---|---|---|
committer | Marvin Borner | 2018-06-08 20:03:25 +0200 |
commit | 92b7dd3335a6572debeacfb5faa82c63a5e67888 (patch) | |
tree | 7ebbca22595d542ec5e2912a24a0400ac8f6b113 /main/app/tests | |
parent | 22a1bb27f94ea33042b0bdd35bef1a5cfa96cc0d (diff) |
Some minor fixes
Diffstat (limited to 'main/app/tests')
-rw-r--r-- | main/app/tests/DatabaseTransactions.php | 94 | ||||
-rw-r--r-- | main/app/tests/TestCase.php | 470 | ||||
-rw-r--r-- | main/app/tests/Unit/ExampleTest.php | 36 |
3 files changed, 300 insertions, 300 deletions
diff --git a/main/app/tests/DatabaseTransactions.php b/main/app/tests/DatabaseTransactions.php index ed2225b..2155c6a 100644 --- a/main/app/tests/DatabaseTransactions.php +++ b/main/app/tests/DatabaseTransactions.php @@ -1,48 +1,48 @@ -<?php -/** - * UserFrosting (http://www.userfrosting.com) - * - * @link https://github.com/userfrosting/UserFrosting - * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) - */ -namespace UserFrosting\Tests; - -/** - * Trait enabling wrapping of each test case in a database transaction - * Based on Laravel `DatabaseTransactions` Traits - * - * @author Louis Charette - */ -trait DatabaseTransactions -{ - /** - * Handle database transactions on the specified connections. - * - * @return void - */ - public function beginDatabaseTransaction() - { - $database = $this->ci['db']; - - foreach ($this->connectionsToTransact() as $name) { - $database->connection($name)->beginTransaction(); - } - - $this->beforeApplicationDestroyed(function () use ($database) { - foreach ($this->connectionsToTransact() as $name) { - $database->connection($name)->rollBack(); - } - }); - } - - /** - * The database connections that should have transactions. - * - * @return array - */ - protected function connectionsToTransact() - { - return property_exists($this, 'connectionsToTransact') - ? $this->connectionsToTransact : [null]; - } +<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Tests;
+
+/**
+ * Trait enabling wrapping of each test case in a database transaction
+ * Based on Laravel `DatabaseTransactions` Traits
+ *
+ * @author Louis Charette
+ */
+trait DatabaseTransactions
+{
+ /**
+ * Handle database transactions on the specified connections.
+ *
+ * @return void
+ */
+ public function beginDatabaseTransaction()
+ {
+ $database = $this->ci['db'];
+
+ foreach ($this->connectionsToTransact() as $name) {
+ $database->connection($name)->beginTransaction();
+ }
+
+ $this->beforeApplicationDestroyed(function () use ($database) {
+ foreach ($this->connectionsToTransact() as $name) {
+ $database->connection($name)->rollBack();
+ }
+ });
+ }
+
+ /**
+ * The database connections that should have transactions.
+ *
+ * @return array
+ */
+ protected function connectionsToTransact()
+ {
+ return property_exists($this, 'connectionsToTransact')
+ ? $this->connectionsToTransact : [null];
+ }
}
\ No newline at end of file diff --git a/main/app/tests/TestCase.php b/main/app/tests/TestCase.php index 1115abe..32dce56 100644 --- a/main/app/tests/TestCase.php +++ b/main/app/tests/TestCase.php @@ -1,236 +1,236 @@ -<?php -/** - * UserFrosting (http://www.userfrosting.com) - * - * @link https://github.com/userfrosting/UserFrosting - * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) - */ -namespace UserFrosting\Tests; - -use Slim\App; -use PHPUnit\Framework\TestCase as BaseTestCase; -use UserFrosting\System\UserFrosting; - -/** - * Class to handle Test - * - * @author Louis Charette - */ -class TestCase extends BaseTestCase -{ - /** - * The Slim application instance. - * - * @var \Slim\App - */ - protected $app; - - /** - * The global container object, which holds all your services. - * - * @var \Interop\Container\ContainerInterface - */ - protected $ci; - - /** - * The callbacks that should be run after the application is created. - * - * @var array - */ - protected $afterApplicationCreatedCallbacks = []; - - /** - * The callbacks that should be run before the application is destroyed. - * - * @var array - */ - protected $beforeApplicationDestroyedCallbacks = []; - - /** - * Indicates if we have made it through the base setUp function. - * - * @var bool - */ - protected $setUpHasRun = false; - - /** - * Setup the test environment. - * - * @return void - */ - protected function setUp() - { - if (!$this->app) { - $this->refreshApplication(); - } - - $this->setUpTraits(); - - foreach ($this->afterApplicationCreatedCallbacks as $callback) { - call_user_func($callback); - } - - $this->setUpHasRun = true; - } - - /** - * Boot the testing helper traits. - * - * @return void - */ - protected function setUpTraits() - { - $uses = array_flip(class_uses_recursive(static::class)); - - /*if (isset($uses[DatabaseMigrations::class])) { - $this->runDatabaseMigrations(); - }*/ - - if (isset($uses[DatabaseTransactions::class])) { - $this->beginDatabaseTransaction(); - } - } - - /** - * Refresh the application instance. - * - * @return void - */ - protected function refreshApplication() - { - // Force setting UF_MODE. This is needed at the moment because Bakery - // uses passthru to invoke PHPUnit. This means that the value of UF_MODE - // has already been set when Bakery was set up, and PHPUnit < 6.3 has - // no way to override environment vars that have already been set. - putenv('UF_MODE=testing'); - - // Setup the sprinkles - $uf = new UserFrosting(); - - // Set argument as false, we are using the CLI - $uf->setupSprinkles(false); - - // Get the container - $this->ci = $uf->getContainer(); - - // Next, we'll instantiate the application. Note that the application is required for the SprinkleManager to set up routes. - $this->app = new App($this->ci); - } - - /** - * Clean up the testing environment before the next test. - * - * @return void - */ - protected function tearDown() - { - if ($this->app) { - foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { - call_user_func($callback); - } - - $this->app = null; - $this->ci = null; - } - - $this->setUpHasRun = false; - - $this->afterApplicationCreatedCallbacks = []; - $this->beforeApplicationDestroyedCallbacks = []; - } - - /** - * Register a callback to be run after the application is created. - * - * @param callable $callback - * @return void - */ - public function afterApplicationCreated(callable $callback) - { - $this->afterApplicationCreatedCallbacks[] = $callback; - - if ($this->setUpHasRun) { - call_user_func($callback); - } - } - - /** - * Asserts that collections are equivalent. - * - * @param array $expected - * @param array $actual - * @param string $message - * @throws PHPUnit_Framework_AssertionFailedError - */ - public static function assertCollectionsSame($expected, $actual, $key = 'id', $message = '') - { - // Check that they have the same number of items - static::assertEquals(count($expected), count($actual)); - - // Sort by primary key - $expected = collect($expected)->sortBy($key); - $actual = collect($actual)->sortBy($key); - - // Check that the keys match - $expectedKeys = $expected->keys()->all(); - $actualKeys = $actual->keys()->all(); - static::assertEquals(sort($expectedKeys), sort($actualKeys)); - - // Check that the array representations of each collection item match - $expected = $expected->values(); - $actual = $actual->values(); - for ($i = 0; $i < count($expected); $i++) { - static::assertEquals( - static::castToComparable($expected[$i]), - static::castToComparable($actual[$i]) - ); - } - } - - /** - * Register a callback to be run before the application is destroyed. - * - * @param callable $callback - * @return void - */ - protected function beforeApplicationDestroyed(callable $callback) - { - $this->beforeApplicationDestroyedCallbacks[] = $callback; - } - - /** - * Helpers - */ - - /** - * Cast an item to an array if it has a toArray() method. - * - * @param $item Collection|mixed[]|mixed - * @return mixed|mixed[] - */ - protected static function castToComparable($item) - { - return (is_object($item) && method_exists($item, 'toArray')) ? $item->toArray() : $item; - } - - /** - * Remove all relations on a collection of models. - */ - protected static function ignoreRelations($models) - { - foreach ($models as $model) { - $model->setRelations([]); - } - } - - protected function cloneObjectArray($original) - { - $cloned = []; - - foreach ($original as $k => $v) { - $cloned[$k] = clone $v; - } - - return $cloned; - } +<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Tests;
+
+use Slim\App;
+use PHPUnit\Framework\TestCase as BaseTestCase;
+use UserFrosting\System\UserFrosting;
+
+/**
+ * Class to handle Test
+ *
+ * @author Louis Charette
+ */
+class TestCase extends BaseTestCase
+{
+ /**
+ * The Slim application instance.
+ *
+ * @var \Slim\App
+ */
+ protected $app;
+
+ /**
+ * The global container object, which holds all your services.
+ *
+ * @var \Interop\Container\ContainerInterface
+ */
+ protected $ci;
+
+ /**
+ * The callbacks that should be run after the application is created.
+ *
+ * @var array
+ */
+ protected $afterApplicationCreatedCallbacks = [];
+
+ /**
+ * The callbacks that should be run before the application is destroyed.
+ *
+ * @var array
+ */
+ protected $beforeApplicationDestroyedCallbacks = [];
+
+ /**
+ * Indicates if we have made it through the base setUp function.
+ *
+ * @var bool
+ */
+ protected $setUpHasRun = false;
+
+ /**
+ * Setup the test environment.
+ *
+ * @return void
+ */
+ protected function setUp()
+ {
+ if (!$this->app) {
+ $this->refreshApplication();
+ }
+
+ $this->setUpTraits();
+
+ foreach ($this->afterApplicationCreatedCallbacks as $callback) {
+ call_user_func($callback);
+ }
+
+ $this->setUpHasRun = true;
+ }
+
+ /**
+ * Boot the testing helper traits.
+ *
+ * @return void
+ */
+ protected function setUpTraits()
+ {
+ $uses = array_flip(class_uses_recursive(static::class));
+
+ /*if (isset($uses[DatabaseMigrations::class])) {
+ $this->runDatabaseMigrations();
+ }*/
+
+ if (isset($uses[DatabaseTransactions::class])) {
+ $this->beginDatabaseTransaction();
+ }
+ }
+
+ /**
+ * Refresh the application instance.
+ *
+ * @return void
+ */
+ protected function refreshApplication()
+ {
+ // Force setting UF_MODE. This is needed at the moment because Bakery
+ // uses passthru to invoke PHPUnit. This means that the value of UF_MODE
+ // has already been set when Bakery was set up, and PHPUnit < 6.3 has
+ // no way to override environment vars that have already been set.
+ putenv('UF_MODE=testing');
+
+ // Setup the sprinkles
+ $uf = new UserFrosting();
+
+ // Set argument as false, we are using the CLI
+ $uf->setupSprinkles(false);
+
+ // Get the container
+ $this->ci = $uf->getContainer();
+
+ // Next, we'll instantiate the application. Note that the application is required for the SprinkleManager to set up routes.
+ $this->app = new App($this->ci);
+ }
+
+ /**
+ * Clean up the testing environment before the next test.
+ *
+ * @return void
+ */
+ protected function tearDown()
+ {
+ if ($this->app) {
+ foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
+ call_user_func($callback);
+ }
+
+ $this->app = null;
+ $this->ci = null;
+ }
+
+ $this->setUpHasRun = false;
+
+ $this->afterApplicationCreatedCallbacks = [];
+ $this->beforeApplicationDestroyedCallbacks = [];
+ }
+
+ /**
+ * Register a callback to be run after the application is created.
+ *
+ * @param callable $callback
+ * @return void
+ */
+ public function afterApplicationCreated(callable $callback)
+ {
+ $this->afterApplicationCreatedCallbacks[] = $callback;
+
+ if ($this->setUpHasRun) {
+ call_user_func($callback);
+ }
+ }
+
+ /**
+ * Asserts that collections are equivalent.
+ *
+ * @param array $expected
+ * @param array $actual
+ * @param string $message
+ * @throws PHPUnit_Framework_AssertionFailedError
+ */
+ public static function assertCollectionsSame($expected, $actual, $key = 'id', $message = '')
+ {
+ // Check that they have the same number of items
+ static::assertEquals(count($expected), count($actual));
+
+ // Sort by primary key
+ $expected = collect($expected)->sortBy($key);
+ $actual = collect($actual)->sortBy($key);
+
+ // Check that the keys match
+ $expectedKeys = $expected->keys()->all();
+ $actualKeys = $actual->keys()->all();
+ static::assertEquals(sort($expectedKeys), sort($actualKeys));
+
+ // Check that the array representations of each collection item match
+ $expected = $expected->values();
+ $actual = $actual->values();
+ for ($i = 0; $i < count($expected); $i++) {
+ static::assertEquals(
+ static::castToComparable($expected[$i]),
+ static::castToComparable($actual[$i])
+ );
+ }
+ }
+
+ /**
+ * Register a callback to be run before the application is destroyed.
+ *
+ * @param callable $callback
+ * @return void
+ */
+ protected function beforeApplicationDestroyed(callable $callback)
+ {
+ $this->beforeApplicationDestroyedCallbacks[] = $callback;
+ }
+
+ /**
+ * Helpers
+ */
+
+ /**
+ * Cast an item to an array if it has a toArray() method.
+ *
+ * @param $item Collection|mixed[]|mixed
+ * @return mixed|mixed[]
+ */
+ protected static function castToComparable($item)
+ {
+ return (is_object($item) && method_exists($item, 'toArray')) ? $item->toArray() : $item;
+ }
+
+ /**
+ * Remove all relations on a collection of models.
+ */
+ protected static function ignoreRelations($models)
+ {
+ foreach ($models as $model) {
+ $model->setRelations([]);
+ }
+ }
+
+ protected function cloneObjectArray($original)
+ {
+ $cloned = [];
+
+ foreach ($original as $k => $v) {
+ $cloned[$k] = clone $v;
+ }
+
+ return $cloned;
+ }
}
\ No newline at end of file diff --git a/main/app/tests/Unit/ExampleTest.php b/main/app/tests/Unit/ExampleTest.php index 4ac3e84..1a2dc40 100644 --- a/main/app/tests/Unit/ExampleTest.php +++ b/main/app/tests/Unit/ExampleTest.php @@ -1,19 +1,19 @@ -<?php - -namespace UserFrosting\Tests\Unit; - -use UserFrosting\Tests\TestCase; -use UserFrosting\Tests\DatabaseTransactions; - -class ExampleTest extends TestCase -{ - /** - * A basic test example. - * - * @return void - */ - public function testBasicTest() - { - $this->assertTrue(true); - } +<?php
+
+namespace UserFrosting\Tests\Unit;
+
+use UserFrosting\Tests\TestCase;
+use UserFrosting\Tests\DatabaseTransactions;
+
+class ExampleTest extends TestCase
+{
+ /**
+ * A basic test example.
+ *
+ * @return void
+ */
+ public function testBasicTest()
+ {
+ $this->assertTrue(true);
+ }
}
\ No newline at end of file |