Completed
Push — develop ( 723ec8...69e22a )
by Peter
08:18
created

WebTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 146
rs 10
wmc 14
lcom 1
cbo 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getWebTitle() 0 4 1
A getWebSource() 0 4 1
A getServerUrl() 0 5 1
A getSessionId() 0 4 1
A is404() 0 6 2
A assertWebContains() 0 5 1
A assertWebNotContains() 0 5 1
A assertWebNotError() 0 15 1
A assertNotError() 0 5 1
A open() 0 7 1
A openOk() 0 4 1
A source() 0 6 2
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDev/ for the canonical source repository
6
 * @copyright   Copyright (c) 2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Test\Selenium;
11
12
/**
13
 * Trait SourceTrait
14
 *
15
 * @property-read object $session
16
 *
17
 * @method assertContains($haystack, $needle)
18
 * @method assertNotContains($haystack, $needle)
19
 */
20
trait WebTrait
21
{
22
    /**
23
     * Return web title
24
     *
25
     * @return string
26
     */
27
    public function getWebTitle()
28
    {
29
        return (string) $this->session->title();
30
    }
31
32
    /**
33
     * Return web source
34
     *
35
     * @return string
36
     */
37
    public function getWebSource()
38
    {
39
        return (string) $this->session->source();
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    protected function getServerUrl()
46
    {
47
        $parts = parse_url($this->session->url());
48
        return $parts['scheme'] . '://' . $parts['host'];
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    protected function getSessionId()
55
    {
56
        return $this->session->getCookie('PHPSESSID')['value'];
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    public function is404()
63
    {
64
        $url = $this->session->url();
65
        $headers = get_headers($url);
66
        return empty($headers[0]) ? true : ('HTTP/1.0 404 Not Found' === $headers[0]);
67
    }
68
69
    /**
70
     * Assert that page source contains given string
71
     *
72
     * @param string $string
73
     * @return $this
74
     */
75
    public function assertWebContains($string)
76
    {
77
        $this->assertContains((string) $string, $this->getWebSource());
78
        return $this;
79
    }
80
81
    /**
82
     * Assert that page source does not contains given string
83
     *
84
     * @param string $string
85
     * @return $this
86
     */
87
    public function assertWebNotContains($string)
88
    {
89
        $this->assertNotContains((string) $string, $this->getWebSource());
90
        return $this;
91
    }
92
93
    /**
94
     * Assert that web page is without errors
95
     *
96
     * @return $this
97
     */
98
    protected function assertWebNotError()
99
    {
100
        $title = $this->getWebTitle();
101
        $this->assertNotContains('Error', $title);
102
        $this->assertNotContains('Exception', $title);
103
104
        // strip script contents & tags
105
        $text = substr($this->getWebSource(), 0, 10000);
106
        $expr = '~<(script).*?>.*?</script>~si';
107
        $src  = strip_tags(preg_replace($expr, '', $text));
108
109
        $this->assertNotContains('Error', $src);
110
        $this->assertNotContains('Exception', $src);
111
        return $this;
112
    }
113
114
    /**
115
     * @todo remove, deprecated
116
     * @deprecated use assertWebNotError() instead
117
     * @return $this
118
     */
119
    protected function assertNotError()
120
    {
121
        $this->assertWebNotError();
122
        return $this;
123
    }
124
125
    /**
126
     * Opens URI and asserts not error
127
     *
128
     * @param string $path
129
     * @param string|null $caption
130
     * @return $this
131
     */
132
    protected function open($path = '', $caption = null)
133
    {
134
        $this->session->open($this->uri . $path);
0 ignored issues
show
Bug introduced by
The property uri does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
135
        $this->debugNotify($caption);
0 ignored issues
show
Bug introduced by
It seems like debugNotify() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
136
        $this->assertWebNotError();
137
        return $this;
138
    }
139
140
    /**
141
     * @param string $path
142
     * @param string $caption
143
     * @return $this
144
     * @deprecated use open()
145
     * @todo remove
146
     */
147
    protected function openOk($path = '', $caption = 'Home')
148
    {
149
        return $this->open($path, $caption);
150
    }
151
152
    /**
153
     * Get raw source from URL
154
     *
155
     * @param string $url
156
     * @param null $sessId
157
     * @return string
158
     */
159
    protected function source($url, $sessId = null)
160
    {
161
        $sid = $sessId ? $sessId : $this->getSessionId();
162
        $opts = ['http' => ['header' => 'Cookie: PHPSESSID=' . $sid ."\r\n"]];
163
        return file_get_contents($url, false, stream_context_create($opts));
164
    }
165
}
166