Refactor phpunit testspull/3904/head
parent
49dfe38186
commit
6190860d71
@ -0,0 +1,43 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Traits; |
||||
|
||||
use Chamilo\CoreBundle\Entity\AccessUrl; |
||||
use Doctrine\ORM\EntityManagerInterface; |
||||
use Symfony\Component\HttpFoundation\RequestStack; |
||||
|
||||
trait AccessUrlListenerTrait |
||||
{ |
||||
protected ?AccessUrl $accessUrl = null; |
||||
|
||||
public function getAccessUrl(EntityManagerInterface $em, RequestStack $request): ?AccessUrl |
||||
{ |
||||
if (null === $this->accessUrl) { |
||||
$request = $request->getCurrentRequest(); |
||||
if (null === $request) { |
||||
return null; |
||||
} |
||||
|
||||
$sessionRequest = $request->getSession(); |
||||
$id = (int) $sessionRequest->get('access_url_id'); |
||||
if (0 !== $id) { |
||||
/** @var AccessUrl $url */ |
||||
$url = $em->getRepository(AccessUrl::class)->find($id); |
||||
|
||||
if (null !== $url) { |
||||
$this->accessUrl = $url; |
||||
|
||||
return $url; |
||||
} |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
return $this->accessUrl; |
||||
} |
||||
} |
||||
@ -0,0 +1,60 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\Tests\CoreBundle\Repository\Node; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CoreBundle\Repository\SessionRepository; |
||||
use Chamilo\Tests\ChamiloTestTrait; |
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
||||
|
||||
/** |
||||
* @covers \SessionRepository |
||||
*/ |
||||
class SessionRepositoryTest extends WebTestCase |
||||
{ |
||||
use ChamiloTestTrait; |
||||
|
||||
/** |
||||
* Create a session. |
||||
*/ |
||||
public function testCreate(): void |
||||
{ |
||||
self::bootKernel(); |
||||
/** @var SessionRepository $repo */ |
||||
$repo = self::getContainer()->get(SessionRepository::class); |
||||
|
||||
$session = $this->createSession('session'); |
||||
|
||||
$this->assertHasNoEntityViolations($session); |
||||
|
||||
$count = $repo->count([]); |
||||
|
||||
$this->assertSame(1, $count); |
||||
} |
||||
|
||||
public function testCreateSessionSameTitle(): void |
||||
{ |
||||
self::bootKernel(); |
||||
$name = 'session'; |
||||
$session = $this->createSession($name); |
||||
$this->assertHasNoEntityViolations($session); |
||||
|
||||
$repo = self::getContainer()->get(SessionRepository::class); |
||||
|
||||
$session = (new Session()) |
||||
->setName($name) |
||||
->setGeneralCoach($this->getUser('admin')) |
||||
->addAccessUrl($this->getAccessUrl()) |
||||
; |
||||
$errors = $this->getViolations($session); |
||||
$this->assertSame(1, \count($errors)); |
||||
|
||||
$this->expectException(UniqueConstraintViolationException::class); |
||||
$repo->create($session); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue