Skip to content
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

Code standards updated to PHP 7.1 #40

Merged
merged 2 commits into from
May 27, 2021
Merged
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
9 changes: 2 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.1, 7.2, 7.3, 7.4, 8.0]

steps:
- name: Checkout code
Expand All @@ -24,13 +24,8 @@ jobs:
- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies on PHP 7
if: matrix.php < 8
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-interaction

- name: Install dependencies on PHP 8
if: matrix.php == 8
run: composer install --prefer-dist --no-progress --no-interaction --ignore-platform-req=php

- name: Run test suite
run: php vendor/bin/codecept run
22 changes: 12 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name":"codeception/module-doctrine2",
"description":"Doctrine2 module for Codeception",
"keywords":["codeception", "doctrine2"],
"homepage":"http://codeception.com/",
"type":"library",
"license":"MIT",
"authors":[
"name": "codeception/module-doctrine2",
"description": "Doctrine2 module for Codeception",
"keywords": [ "codeception", "doctrine2" ],
"homepage": "https://codeception.com/",
"type": "library",
"license": "MIT",
"authors": [
{
"name":"Michael Bodnarchuk"
},
Expand All @@ -15,7 +15,9 @@
],
"minimum-stability": "RC",
"require": {
"php": ">=5.6.0 <9.0",
"php": "^7.1 || ^8.0",
"ext-pdo": "*",
"ext-json": "*",
"codeception/codeception": "^4.0"
},
"require-dev": {
Expand All @@ -24,8 +26,8 @@
"doctrine/data-fixtures": "^1",
"ramsey/uuid-doctrine": "^1.5"
},
"autoload":{
"classmap": ["src/"]
"autoload": {
"classmap": [ "src/" ]
},
"config": {
"classmap-authoritative": true
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ A Doctrine 2 module for Codeception.
[![Total Downloads](https://poser.pugx.org/codeception/module-doctrine2/downloads)](https://packagist.org/packages/codeception/module-doctrine2)
[![License](https://poser.pugx.org/codeception/module-doctrine2/license)](/LICENSE)

## Requirements

* `PHP 7.1` or higher.

## Installation

```
Expand Down
82 changes: 27 additions & 55 deletions src/Codeception/Module/Doctrine2.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codeception\Module;

use Codeception\Exception\ModuleConfigException;
Expand All @@ -8,7 +10,6 @@
use Codeception\Lib\Interfaces\DataMapper;
use Codeception\Lib\Interfaces\DependsOnModule;
use Codeception\Lib\Interfaces\DoctrineProvider;
use Codeception\Lib\Notification;
use Codeception\Module as CodeceptionModule;
use Codeception\TestInterface;
use Codeception\Util\ReflectionPropertyAccessor;
Expand Down Expand Up @@ -164,7 +165,7 @@ class Doctrine2 extends CodeceptionModule implements DependsOnModule, DataMapper
public $em = null;

/**
* @var \Codeception\Lib\Interfaces\DoctrineProvider
* @var DoctrineProvider
*/
private $dependentModule;

Expand Down Expand Up @@ -305,7 +306,7 @@ protected function clean()
/**
* Performs $em->flush();
*/
public function flushToDatabase()
public function flushToDatabase(): void
{
$this->em->flush();
}
Expand All @@ -323,7 +324,7 @@ public function flushToDatabase()
*
* @param object|object[] $entities
*/
public function refreshEntities($entities)
public function refreshEntities($entities): void
{
if (!is_array($entities)) {
$entities = [$entities];
Expand All @@ -341,20 +342,11 @@ public function refreshEntities($entities)
* $I->clearEntityManager();
* ```
*/
public function clearEntityManager()
public function clearEntityManager(): void
{
$this->em->clear();
}

/**
* This method is deprecated in favor of `haveInRepository()`. Its functionality is exactly the same.
*/
public function persistEntity($obj, $values = [])
{
Notification::deprecate("Doctrine2::persistEntity is deprecated in favor of Doctrine2::haveInRepository");
return $this->haveInRepository($obj, $values);
}

/**
* Mocks the repository.
*
Expand All @@ -373,14 +365,14 @@ public function persistEntity($obj, $values = [])
* This creates a stub class for Entity\User repository with redefined method findByUsername,
* which will always return the NULL value.
*
* @param $classname
* @param string $className
* @param array $methods
*/
public function haveFakeRepository($classname, $methods = [])
public function haveFakeRepository(string $className, array $methods = []): void
{
$em = $this->em;

$metadata = $em->getMetadataFactory()->getMetadataFor($classname);
$metadata = $em->getMetadataFactory()->getMetadataFor($className);
$customRepositoryClassName = $metadata->customRepositoryClassName;

if (!$customRepositoryClassName) {
Expand Down Expand Up @@ -408,7 +400,7 @@ public function haveFakeRepository($classname, $methods = [])

$property = $reflectedEm->getProperty('repositories');
$property->setAccessible(true);
$property->setValue($em, array_merge($property->getValue($em), [$classname => $mock]));
$property->setValue($em, array_merge($property->getValue($em), [$className => $mock]));
} elseif ($reflectedEm->hasProperty('repositoryFactory')) {
//For doctrine 2.4.0+ versions

Expand All @@ -424,7 +416,7 @@ public function haveFakeRepository($classname, $methods = [])

$repositoryListProperty->setValue(
$repositoryFactory,
[$classname => $mock]
[$className => $mock]
);

$repositoryFactoryProperty->setValue($em, $repositoryFactory);
Expand Down Expand Up @@ -537,7 +529,7 @@ public function haveInRepository($classNameOrInstance, array $data = [])
* @return object
* @throws ReflectionException
*/
private function instantiateAndPopulateEntity($className, array $data, array &$instances)
private function instantiateAndPopulateEntity(string $className, array $data, array &$instances)
{
$rpa = new ReflectionPropertyAccessor();
list($scalars,$relations) = $this->splitScalarsAndRelations($className, $data);
Expand Down Expand Up @@ -583,7 +575,7 @@ private function populateEntity($instance, array $data, array &$instances)
* @param array $data
* @return array
*/
private function splitScalarsAndRelations($className, array $data)
private function splitScalarsAndRelations(string $className, array $data): array
{
$scalars = [];
$relations = [];
Expand All @@ -608,7 +600,7 @@ private function splitScalarsAndRelations($className, array $data)
* @param array &$instances
* @return array
*/
private function instantiateRelations($className, $master, array $data, array &$instances)
private function instantiateRelations(string $className, $master, array $data, array &$instances): array
{
$metadata = $this->em->getClassMetadata($className);

Expand Down Expand Up @@ -691,11 +683,11 @@ private function extractPrimaryKey($instance)
* @throws ModuleException
* @throws ModuleRequireException
*/
public function loadFixtures($fixtures, $append = true)
public function loadFixtures($fixtures, bool $append = true): void
{
if (!class_exists(\Doctrine\Common\DataFixtures\Loader::class)
|| !class_exists(\Doctrine\Common\DataFixtures\Purger\ORMPurger::class)
|| !class_exists(\Doctrine\Common\DataFixtures\Executor\ORMExecutor::class)) {
if (!class_exists(Loader::class)
|| !class_exists(ORMPurger::class)
|| !class_exists(ORMExecutor::class)) {
throw new ModuleRequireException(
__CLASS__,
'Doctrine fixtures support in unavailable.\n'
Expand Down Expand Up @@ -871,11 +863,10 @@ public function dontSeeInRepository($entity, $params = [])
$this->assertNot($res);
}

protected function proceedSeeInRepository($entity, $params = [])
protected function proceedSeeInRepository($entity, $params = []): array
{
// we need to store to database...
$this->em->flush();
$data = $this->em->getClassMetadata($entity);
$qb = $this->em->getRepository($entity)->createQueryBuilder('s');
$this->buildAssociationQuery($qb, $entity, 's', $params);
$this->debug($qb->getDQL());
Expand Down Expand Up @@ -907,7 +898,6 @@ public function grabFromRepository($entity, $field, $params = [])
{
// we need to store to database...
$this->em->flush();
$data = $this->em->getClassMetadata($entity);
$qb = $this->em->getRepository($entity)->createQueryBuilder('s');
$qb->select('s.' . $field);
$this->buildAssociationQuery($qb, $entity, 's', $params);
Expand All @@ -928,16 +918,15 @@ public function grabFromRepository($entity, $field, $params = [])
* ?>
* ```
*
* @version 1.1
* @param $entity
* @param array $params. For `IS NULL`, use `['field' => null]`
* @return array
* @version 1.1
*/
public function grabEntitiesFromRepository($entity, $params = [])
public function grabEntitiesFromRepository($entity, array $params = []): array
{
// we need to store to database...
$this->em->flush();
$data = $this->em->getClassMetadata($entity);
$qb = $this->em->getRepository($entity)->createQueryBuilder('s');
$qb->select('s');
$this->buildAssociationQuery($qb, $entity, 's', $params);
Expand All @@ -959,16 +948,15 @@ public function grabEntitiesFromRepository($entity, $params = [])
* ?>
* ```
*
* @version 1.1
* @param $entity
* @param array $params. For `IS NULL`, use `['field' => null]`
* @return object
* @version 1.1
*/
public function grabEntityFromRepository($entity, $params = [])
public function grabEntityFromRepository($entity, array $params = [])
{
// we need to store to database...
$this->em->flush();
$data = $this->em->getClassMetadata($entity);
$qb = $this->em->getRepository($entity)->createQueryBuilder('s');
$qb->select('s');
$this->buildAssociationQuery($qb, $entity, 's', $params);
Expand All @@ -977,28 +965,13 @@ public function grabEntityFromRepository($entity, $params = [])
return $qb->getQuery()->getSingleResult();
}

/**
* It's Fuckin Recursive!
*
* @param QueryBuilder $qb
* @param string $assoc
* @param string $alias
* @param array $params
*/
protected function buildAssociationQuery($qb, $assoc, $alias, $params)
protected function buildAssociationQuery(QueryBuilder $qb, string $assoc, string $alias, array $params): void
{
$paramIndex = 0;
$this->_buildAssociationQuery($qb, $assoc, $alias, $params, $paramIndex);
}

/**
* @param QueryBuilder $qb
* @param string $assoc
* @param string $alias
* @param array $params
* @param int &$paramIndex
*/
protected function _buildAssociationQuery($qb, $assoc, $alias, $params, &$paramIndex)
protected function _buildAssociationQuery(QueryBuilder $qb, string $assoc, string $alias, array $params, int &$paramIndex)
{
$data = $this->em->getClassMetadata($assoc);
foreach ($params as $key => $val) {
Expand Down Expand Up @@ -1038,7 +1011,7 @@ public function _getEntityManager()
* @param mixed $instance
* @param mixed $pks
*
* @throws \ReflectionException
* @throws ReflectionException
*/
private function debugEntityCreation($instance, $pks)
{
Expand All @@ -1064,10 +1037,9 @@ private function debugEntityCreation($instance, $pks)

/**
* @param mixed $pk
*
* @return bool
*/
private function isDoctrineEntity($pk)
private function isDoctrineEntity($pk): bool
{
$isEntity = is_object($pk);

Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Codeception/Module/Doctrine2Test.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use Codeception\Exception\ModuleException;
use Codeception\Module\Doctrine2;
use Codeception\Test\Unit;
Expand All @@ -12,7 +14,7 @@
use Ramsey\Uuid\Doctrine\UuidType;
use Ramsey\Uuid\UuidInterface;

class Doctrine2Test extends Unit
final class Doctrine2Test extends Unit
{
/**
* @var EntityManager
Expand Down