FilterManagerQueryTest::prepareDbData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zenify\DoctrineFilters\Tests\FilterManager;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\EntityRepository;
10
use PHPUnit\Framework\TestCase;
11
use Zenify\DoctrineFilters\Contract\FilterManagerInterface;
12
use Zenify\DoctrineFilters\FilterManager;
13
use Zenify\DoctrineFilters\Tests\ContainerFactory;
14
use Zenify\DoctrineFilters\Tests\Entity\Product;
15
16
17
final class FilterManagerQueryTest extends TestCase
18
{
19
20
	/**
21
	 * @var EntityManagerInterface
22
	 */
23
	private $entityManager;
24
25
	/**
26
	 * @var EntityRepository
27
	 */
28
	private $productRepository;
29
30
	/**
31
	 * @var FilterManagerInterface
32
	 */
33
	private $filterManager;
34
35
36
	protected function setUp()
37
	{
38
		$container = (new ContainerFactory)->create();
39
		$this->entityManager = $container->getByType(EntityManagerInterface::class);
40
		$this->filterManager = $container->getByType(FilterManager::class);
41
		$this->productRepository = $this->entityManager->getRepository(Product::class);
42
43
		$this->prepareDbData($container->getByType(Connection::class));
0 ignored issues
show
Documentation introduced by
$container->getByType(\D...DBAL\Connection::class) is of type object|null, but the function expects a object<Doctrine\DBAL\Connection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
	}
45
46
47
	public function testFindOneBy()
48
	{
49
		$this->filterManager->enableFilters();
50
51
		$product = $this->productRepository->findOneBy(['id' => 1]);
52
		$this->assertInstanceOf(Product::class, $product);
53
		$this->assertTrue($product->isActive());
54
55
		$product2 = $this->productRepository->findOneBy(['id' => 2]);
56
		$this->assertNull($product2);
57
58
		// this should be NULL; this appears only in CLI
59
		$product2 = $this->productRepository->find(2);
60
		$this->assertInstanceOf(Product::class, $product2);
61
		$this->assertFalse($product2->isActive());
62
	}
63
64
65
	private function prepareDbData(Connection $connection)
66
	{
67
		$connection->query('CREATE TABLE product (id INTEGER NOT NULL, name string, is_active int NULL, PRIMARY KEY(id))');
68
69
		$this->entityManager->persist(new Product(TRUE));
70
		$this->entityManager->persist(new Product(FALSE));
71
		$this->entityManager->flush();
72
	}
73
74
}
75