Completed
Push — master ( 8830da...3eaba8 )
by Eric
09:18
created

AbstractResourceContext::getResource()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Behat\Context;
13
14
use Behat\Symfony2Extension\Context\KernelAwareContext;
15
use Lug\Bundle\ResourceBundle\Behat\Dictionary\ResourceDictionary;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
abstract class AbstractResourceContext implements KernelAwareContext
21
{
22
    use ResourceDictionary;
23
24
    /**
25
     * @param string $resource
26
     * @param mixed  $criteria
27
     */
28
    public function assertResourceFound($resource, array $criteria)
29
    {
30
        \PHPUnit_Framework_Assert::assertNotNull(
31
            $this->findResource($resource, $criteria),
32
            sprintf('The resource "%s" could not be found. (%s)', $resource, json_encode($criteria))
33
        );
34
    }
35
36
    /**
37
     * @param string $resource
38
     * @param mixed  $criteria
39
     */
40
    public function assertResourceNotFound($resource, array $criteria)
41
    {
42
        \PHPUnit_Framework_Assert::assertNull(
43
            $this->findResource($resource, $criteria),
44
            sprintf('The resource "%s" could be found. (%s)', $resource, json_encode($criteria))
45
        );
46
    }
47
48
    /**
49
     * @param string  $resource
50
     * @param mixed[] $criteria
51
     *
52
     * @return object|null
53
     */
54
    protected function findResource($resource, array &$criteria)
55
    {
56
        array_walk_recursive($criteria, function (&$value) {
57
            if ($value === 'yes') {
58
                $value = true;
59
            } elseif ($value === 'no') {
60
                $value = false;
61
            }
62
        });
63
64
        return $this->getRepository($resource)->findOneBy($criteria);
65
    }
66
}
67