Completed
Pull Request — master (#6)
by
unknown
02:21
created

NodeContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B iVisitNodePageOfType() 0 24 3
A iEditThisNode() 0 9 2
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Node;
6
7
use Drupal\TqExtension\Context\RawTqContext;
8
9
class NodeContext extends RawTqContext
10
{
11
    /**
12
     * @When /^I (visit|view|edit) "([^"]+)" node of type "([^"]+)"$/
13
     */
14
    public function iVisitNodePageOfType($operation, $title, $type) {
15
        if ($operation == 'visit') {
16
            $operation = 'view';
17
        }
18
        $query = new \EntityFieldQuery();
19
        $result = $query
20
            ->entityCondition('entity_type', 'node')
21
            // @todo: Add support for CT label.
22
            ->entityCondition('bundle', strtolower($type))
23
            ->propertyCondition('title', $title)
24
            ->range(0, 1)
25
            ->execute();
26
27
        if (empty($result['node'])) {
28
            $params = array(
29
                '@title' => $title,
30
                '@type' => $type,
31
            );
32
            throw new \Exception(format_string("Node @title of @type not found.", $params));
33
        }
34
35
        $nid = key($result['node']);
36
        $this->getSession()->visit($this->locatePath('/node/' . $nid . '/' . $operation));
37
    }
38
39
    /**
40
     * @When I edit this node
41
     */
42
    public function iEditThisNode()
0 ignored issues
show
Coding Style introduced by
iEditThisNode uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
43
    {
44
        if (preg_match("@node/(\d+)@", $_GET['q'], $matches)) {
45
            $this->getSession()->visit($this->locatePath('/node/' . $matches[1] . '/edit'));
46
        }
47
        else {
48
            throw new \Exception("You're not currently on a node page.");
49
        }
50
    }
51
}
52