Skip to content

Feature/unit tests company #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/Http/Middleware/TrackRequestMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function __construct(LogManager $logger)
*/
public function handle(Request $request, Closure $next)
{
if(env('APP_ENV') === 'testing') {
// Skip tracking in testing environment
return $next($request);
}
try {
// generating dynamic id for span with configurable prefix
$spanId = env('TRACE_SPAN_PREFIX', 'SPAN') . '_' . Str::uuid();
Expand Down Expand Up @@ -73,6 +77,10 @@ public function handle(Request $request, Closure $next)
*/
public function terminate(Request $request, Response $response): void
{
if(env('APP_ENV') === 'testing') {
// Skip tracking in testing environment
return;
}
try {
$endTime = microtime(true);
$responseTime = intval(($endTime - $this->startTime) * 1000);
Expand Down
57 changes: 57 additions & 0 deletions tests/SetupEntityMananger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace Tests;
/**
* Copyright 2020 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use LaravelDoctrine\ORM\Facades\EntityManager;
use LaravelDoctrine\ORM\Facades\Registry;
use models\utils\SilverstripeBaseModel;
use Illuminate\Support\Facades\DB;

/**
* Trait InsertMemberTestData
*/
trait SetupEntityMananger
{
/**
* @var EntityManager
*/
static $em;


/**
* SetupEntityManager constructor.
*/
protected static function setupEntityManager()
{
DB::setDefaultConnection("model");

self::$em = Registry::getManager(SilverstripeBaseModel::EntityManager);
if (!self::$em->isOpen()) {
self::$em = Registry::resetManager(SilverstripeBaseModel::EntityManager);
}
}

protected static function tearDownEntityManager()
{
try {
if (!self::$em->isOpen()) {
self::$em = Registry::resetManager(SilverstripeBaseModel::EntityManager);
}

self::$em->flush();

} catch (\Exception $ex) {

}
}
}
67 changes: 67 additions & 0 deletions tests/Unit/Companies/CompanyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php namespace Tests\Unit\Companies;

/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use models\main\Company;
use Tests\BrowserKitTestCase;
use Tests\SetupEntityMananger;

/**
* Class CompanyTest
* @package Tests\unit
*/
class CompanyTest extends BrowserKitTestCase
{
use SetupEntityMananger;

/**
* @throws \Exception
*/
protected function setUp():void
{
parent::setUp();
self::setupEntityManager();
}

public function tearDown():void
{
self::tearDownEntityManager();
parent::tearDown();
}

public function test()
{
$company = new Company();
$company->setName('Test Company');
$company->setDescription('Test Company desc');
$company->setCity('Test City');
$company->setCountry('Test Country');
$company->setUrl('www.google.com');

self::$em->persist($company);
self::$em->flush();

$repo = self::$em->getRepository(Company::class);
$found = $repo->find($company->getId());

$this->assertInstanceOf(Company::class, $found);
$this->assertEquals($company->getName(), $found->getName());
$this->assertEquals($company->getDescription(), $found->getDescription());
$this->assertEquals($company->getCity(), $found->getCity());
$this->assertEquals($company->getCountry(), $found->getCountry());
$this->assertEquals($company->getUrl(), $found->getUrl());

self::$em->remove($found);
}
}
71 changes: 71 additions & 0 deletions tests/Unit/Companies/ProjectSponsorshipTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php namespace Tests\Unit\Companies;

/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use models\main\Company;
use models\main\ProjectSponsorshipType;
use Tests\BrowserKitTestCase;
use Tests\SetupEntityMananger;

/**
* Class ProjectSponsorshipTypeTest
* @package Tests\unit
*/
class ProjectSponsorshipTypeTest extends BrowserKitTestCase
{
use SetupEntityMananger;

/**
* @throws \Exception
*/
protected function setUp():void
{
parent::setUp();
self::setupEntityManager();
}

public function tearDown():void
{
self::tearDownEntityManager();
parent::tearDown();
}

public function test()
{
$child_entity = new Company();
$child_entity->setName('Child Company Test');

self::$em->persist($child_entity);

$entity = new ProjectSponsorshipType();
$entity->setName('Test Sponsorship Type');
$entity->setDescription('Test Sponsorship Type desc');
$entity->setIsActive(true);
$entity->setOrder(1);
$supporting_company = $entity->addSupportingCompany($child_entity);

self::$em->persist($entity);
self::$em->flush();

$repo = self::$em->getRepository(ProjectSponsorshipType::class);
$found = $repo->find($entity->getId());

$this->assertInstanceOf(ProjectSponsorshipType::class, $found);
$this->assertEquals($entity->getName(), $found->getName());
$this->assertEquals($entity->getDescription(), $found->getDescription());
$this->assertEquals($supporting_company, $found->getSupportingCompanyByCompany($child_entity));

self::$em->remove($found);
}
}
65 changes: 65 additions & 0 deletions tests/Unit/Companies/SponsoredProjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php namespace Tests\Unit\Companies;

/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use models\main\Company;
use models\main\ProjectSponsorshipType;
use models\main\SponsoredProject;
use Tests\BrowserKitTestCase;
use Tests\SetupEntityMananger;

/**
* Class SponsoredProjectTest
* @package Tests\unit
*/
class SponsoredProjectTest extends BrowserKitTestCase
{
use SetupEntityMananger;

/**
* @throws \Exception
*/
protected function setUp():void
{
parent::setUp();
self::setupEntityManager();
}

public function tearDown():void
{
self::tearDownEntityManager();
parent::tearDown();
}

public function test()
{

$entity = new SponsoredProject();
$entity->setName('Test Sponsorship Type');
$entity->setDescription('Test Sponsorship Type desc');
$entity->setIsActive(true);

self::$em->persist($entity);
self::$em->flush();

$repo = self::$em->getRepository(SponsoredProject::class);
$found = $repo->find($entity->getId());

$this->assertInstanceOf(SponsoredProject::class, $found);
$this->assertEquals($entity->getName(), $found->getName());
$this->assertEquals($entity->getDescription(), $found->getDescription());

self::$em->remove($found);
}
}
73 changes: 73 additions & 0 deletions tests/Unit/Companies/SupportingCompanyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php namespace Tests\Unit\Companies;

/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use models\main\Company;
use models\main\ProjectSponsorshipType;
use models\main\SupportingCompany;
use Tests\BrowserKitTestCase;
use Tests\SetupEntityMananger;

/**
* Class SupportingCompanyTest
* @package Tests\unit
*/
class SupportingCompanyTest extends BrowserKitTestCase
{
use SetupEntityMananger;

/**
* @throws \Exception
*/
protected function setUp():void
{
parent::setUp();
self::setupEntityManager();
}

public function tearDown():void
{
self::tearDownEntityManager();
parent::tearDown();
}

public function test()
{
$child_entity = new Company();
$child_entity->setName('Test Company');
self::$em->persist($child_entity);

$child_entity_2 = new ProjectSponsorshipType();
$child_entity_2->setName('Test Project Type');
$child_entity_2->setOrder(1);
self::$em->persist($child_entity_2);

$entity = new SupportingCompany();
$entity->setOrder(1);
$entity->setCompany($child_entity);
$entity->setSponsorshipType($child_entity_2);

self::$em->persist($entity);
self::$em->flush();

$repo = self::$em->getRepository(SupportingCompany::class);
$found = $repo->find($entity->getId());

$this->assertInstanceOf(SupportingCompany::class, $found);
$this->assertEquals($entity->getCompany(), $found->getCompany());
$this->assertEquals($entity->getSponsorshipType(), $found->getSponsorshipType());

self::$em->remove($found);
}
}
Loading