Completed
Push — 1.0.x ( 235ffe...0a17d9 )
by Antonio
08:29
created

ResponsiveContext::getDeviceResolution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Mink\Exception\ExpectationException;
6
use function bovigo\assert\predicate\isOfType;
7
use function bovigo\assert\predicate\hasKey;
8
use function bovigo\assert\assert;
9
use Behat\MinkExtension\Context\RawMinkContext;
10
use NuvoleWeb\Drupal\DrupalExtension\Component\ResolutionComponent;
11
12
/**
13
 * Class ResponsiveContext.
14
 *
15
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
16
 */
17
class ResponsiveContext extends RawMinkContext {
18
19
  /**
20
   * Default list of device definitions.
21
   *
22
   * @var array
23
   */
24
  protected $defaultDevices = [
25
    'mobile_portrait' => '360x640',
26
    'mobile_landscape' => '640x360',
27
    'tablet_portrait' => '768x1024',
28
    'tablet_landscape' => '1024x768',
29
    'laptop' => '1280x800',
30
    'desktop' => '2560x1440',
31
  ];
32
33
  /**
34
   * Contains list of processed devices.
35
   *
36
   * @var array
37
   */
38
  protected $devices = [];
39
40
  /**
41
   * ResponsiveContext constructor.
42
   *
43
   * @param array $devices
44
   *    List of devices.
45
   */
46
  public function __construct($devices = []) {
47
    assert($devices, isOfType('array'));
48
    $devices = $devices + $this->defaultDevices;
49
    $this->processDevices($devices);
50
  }
51
52
  /**
53
   * Process devices.
54
   *
55
   * @param array $devices
56
   *    List of devices.
57
   */
58
  private function processDevices(array $devices) {
59
    foreach ($devices as $name => $resolution) {
60
      $resolution_component = new ResolutionComponent();
61
      $resolution_component->parse($resolution);
62
      $this->devices[$name] = $resolution_component;
63
    }
64
  }
65
66
  /**
67
   * Get device resolution.
68
   *
69
   * @param string $name
70
   *    Device name.
71
   *
72
   * @return ResolutionComponent
73
   *    Resolution object.
74
   */
75
  protected function getDeviceResolution($name) {
76
    assert($this->devices, hasKey($name), "Device '{$name}' not found.");
77
    return $this->devices[$name];
78
  }
79
80
  /**
81
   * Resize browser window according to the specified device.
82
   *
83
   * @param string $device
84
   *    Device name as specified in behat.yml.
85
   *
86
   * @Given I view the site on a :device device
87
   */
88
  public function assertDeviceScreenResize($device) {
89
    $resolution = $this->getDeviceResolution($device);
90
    $this->getSession()->resizeWindow($resolution->getWidth(), $resolution->getHeight(), 'current');
91
  }
92
93
  /**
94
   * Resize browser window width.
95
   *
96
   * @param string $size
97
   *    Size in pixel.
98
   *
99
   * @throws \Behat\Mink\Exception\ExpectationException
100
   *
101
   * @Then the browser window width should be :size
102
   */
103
  public function assertBrowserWindowWidth($size) {
104
    $actual = $this->getSession()->evaluateScript('return window.innerWidth;');
105
    if ($actual != $size) {
106
      throw new ExpectationException("Browser window width expected to be {$size} but it is {$actual} instead.", $this->getSession());
107
    }
108
  }
109
110
  /**
111
   * Resize browser window height.
112
   *
113
   * @param string $size
114
   *    Size in pixel.
115
   *
116
   * @throws \Behat\Mink\Exception\ExpectationException
117
   *
118
   * @Then the browser window height should be :size
119
   */
120
  public function assertBrowserWindowHeight($size) {
121
    $actual = $this->getSession()->evaluateScript('return window.innerHeight;');
122
    if ($actual != $size) {
123
      throw new ExpectationException("Browser window height expected to be {$size} but it is {$actual} instead.", $this->getSession());
124
    }
125
  }
126
127
}
128