GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#186)
by
unknown
11:15 queued 05:03
created

functions.php ➔ assertsObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) 2011-present Mediasift Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Storyplayer
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://datasift.github.io/storyplayer
42
 */
43
44
use DataSift\Storyplayer\PlayerLib\StoryTeller;
45
use DataSift\Storyplayer\ProvisioningLib\ProvisioningDefinition;
46
use DataSift\Stone\HttpLib\HttpClientResponse;
47
use Predis\Client as PredisClient;
48
49
// ==================================================================
50
//
51
// a list of all the Prose modules that we are exposing
52
//
53
// this file registers global functions for each of our Prose modules
54
// it is a great help for everyone who uses autocompletion in their
55
// editor / IDE of choice
56
//
57
// keep this list in alphabetical order, please!
58
//
59
// ------------------------------------------------------------------
60
61
use Prose\AssertsArray;
62
use Prose\AssertsBoolean;
63
use Prose\AssertsDouble;
64
use Prose\AssertsInteger;
65
use Prose\AssertsObject;
66
use Prose\AssertsString;
67
use Prose\CleanupHosts;
68
use Prose\CleanupProcesses;
69
use Prose\CleanupRoles;
70
use Prose\CleanupTargets;
71
use Prose\ExpectsBrowser;
72
use Prose\ExpectsEc2Image;
73
use Prose\ExpectsFailure;
74
use Prose\ExpectsFirstHostWithRole;
75
use Prose\ExpectsForm;
76
use Prose\ExpectsGraphite;
77
use Prose\ExpectsHost;
78
use Prose\ExpectsHostsTable;
79
use Prose\ExpectsHttpResponse;
80
use Prose\ExpectsProcessesTable;
81
use Prose\ExpectsRolesTable;
82
use Prose\ExpectsRuntimeTable;
83
use Prose\ExpectsShell;
84
use Prose\ExpectsSupervisor;
85
use Prose\ExpectsUuid;
86
use Prose\ExpectsZmq;
87
use Prose\ExpectsZmqSocket;
88
use Prose\ForeachHostWithRole;
89
use Prose\FromAws;
90
use Prose\FromBrowser;
91
use Prose\FromCheckpoint;
92
use Prose\FromConfig;
93
use Prose\FromCurl;
94
use Prose\FromEc2;
95
use Prose\FromEc2Instance;
96
use Prose\FromEnvironment;
97
use Prose\FromFacebook;
98
use Prose\FromFile;
99
use Prose\FromFirstHostWithRole;
100
use Prose\FromForm;
101
use Prose\FromGraphite;
102
use Prose\FromHost;
103
use Prose\FromHostsTable;
104
use Prose\FromHttp;
105
use Prose\FromIframe;
106
use Prose\FromPDOStatement;
107
use Prose\FromProcessesTable;
108
use Prose\FromRedisConn;
109
use Prose\FromRolesTable;
110
use Prose\FromRuntimeTable;
111
use Prose\FromRuntimeTableForTargetEnvironment;
112
use Prose\FromSauceLabs;
113
use Prose\FromShell;
114
use Prose\FromStoryplayer;
115
use Prose\FromSupervisor;
116
use Prose\FromSystemUnderTest;
117
use Prose\FromTargetsTable;
118
use Prose\FromTestEnvironment;
119
use Prose\FromUsers;
120
use Prose\FromUuid;
121
use Prose\FromZmqSocket;
122
use Prose\UsingBrowser;
123
use Prose\UsingCheckpoint;
124
use Prose\UsingEc2;
125
use Prose\UsingEc2Instance;
126
use Prose\UsingFacebookGraphApi;
127
use Prose\UsingFile;
128
use Prose\UsingFirstHostWithRole;
129
use Prose\UsingForm;
130
use Prose\UsingHornet;
131
use Prose\UsingHost;
132
use Prose\UsingHostsTable;
133
use Prose\UsingHttp;
134
use Prose\UsingIframe;
135
use Prose\UsingLog;
136
use Prose\UsingMysql;
137
use Prose\UsingPDO;
138
use Prose\UsingPDODB;
139
use Prose\UsingProcessesTable;
140
use Prose\UsingProvisioning;
141
use Prose\UsingProvisioningDefinition;
142
use Prose\UsingProvisioningEngine;
143
use Prose\UsingRedis;
144
use Prose\UsingRedisConn;
145
use Prose\UsingReporting;
146
use Prose\UsingRolesTable;
147
use Prose\UsingRuntimeTable;
148
use Prose\UsingRuntimeTableForTargetEnvironment;
149
use Prose\UsingSauceLabs;
150
use Prose\UsingSavageD;
151
use Prose\UsingShell;
152
use Prose\UsingSupervisor;
153
use Prose\UsingTargetsTable;
154
use Prose\UsingTimer;
155
use Prose\UsingUsers;
156
use Prose\UsingVagrant;
157
use Prose\UsingYamlFile;
158
use Prose\UsingZmq;
159
use Prose\UsingZmqContext;
160
use Prose\UsingZmqSocket;
161
use Prose\UsingZookeeper;
162
163
use Prose\E5xx_ActionFailed;
164
165
/**
166
 * returns the AssertsArray module
167
 *
168
 * @param  array $actual
169
 *         the array to be tested
170
 * @return \Prose\AssertsArray
171
 */
172
function assertsArray($actual)
173
{
174
    return new AssertsArray(StoryTeller::instance(), [$actual]);
175
}
176
177
/**
178
 * returns the AssertsBoolean module
179
 *
180
 * @param  boolean $actual
181
 *         the data to be tested
182
 * @return \Prose\AssertsBoolean
183
 */
184
function assertsBoolean($actual)
185
{
186
    return new AssertsBoolean(StoryTeller::instance(), [$actual]);
187
}
188
189
/**
190
 * returns the AssertsDouble module
191
 *
192
 * @param  double $actual
193
 *         the data to be tested
194
 * @return \Prose\AssertsDouble
195
 */
196
function assertsDouble($actual)
197
{
198
    return new AssertsDouble(StoryTeller::instance(), [$actual]);
199
}
200
201
/**
202
 * returns the AssertsInteger module
203
 *
204
 * @param  int $actual
205
 *         the data to be tested
206
 * @return \Prose\AssertsInteger
207
 */
208
function assertsInteger($actual)
209
{
210
    return new AssertsInteger(StoryTeller::instance(), [$actual]);
211
}
212
213
/**
214
 * returns the AssertsObject module
215
 *
216
 * @param  object $actual
217
 *         the data to be tested
218
 * @return \Prose\AssertsObject
219
 */
220
function assertsObject($actual)
221
{
222
    return new AssertsObject(StoryTeller::instance(), [$actual]);
223
}
224
225
/**
226
 * returns the AssertsString module
227
 *
228
 * @param  string $actual
229
 *         the data to be tested
230
 * @return \Prose\AssertsString
231
 */
232
function assertsString($actual)
233
{
234
    return new AssertsString(StoryTeller::instance(), [$actual]);
235
}
236
237
/**
238
 * returns the CleanupHosts module
239
 *
240
 * This module is used internally when Storyplayer shuts down to cleanup any
241
 * remaining entries in the internal 'hosts' table.
242
 *
243
 * You will never need to call this module from your stories.
244
 *
245
 * @param  string $key
246
 *         the name of the hosts table in the runtime table
247
 * @return \Prose\CleanupHosts
248
 */
249
function cleanupHosts($key)
250
{
251
    return new CleanupHosts(StoryTeller::instance(), [$key]);
252
}
253
254
/**
255
 * returns the CleanupProcesses module
256
 *
257
 * This module is used internally when Storyplayer shuts down to cleanup any
258
 * remaining entries in the internal 'processes' table.
259
 *
260
 * You will never need to call this module from your stories.
261
 *
262
 * @param  string $key
263
 *         the name of the processes table in the runtime table
264
 * @return \Prose\CleanupProcesses
265
 */
266
function cleanupProcesses($key)
267
{
268
    return new CleanupProcesses(StoryTeller::instance(), [$key]);
269
}
270
271
/**
272
 * returns the CleanupRoles module
273
 *
274
 * This module is used internally when Storyplayer shuts down to cleanup any
275
 * remaining entries in the internal 'roles' table.
276
 *
277
 * You will never need to call this module from your stories.
278
 *
279
 * @param  string $key
280
 *         the name of the roles table in the runtime table
281
 * @return \Prose\CleanupRoles
282
 */
283
function cleanupRoles($key)
284
{
285
    return new CleanupRoles(StoryTeller::instance(), [$key]);
286
}
287
288
/**
289
 * returns the CleanupTargets module
290
 *
291
 * This module is used internally when Storyplayer shuts down to cleanup any
292
 * remaining entries in the internal 'targets' table.
293
 *
294
 * You will never need to call this module from your stories.
295
 *
296
 * @param  string $key
297
 *         the name of the targets table in the runtime table
298
 * @return \Prose\CleanupTargets
299
 */
300
function cleanupTargets($key)
301
{
302
    return new CleanupTargets(StoryTeller::instance(), [$key]);
303
}
304
305
/**
306
 * returns the ExpectsBrowser module
307
 *
308
 * This module provides support for checking the content of your web browser.
309
 *
310
 * Internally, this module interacts with your chosen browser (you chose by
311
 * using the --device command-line switch) using the Selenium WebDriver API.
312
 *
313
 * We've added a lot of helpful methods you can use to make it both quick
314
 * and natural to check the contents of the HTML page that's currently
315
 * open in the web browser.
316
 *
317
 * If the check fails, this module throws an exception.
318
 *
319
 * @return \Prose\ExpectsBrowser
320
 * @throws \Prose\E5xx_ExpectFailed
321
 */
322
function expectsBrowser()
323
{
324
    return new ExpectsBrowser(StoryTeller::instance());
325
}
326
327
/**
328
 * returns the ExpectsEc2Image module
329
 *
330
 * This module provides support for testing the current state / condition
331
 * of an Amazon EC2 AMI. If the check fails, then this module will throw
332
 * an exception.
333
 *
334
 * @param  string $amiId
335
 *         the AMI ID that you want to check on
336
 * @return \Prose\ExpectsEc2Image
337
 * @throws \Prose\E5xx_ExpectFailed
338
 */
339
function expectsEc2Image($amiId)
340
{
341
    return new ExpectsEc2Image(StoryTeller::instance(), [$amiId]);
342
}
343
344
/**
345
 * returns the ExpectsFailure module
346
 *
347
 * Use this module to wrap operations that should not succeed, so that when
348
 * they do fail, they do not cause your story to fail. If the operations
349
 * actually end up succeeding, then this module will throw an exception.
350
 *
351
 * @return \Prose\ExpectsFailure
352
 * @throws \Prose\E5xx_ExpectFailed
353
 */
354
function expectsFailure()
355
{
356
    return new ExpectsFailure(StoryTeller::instance());
357
}
358
359
/**
360
 * returns the ExpectsHost module, with its hostId already set to the first
361
 * host in your test environment config file that has the given role.
362
 *
363
 * This module provides support for making your tests work on multiple
364
 * test environments. Instead of hard-coding hostIds into your stories,
365
 * use this module to find a host by its assigned role. That way, it doesn't
366
 * matter how many hosts are in different environments, or if their hostIds
367
 * are different.
368
 *
369
 * This module is normally used for testing the status of a host, such as if
370
 * a given process is currently running. If the check fails, this module
371
 * will throw an exception.
372
 *
373
 * @param  string $roleName
374
 *         the assigned role you're looking for
375
 * @return \Prose\ExpectsHost
376
 * @throws \Prose\E5xx_ExpectFailed
377
 */
378
function expectsFirstHostWithRole($roleName)
379
{
380
    return new ExpectsFirstHostWithRole(StoryTeller::instance(), [$roleName]);
381
}
382
383
/**
384
 * returns the ExpectsForm module
385
 *
386
 * This module provides support for working with a given form on a HTML page
387
 * that's open in your browser. To use it, the form must have an 'id'
388
 * attribute set. Targetting a form with an 'id' helps make your test more
389
 * robust, especially if there are multiple forms on the same HTML page.
390
 *
391
 * Use the UsingBrowser module first to open the HTML page where the form is.
392
 *
393
 * @param  string $formId
394
 *         the 'id' attribute of the HTML form to use
395
 * @return \Prose\ExpectsForm
396
 * @throws \Prose\E5xx_ExpectFailed
397
 */
398
function expectsForm($formId)
399
{
400
    return new ExpectsForm(StoryTeller::instance(), [$formId]);
401
}
402
403
/**
404
 * returns the ExpectsGraphite module
405
 *
406
 * This module provides support for retrieving data from Graphite. Graphite
407
 * is a round-robin database for capturing time-based series of data to be
408
 * displayed in graphs.
409
 *
410
 * You can use SavageD in your test environment to monitor the CPU and
411
 * memory usage of your system under test, logging the data to Graphite.
412
 * You can then use this module to make sure that your system under test
413
 * hasn't used too much CPU or RAM.
414
 *
415
 * @return \Prose\ExpectsGraphite
416
 * @throws \Prose\E5xx_ExpectFailed
417
 */
418
function expectsGraphite()
419
{
420
    return new ExpectsGraphite(StoryTeller::instance());
421
}
422
423
/**
424
 * returns the ExpectsHost module
425
 *
426
 * This module provides support for checking on something on a computer in
427
 * your test environment. If the check fails, an exception is thrown for you.
428
 *
429
 * In SPv1, it was common to call this module directly from your own stories.
430
 * In SPv2, you're much more likely to use one of our multi-host modules or
431
 * helpers (such as usingFirstHostWithRole) so that your stories are as
432
 * test-environment-independent as possible.
433
 *
434
 * @param  string $hostId
435
 *         the ID of the host to use
436
 * @return \Prose\ExpectsHost
437
 * @throws \Prose\E5xx_ExpectFailed
438
 */
439
function expectsHost($hostId)
440
{
441
    return new ExpectsHost(StoryTeller::instance(), [$hostId]);
442
}
443
444
/**
445
 * returns the ExpectsHostsTable module
446
 *
447
 * This module provides access to Storyplayer's internal list of computers
448
 * that are running in your test environment.
449
 *
450
 * This module is intended for internal use by Storyplayer. You should not
451
 * need to call this module from your own stories.
452
 *
453
 * @return \Prose\ExpectsHostsTable
454
 * @throws \Prose\E5xx_ExpectFailed
455
 */
456
function expectsHostsTable()
457
{
458
    return new ExpectsHostsTable(StoryTeller::instance());
459
}
460
461
/**
462
 * returns the ExpectsHttpResponse module
463
 *
464
 * This module provides a great way to test that you got the response that
465
 * you expected from your usingHttp() / fromHttp() call.
466
 *
467
 * If the response doesn't meet your expectations, an exception will be
468
 * thrown.
469
 *
470
 * @param  HttpClientResponse $httpResponse
471
 *         the return value from usingHttp()->post() et al
472
 * @return \Prose\ExpectsHttpResponse
473
 * @throws \Prose\E5xx_ExpectFailed
474
 */
475
function expectsHttpResponse(HttpClientResponse $httpResponse)
476
{
477
    return new ExpectsHttpResponse(StoryTeller::instance(), [$httpResponse]);
478
}
479
480
/**
481
 * returns the ExpectsProcessesTable module
482
 *
483
 * This module provides access to Storyplayer's internal table of which
484
 * child processes are currently running.  Storyplayer uses this table to
485
 * make sure that these processes are terminated when a test run ends.
486
 *
487
 * This module is for internal use by Storyplayer. You shouldn't need to call
488
 * this from your own stories.
489
 *
490
 * @return \Prose\ExpectsProcessesTable
491
 * @throws \Prose\E5xx_ExpectFailed
492
 */
493
function expectsProcessesTable()
494
{
495
    return new ExpectsProcessesTable(StoryTeller::instance());
496
}
497
498
/**
499
 * returns the ExpectsRolesTable module
500
 *
501
 * This module provides access to Storyplayer's table of active roles in
502
 * your test environment.
503
 *
504
 * This module is for internal use inside Storyplayer. You shouldn't need to
505
 * use it from your own stories.
506
 *
507
 * @return \Prose\ExpectsRolesTable
508
 * @throws \Prose\E5xx_ExpectFailed
509
 */
510
function expectsRolesTable()
511
{
512
    return new ExpectsRolesTable(StoryTeller::instance());
513
}
514
515
/**
516
 * returns the ExpectsRuntimeTable module
517
 *
518
 * This module provides access to Storyplayer's internal state, also known
519
 * as the table of tables.
520
 *
521
 * This module is for internal use inside Storyplayer. You shouldn't need to
522
 * use it from your own stories.
523
 *
524
 * @param  string $tableName
525
 *         which table do we want to test?
526
 * @return \Prose\ExpectsRuntimeTable
527
 * @throws \Prose\E5xx_ExpectFailed
528
 */
529
function expectsRuntimeTable($tableName)
530
{
531
    return new ExpectsRuntimeTable(StoryTeller::instance(), [$tableName]);
532
}
533
534
/**
535
 * returns the FromShell module
536
 *
537
 * This module provides support for running commands via the UNIX shell.
538
 * These commands will run on the same computer where Storyplayer is running.
539
 *
540
 * If you want to run commands on a computer in your test environment, you
541
 * need the UsingHost module.
542
 *
543
 * @return \Prose\ExpectsShell
544
 * @throws \Prose\E5xx_ExpectFailed
545
 */
546
function expectsShell()
547
{
548
    return new ExpectsShell(StoryTeller::instance());
549
}
550
551
/**
552
 * returns the ExpectsSupervisor module
553
 *
554
 * This module provides support for working the the Supervisor daemon on
555
 * a host in your test environment. Supervisor is an increasingly popular
556
 * solution for looking after network daemons.
557
 *
558
 * @param  string $hostId
559
 *         the ID of the host you want to use Supervisor on
560
 * @return \Prose\ExpectsSupervisor
561
 * @throws \Prose\E5xx_ExpectFailed
562
 */
563
function expectsSupervisor($hostId)
564
{
565
    return new ExpectsSupervisor(StoryTeller::instance(), [$hostId]);
566
}
567
568
/**
569
 * returns the ExpectsUuid module
570
 *
571
 * This module adds support for inspecting a universally-unique-ID. To use it,
572
 * you need to have PHP's UUID extension installed.
573
 *
574
 * @return \Prose\ExpectsUuid
575
 * @throws \Prose\E5xx_ExpectFailed
576
 */
577
function expectsUuid()
578
{
579
    return new ExpectsUuid(StoryTeller::instance());
580
}
581
582
/**
583
 * returns the ExpectsZmq module
584
 *
585
 * This module provides support for working with ZeroMQ, the no-broker
586
 * inter-process queuing library.
587
 *
588
 * @return \Prose\ExpectsZmq
589
 * @throws \Prose\E5xx_ExpectFailed
590
 */
591
function expectsZmq()
592
{
593
    return new ExpectsZmq(StoryTeller::instance());
594
}
595
596
/**
597
 * returns the ExpectsZmqSocket module
598
 *
599
 * This module provides support for testing ZeroMQ sockets
600
 *
601
 * @param  \ZMQSocket
602
 *         the ZMQSocket to test
603
 * @return \Prose\ExpectsZmqSocket
604
 */
605
function expectsZmqSocket($zmqSocket)
606
{
607
    return new ExpectsZmqSocket(StoryTeller::instance(), [$zmqSocket]);
608
}
609
610
/**
611
 * iterates over each host in your test environment that has been assigned
612
 * the given role
613
 *
614
 * This helper gives you a way to run any host-aware module across every
615
 * computer in your test environment that has the role you ask for.
616
 *
617
 * For example, if you want to get a table of IP addresses, you can do this:
618
 *
619
 *     $ipAddresses = foreachHostWithRole('web-server')->fromHost()->getIpAddress();
620
 *
621
 * or, if you wanted to reboot machines, you could do this:
622
 *
623
 *     foreachHostWithRole('web-server')->usingHost()->restartHost();
624
 *
625
 * This iterator will automatically provide the $hostId parameter to the module
626
 * you tell it to call, and will collate all return values into an array.
627
 *
628
 * Alternatively, you can use a normal PHP foreach() loop:
629
 *
630
 *     foreach(hostWithRole('web-server') as $hostId) {
631
 *         usingHost($hostId)->restartHost();
632
 *     }
633
 *
634
 * @param  string $roleName
635
 *         the role that you want to work with
636
 * @return \Prose\ForeachHostWithRole
637
 */
638
function foreachHostWithRole($roleName)
639
{
640
    return new ForeachHostWithRole(StoryTeller::instance(), [$roleName]);
641
}
642
643
/**
644
 * returns the FromAws module
645
 *
646
 * This module is used internally by Storyplayer to connect the Amazon EC2
647
 * API, using the official Amazon SDK.
648
 *
649
 * If you're writing your own modules for working with EC2, then you should
650
 * always create your EC2 client using this module.
651
 *
652
 * You shouldn't need to call this module from your stories.
653
 *
654
 * @return \Prose\FromAws
655
 */
656
function fromAws()
657
{
658
    return new FromAws(StoryTeller::instance());
659
}
660
661
/**
662
 * returns the FromBrowser module
663
 *
664
 * This module (along with the UsingBrowser module) allows you to control a
665
 * real web browser from your story.
666
 *
667
 * Internally, this module interacts with your chosen browser (you chose by
668
 * using the --device command-line switch) using the Selenium WebDriver API.
669
 *
670
 * We've added a lot of helpful methods you can use to make it both quick
671
 * and natural to retrieve information from the HTML page that's currently
672
 * open in the web browser.
673
 *
674
 * @return \Prose\FromBrowser
675
 */
676
function fromBrowser()
677
{
678
    return new FromBrowser(StoryTeller::instance());
679
}
680
681
/**
682
 * returns the FromCheckpoint module
683
 *
684
 * This module is old SPv1 functionality. In practice, you'll want to call
685
 * getCheckpoint() to retrieve the checkpoint, and then interact with that
686
 * directly.
687
 *
688
 * This module will probably be removed in SPv3.
689
 *
690
 * @return \Prose\FromCheckpoint
691
 */
692
function fromCheckpoint()
693
{
694
    return new FromCheckpoint(StoryTeller::instance());
695
}
696
697
/**
698
 * returns the FromConfig module
699
 *
700
 * This module provides access to Storyplayer's loaded config. This is a
701
 * combination of your storyplayer.json[.dist] file and additional information
702
 * about Storyplayer (such as the local computer's IP address) that are
703
 * detected at runtime.
704
 *
705
 * @return \Prose\FromConfig
706
 */
707
function fromConfig()
708
{
709
    return new FromConfig(StoryTeller::instance());
710
}
711
712
/**
713
 * returns the FromCurl module
714
 *
715
 * This module provides support for making requests using cURL. cURL is the
716
 * de facto standard library across all major languages for talking to HTTP
717
 * servers.
718
 *
719
 * @return \Prose\FromCurl
720
 */
721
function fromCurl()
722
{
723
    return new FromCurl(StoryTeller::instance());
724
}
725
726
/**
727
 * returns the FromEc2 module
728
 *
729
 * This module provides support for querying Amazon's EC2.
730
 *
731
 * To start an EC2 virtual machine, call usingEc2().
732
 *
733
 * @return \Prose\FromEc2
734
 */
735
function fromEc2()
736
{
737
    return new FromEc2(StoryTeller::instance());
738
}
739
740
/**
741
 * returns the FromEc2Instance module
742
 *
743
 * This module provides support for querying an Amazon EC2 VM.
744
 *
745
 * To start an EC2 VM, call usingEc2() first.
746
 *
747
 * @param  string $amiId
748
 *         the AMI ID that you want to work with
749
 * @return \Prose\FromEc2Instance
750
 */
751
function fromEc2Instance($amiId)
752
{
753
    return new FromEc2Instance(StoryTeller::instance(), [$amiId]);
754
}
755
756
/**
757
 * returns the FromEnvironment module
758
 *
759
 * This module is here to help you port any SPv1 stories to run under SPv2.
760
 *
761
 * In SPv1, we didn't have the concept that Storyplayer, systems-under-test
762
 * and test environments were separate things in their own right. We had a
763
 * single combined 'environment' where all configs were lumped together.
764
 *
765
 * @deprecated 2.0.0 Use fromStoryplayerConfig(), fromSystemUnderTestConfig() or fromTestEnvironmentConfig() instead
766
 *
767
 * @return \Prose\FromEnvironment
768
 */
769
function fromEnvironment()
770
{
771
    return new FromEnvironment(StoryTeller::instance());
772
}
773
774
/**
775
 * returns the FromFacebook module
776
 *
777
 * This module provides support for querying the Facebook API.
778
 *
779
 * It is currently unmaintained.
780
 *
781
 * @return \Prose\FromFacebook
782
 */
783
function fromFacebook()
784
{
785
    return new FromFacebook(StoryTeller::instance());
786
}
787
788
/**
789
 * returns the FromFile module
790
 *
791
 * This is an old SPv1 module that doesn't really do a lot. We'll shortly
792
 * be introducing a new 'FromFS' module that's much more capable. We'll
793
 * probably remove the 'FromFile' module in SPv3.
794
 *
795
 * @return \Prose\FromFile
796
 */
797
function fromFile()
798
{
799
    return new FromFile(StoryTeller::instance());
800
}
801
802
/**
803
 * returns the FromHost module, with its hostId already set to the first
804
 * host in your test environment config file that has the given role.
805
 *
806
 * This module provides support for making your tests work on multiple
807
 * test environments. Instead of hard-coding hostIds into your stories,
808
 * use this module to find a host by its assigned role. That way, it doesn't
809
 * matter how many hosts are in different environments, or if their hostIds
810
 * are different.
811
 *
812
 * This module is normally used for testing read requests via APIs. You
813
 * should be able to write once, and then read from all hosts to prove
814
 * that the data was correctly written (and that there are no caching errors).
815
 *
816
 * To read from all hosts, you would use:
817
 *
818
 *     foreach(hostWithRole($roleName) as $hostId) { ... }
819
 *
820
 * @param  string $roleName
821
 *         the assigned role you're looking for
822
 * @return \Prose\FromHost
823
 */
824
function fromFirstHostWithRole($roleName)
825
{
826
    return new FromFirstHostWithRole(StoryTeller::instance(), [$roleName]);
827
}
828
829
/**
830
 * returns the FromForm module
831
 *
832
 * This module provides support for working with a given form on a HTML page
833
 * that's open in your browser. To use it, the form must have an 'id'
834
 * attribute set. Targetting a form with an 'id' helps make your test more
835
 * robust, especially if there are multiple forms on the same HTML page.
836
 *
837
 * Use the UsingBrowser module first to open the HTML page where the form is.
838
 *
839
 * @param  string $formId
840
 *         the 'id' attribute of the HTML form to use
841
 * @return \Prose\FromForm
842
 */
843
function fromForm($formId)
844
{
845
    return new FromForm(StoryTeller::instance(), [$formId]);
846
}
847
848
/**
849
 * returns the FromGraphite module
850
 *
851
 * This module provides support for retrieving data from Graphite. Graphite
852
 * is a round-robin database for capturing time-based series of data to be
853
 * displayed in graphs.
854
 *
855
 * You can use SavageD in your test environment to monitor the CPU and
856
 * memory usage of your system under test, logging the data to Graphite.
857
 * You can then use this module to make sure that your system under test
858
 * hasn't used too much CPU or RAM.
859
 *
860
 * @return \Prose\FromGraphite
861
 */
862
function fromGraphite()
863
{
864
    return new FromGraphite(StoryTeller::instance());
865
}
866
867
/**
868
 * returns the FromHost module
869
 *
870
 * This module provides support for running commands on a computer in your
871
 * test environment - basically for doing anything that's likely to change
872
 * the state of that computer.
873
 *
874
 * In SPv1, it was common to call this module directly from your own stories.
875
 * In SPv2, you're much more likely to use one of our multi-host modules or
876
 * helpers (such as usingFirstHostWithRole) so that your stories are as
877
 * test-environment-independent as possible.
878
 *
879
 * @param  string $hostId
880
 *         the ID of the host to use
881
 * @return \Prose\FromHost
882
 */
883
function fromHost($hostId)
884
{
885
    return new FromHost(StoryTeller::instance(), [$hostId]);
886
}
887
888
/**
889
 * returns the FromHostsTable module
890
 *
891
 * This module provides access to Storyplayer's internal list of computers
892
 * that are running in your test environment.
893
 *
894
 * This module is intended for internal use by Storyplayer. You should not
895
 * need to call this module from your own stories.
896
 *
897
 * @return \Prose\FromHostsTable
898
 */
899
function fromHostsTable()
900
{
901
    return new FromHostsTable(StoryTeller::instance());
902
}
903
904
/**
905
 * returns the FromHttp module
906
 *
907
 * This module provides support for making GET requests over HTTP.
908
 *
909
 * SSL/TLS is fully supported.
910
 *
911
 * If you are using self-signed certificates, you will need to set
912
 * 'moduleSettings.http.validateSsl = false' in your test environment's
913
 * config file first.
914
 *
915
 * To make PUT, POST and DELETE requests, use the UsingHttp module.
916
 *
917
 * @return \Prose\FromHttp
918
 */
919
function fromHttp()
920
{
921
    return new FromHttp(StoryTeller::instance());
922
}
923
924
/**
925
 * returns the FromPDOStatement module
926
 *
927
 * This module provides support for retrieving data from a PDO prepared
928
 * statement after you've executed it.  You can use it in your stories to
929
 * retrieve the results of SQL queries.
930
 *
931
 * To create and run a PDO query, use the UsingPDO and UsingPDOStatement
932
 * modules.
933
 *
934
 * @param  PDOStatement $stmt
935
 *         the prepared statement used to run an SQL query
936
 * @return \Prose\FromPDOStatement
937
 */
938
function fromPDOStatement(PDOStatement $stmt)
939
{
940
    return new FromPDOStatement(StoryTeller::instance(), [$stmt]);
941
}
942
943
/**
944
 * returns the FromProcessesTable module
945
 *
946
 * This module provides access to Storyplayer's internal table of running
947
 * child processes. Storyplayer uses this table to make sure that these
948
 * processes are terminated when a test run ends.
949
 *
950
 * This module is intended for internal use by Storyplayer. You shouldn't
951
 * need to call this module from your stories.
952
 *
953
 * @return \Prose\FromProcessesTable
954
 */
955
function fromProcessesTable()
956
{
957
    return new FromProcessesTable(StoryTeller::instance());
958
}
959
960
/**
961
 * returns the FromRedisConn module
962
 *
963
 * This module provides support for retrieving data from a Redis server.
964
 * Redis is a very popular key/value store (and a whole lot more) - it's a
965
 * bit like Memcached on steroids :)
966
 *
967
 * To make a connection to Redis, use the UsingRedis module first.
968
 *
969
 * @param  PredisClient $client
970
 *         the Redis connection opened with UsingRedis
971
 * @return \Prose\FromRedisConn
972
 */
973
function fromRedisConn(PredisClient $client)
974
{
975
    return new FromRedisConn(StoryTeller::instance(), [$client]);
976
}
977
978
/**
979
 * returns the FromRolesTable module
980
 *
981
 * This module provides access to Storyplayer's table of active roles in
982
 * your test environment.
983
 *
984
 * This module is for internal use inside Storyplayer. You shouldn't need to
985
 * use it from your own stories.
986
 *
987
 * @return \Prose\FromRolesTable
988
 */
989
function fromRolesTable()
990
{
991
    return new FromRolesTable(StoryTeller::instance());
992
}
993
994
/**
995
 * returns the FromRuntimeTable module
996
 *
997
 * The 'runtime table' is Storyplayer's internal table of tables - how it
998
 * keeps track of what is happening during a test run.
999
 *
1000
 * This module is intended for internal use only. You shouldn't need to call
1001
 * this module from your own stories.
1002
 *
1003
 * @param  string $tableName
1004
 *         which runtime table do you want?
1005
 * @return \Prose\FromRuntimeTable
1006
 */
1007
function fromRuntimeTable($tableName)
1008
{
1009
    return new FromRuntimeTable(StoryTeller::instance(), [$tableName]);
1010
}
1011
1012
/**
1013
 * returns the FromRuntimeTableForTargetEnvironment module
1014
 *
1015
 * This module provides access to Storyplayer's internal table that tracks
1016
 * the state of the current test environment.
1017
 *
1018
 * This module is intended for internal use only. You shouldn't need to call
1019
 * this module from your own stories.
1020
 *
1021
 * @return \Prose\FromRuntimeTableForTargetEnvironment
1022
 */
1023
function fromRuntimeTableForTargetEnvironment($tableName)
1024
{
1025
    return new FromRuntimeTableForTargetEnvironment(StoryTeller::instance(), [$tableName]);
1026
}
1027
1028
/**
1029
 * returns the FromSauceLabs module
1030
 *
1031
 * At the moment, this module is a placeholder. We hope to add full support
1032
 * for the SauceLabs API soon.
1033
 *
1034
 * You can use the --device switch to run your tests using SauceLabs'
1035
 * platform today. Supported devices start with the prefix 'sl_'.
1036
 *
1037
 * @return \Prose\FromSauceLabs
1038
 */
1039
function fromSauceLabs()
1040
{
1041
    return new FromSauceLabs(StoryTeller::instance());
1042
}
1043
1044
/**
1045
 * returns the FromShell module
1046
 *
1047
 * This module provides support for running commands via the UNIX shell.
1048
 * These commands will run on the same computer where Storyplayer is running.
1049
 *
1050
 * The commands available via this module are for looking up information.
1051
 * They do not make any changes to your computer at all.
1052
 *
1053
 * If you want to run commands on a computer in your test environment, you
1054
 * need the FromHost module.
1055
 *
1056
 * @return \Prose\FromShell
1057
 */
1058
function fromShell()
1059
{
1060
    return new FromShell(StoryTeller::instance());
1061
}
1062
1063
/**
1064
 * returns the FromStoryplayer module
1065
 *
1066
 * This module provides access to Storyplayer's loaded config. This is a
1067
 * combination of your storyplayer.json[.dist] file and additional information
1068
 * about Storyplayer (such as the local computer's IP address) that are
1069
 * detected at runtime.
1070
 *
1071
 * @return \Prose\FromStoryplayer
1072
 */
1073
function fromStoryplayer()
1074
{
1075
    return new FromStoryplayer(StoryTeller::instance());
1076
}
1077
1078
/**
1079
 * returns the FromSupervisor module
1080
 *
1081
 * This module provides support for querying Supervisor to discover whether
1082
 * an app is running or not. Supervisor is an increasingly popular
1083
 * solution for looking after network daemons.
1084
 *
1085
 * @param  string $hostId
1086
 *         the ID of the host you want to use Supervisor on
1087
 * @return \Prose\FromSupervisor
1088
 */
1089
function fromSupervisor($hostId)
1090
{
1091
    return new FromSupervisor(StoryTeller::instance(),[$hostId]);
1092
}
1093
1094
/**
1095
 * returns the FromSystemUnderTest module
1096
 *
1097
 * This module provides access to the configuration for your chosen
1098
 * system-under-test. You can call this from your stories to get access
1099
 * to any 'appSettings' sections that you have added to your system-under-test
1100
 * config file.
1101
 *
1102
 * The system-under-test config file is an important part of making your
1103
 * stories run against different versions of whatever it is you want to test.
1104
 * Instead of hard-coding app-specific settings in your stories (and then
1105
 * having to use 'if' statements to conditionally interact with the app that
1106
 * you're testing), you can simply add these settings to your
1107
 * system-under-test config files, and load the settings into your stories.
1108
 *
1109
 * @return \Prose\FromSystemUnderTest
1110
 */
1111
function fromSystemUnderTest()
1112
{
1113
    return new FromSystemUnderTest(StoryTeller::instance());
1114
}
1115
1116
/**
1117
 * returns the FromTargetsTable module
1118
 *
1119
 * This module provides access to Storyplayer's internal table of active test
1120
 * environments. It's intended for internal use by Storyplayer.
1121
 *
1122
 * You shouldn't need to call this module from your own stories.
1123
 *
1124
 * @return \Prose\FromTargetsTable
1125
 */
1126
function fromTargetsTable()
1127
{
1128
    return new FromTargetsTable(StoryTeller::instance());
1129
}
1130
1131
/**
1132
 * returns the FromTestEnvironment module
1133
 *
1134
 * This module provides you with access to the configuration for the
1135
 * active test environment.
1136
 *
1137
 * You can use this in your stories to retrieve any 'appSettings' that
1138
 * you've added to your test environment config.
1139
 *
1140
 * 'appSettings' are an important part of making your stories work against
1141
 * multiple test environments. You want all of your end-to-end tests to
1142
 * work against all of your test environments. That way, you can run them
1143
 * in development to prove that new features work, and you can run them in
1144
 * production to prove that your deployment has been successful.
1145
 *
1146
 * You can also use this from any custom modules to retrieve any
1147
 * 'moduleSettings' that have been set in the test environment config.
1148
 *
1149
 * @return \Prose\FromTestEnvironment
1150
 */
1151
function fromTestEnvironment()
1152
{
1153
    return new FromTestEnvironment(StoryTeller::instance());
1154
}
1155
1156
/**
1157
 * returns the FromUsers module
1158
 *
1159
 * This module allows you to retrieve specific user(s) from the test users
1160
 * you loaded with the --users command-line switch. Each user is a plain
1161
 * PHP object created using json_decode().
1162
 *
1163
 * Any changes you make to these users will be saved back to disk when
1164
 * Storyplayer finishes the current test run. You can prevent this by using
1165
 * the --readonly-users command-line switch.
1166
 *
1167
 * @return \Prose\FromUsers
1168
 */
1169
function fromUsers()
1170
{
1171
    return new FromUsers(StoryTeller::instance());
1172
}
1173
1174
/**
1175
 * returns the FromUuid module
1176
 *
1177
 * This module adds support for inspecting a universally-unique-ID. To use it,
1178
 * you need to have PHP's UUID extension installed.
1179
 *
1180
 * @return \Prose\FromUuid
1181
 */
1182
function fromUuid()
1183
{
1184
    return new FromUuid(StoryTeller::instance());
1185
}
1186
1187
/**
1188
 * returns the FromZmqSocket module
1189
 *
1190
 * This module adds support for receiving data via a ZeroMQ socket.
1191
 *
1192
 * @param  \ZMQSocket $zmqSocket
1193
 *         the ZeroMQ socket you want to receive data from
1194
 * @return \Prose\FromZmqSocket
1195
 */
1196
function fromZmqSocket($zmqSocket)
1197
{
1198
    return new FromZmqSocket(StoryTeller::instance(), [$zmqSocket]);
1199
}
1200
1201
/**
1202
 * return the Checkpoint object
1203
 *
1204
 * The Checkpoint is a 'data bag' - an object that you can store anything
1205
 * you like it. It's the only way for you to share any data or variables
1206
 * between the different phases of your story.
1207
 *
1208
 * Once you have the checkpoint, simply get and set properties on the object.
1209
 *
1210
 * You can also use the Asserts module on the checkpoint.
1211
 *
1212
 * @return \DataSift\Storyplayer\PlayerLib\Story_Checkpoint
1213
 */
1214
function getCheckpoint()
1215
{
1216
    return StoryTeller::instance()->getCheckpoint();
1217
}
1218
1219
/**
1220
 * shut down the running test device / web browser
1221
 *
1222
 * This function tells Storyplayer to shutdown your chosen web browser
1223
 * (chosen using the --device command-line switch).
1224
 *
1225
 * Storyplayer will normally shut down the test device for you. You should
1226
 * not need to call this from your own stories.
1227
 *
1228
 * @return void
1229
 */
1230
function stopDevice()
1231
{
1232
    return StoryTeller::instance()->stopDevice();
1233
}
1234
1235
/**
1236
 * returns the UsingBrowser module
1237
 *
1238
 * This module (along with the FromBrowser module) allows you to control a
1239
 * real web browser from your story.
1240
 *
1241
 * Internally, this module interacts with your chosen browser (you chose by
1242
 * using the --device command-line switch) using the Selenium WebDriver API.
1243
 *
1244
 * We've added a lot of helpful methods you can use to make it both quick
1245
 * and natural to control the web browser.
1246
 *
1247
 * @return \Prose\UsingBrowser
1248
 */
1249
function usingBrowser()
1250
{
1251
    return new UsingBrowser(StoryTeller::instance());
1252
}
1253
1254
/**
1255
 * returns the UsingCheckpoint module
1256
 *
1257
 * This module is old SPv1 functionality. In practice, you'll want to call
1258
 * getCheckpoint() to retrieve the checkpoint, and then interact with that
1259
 * directly.
1260
 *
1261
 * This module will probably be removed in SPv3.
1262
 *
1263
 * @return \Prose\UsingCheckpoint
1264
 */
1265
function usingCheckpoint()
1266
{
1267
    return new UsingCheckpoint(StoryTeller::instance());
1268
}
1269
1270
/**
1271
 * returns the UsingEc2 module
1272
 *
1273
 * This module provides support for creating, destroying, starting and
1274
 * stopping virtual machines on Amazon EC2. Once the machine has been started,
1275
 * you can interact with it using the FromHost() / UsingHost() module as
1276
 * normal.
1277
 *
1278
 * @return \Prose\UsingEc2
1279
 */
1280
function usingEc2()
1281
{
1282
    return new UsingEc2(StoryTeller::instance());
1283
}
1284
1285
/**
1286
 * returns the UsingEc2Instance module
1287
 *
1288
 * This module provides support for making changes to an Amazon EC2 AMI.
1289
 * You can do the basics such as creating an AMI and editing its configuration.
1290
 *
1291
 * We hope to expand on what this module can do in a future release.
1292
 *
1293
 * If you're looking to start and stop EC2 instances, use a combination of
1294
 * UsingEc2 and UsingHost modules.
1295
 *
1296
 * @param  string $amiId
1297
 *         the AMI ID to work with
1298
 * @return \Prose\UsingEc2Instance
1299
 */
1300
function usingEc2Instance($amiId)
1301
{
1302
    return new UsingEc2Instance(StoryTeller::instance(), [$amiId]);
1303
}
1304
1305
/**
1306
 * returns the UsingFile module
1307
 *
1308
 * This is an old SPv1 module that doesn't really do a lot. We'll shortly
1309
 * be introducing a new 'UsingFS' module that's much more capable. We'll
1310
 * probably remove the 'UsingFile' module in SPv3.
1311
 *
1312
 * @return \Prose\UsingFile
1313
 */
1314
function usingFile()
1315
{
1316
    return new UsingFile(StoryTeller::instance());
1317
}
1318
1319
/**
1320
 * returns the UsingHost module, with its hostId already set to the first
1321
 * host in your test environment config file that has the given role.
1322
 *
1323
 * This module provides support for making your tests work on multiple
1324
 * test environments. Instead of hard-coding hostIds into your stories,
1325
 * use this module to find a host by its assigned role. That way, it doesn't
1326
 * matter how many hosts are in different environments, or if their hostIds
1327
 * are different.
1328
 *
1329
 * This module is normally used for testing write requests via APIs. You
1330
 * should be able to write once, and then read from all hosts to prove
1331
 * that the data was correctly written (and that there are no caching errors).
1332
 *
1333
 * To read from all hosts, you would use:
1334
 *
1335
 *     foreach(hostWithRole($roleName) as $hostId) { ... }
1336
 *
1337
 * @param  string $roleName
1338
 *         the assigned role you're looking for
1339
 * @return \Prose\UsingHost
1340
 */
1341
function usingFirstHostWithRole($roleName)
1342
{
1343
    return new UsingFirstHostWithRole(StoryTeller::instance(), [$roleName]);
1344
}
1345
1346
/**
1347
 * returns the UsingForm module
1348
 *
1349
 * This module provides support for working with a given form on a HTML page
1350
 * that's open in your browser. To use it, the form must have an 'id'
1351
 * attribute set. Targetting a form with an 'id' helps make your test more
1352
 * robust, especially if there are multiple forms on the same HTML page.
1353
 *
1354
 * Use the UsingBrowser module first to open the HTML page where the form is.
1355
 *
1356
 * @param  string $formId
1357
 *         the 'id' attribute of the HTML form to use
1358
 * @return \Prose\UsingForm
1359
 */
1360
function usingForm($formId)
1361
{
1362
    return new UsingForm(StoryTeller::instance(), [$formId]);
1363
}
1364
1365
/**
1366
 * returns the UsingHost module
1367
 *
1368
 * This module provides support for running commands on a computer in your
1369
 * test environment - basically for doing anything that's likely to change
1370
 * the state of that computer.
1371
 *
1372
 * In SPv1, it was common to call this module directly from your own stories.
1373
 * In SPv2, you're much more likely to use one of our multi-host modules or
1374
 * helpers (such as usingFirstHostWithRole) so that your stories are as
1375
 * test-environment-independent as possible.
1376
 *
1377
 * @param  string $hostId
1378
 *         the ID of the host to use
1379
 * @return \Prose\UsingHost
1380
 */
1381
function usingHost($hostId)
1382
{
1383
    return new UsingHost(StoryTeller::instance(), [$hostId]);
1384
}
1385
1386
/**
1387
 * returns the UsingHostsTable module
1388
 *
1389
 * This module provides access to Storyplayer's internal list of computers
1390
 * that are running in your test environment.
1391
 *
1392
 * This module is intended for internal use by Storyplayer. You should not
1393
 * need to call this module from your own stories.
1394
 *
1395
 * @return \Prose\UsingHostsTable
1396
 */
1397
function usingHostsTable()
1398
{
1399
    return new UsingHostsTable(StoryTeller::instance());
1400
}
1401
1402
/**
1403
 * returns the UsingHttp module
1404
 *
1405
 * This module provides support for making PUT, POST and DELETE requests
1406
 * over HTTP (basically, any HTTP verb that should change state at the other
1407
 * end).
1408
 *
1409
 * SSL/TLS is fully supported.
1410
 *
1411
 * If you are using self-signed certificates, you will need to set
1412
 * 'moduleSettings.http.validateSsl = false' in your test environment's
1413
 * config file first.
1414
 *
1415
 * To make GET requests, use the FromHttp module.
1416
 *
1417
 * @return \Prose\UsingHttp
1418
 */
1419
function usingHttp()
1420
{
1421
    return new UsingHttp(StoryTeller::instance());
1422
}
1423
1424
/**
1425
 * returns the UsingLog module
1426
 *
1427
 * This module provides support for writing to the storyplayer.log. You can
1428
 * use this in your own stories whenever you to need to add an extra log
1429
 * message, for example to make it really clear what is happening.
1430
 *
1431
 * @return \Prose\UsingLog
1432
 */
1433
function usingLog()
1434
{
1435
    return new UsingLog(StoryTeller::instance());
1436
}
1437
1438
/**
1439
 * returns the UsingPDO module
1440
 *
1441
 * This module provides support for opening a PDO connection to a database
1442
 * such as MySQL. PDO is PHP's preferred way of working with SQL databases.
1443
 * Once you have an open connection, use the UsingPDODB module to execute
1444
 * SQL statements.
1445
 *
1446
 * @return \Prose\UsingPDO
1447
 */
1448
function usingPDO()
1449
{
1450
    return new UsingPDO(StoryTeller::instance());
1451
}
1452
1453
/**
1454
 * returns the UsingPDODB module
1455
 *
1456
 * This module provides support for using an open PDO connection. PDO is
1457
 * PHP's preferred way of working with SQL databases such as MySQL. To open
1458
 * a connection, use the UsingPDO module first.
1459
 *
1460
 * @param  \PDO    $db
1461
 *         a PDO connection created by UsingPDO
1462
 * @return \Prose\UsingPDODB
1463
 */
1464
function usingPDODB(PDO $db)
1465
{
1466
    return new UsingPDODB(StoryTeller::instance(), [$db]);
1467
}
1468
1469
/**
1470
 * returns the UsingProcessesTable module
1471
 *
1472
 * This module provides access to Storyplayer's internal table of which
1473
 * child processes are currently running.  Storyplayer uses this table to
1474
 * make sure that these processes are terminated when a test run ends.
1475
 *
1476
 * This module is for internal use by Storyplayer. You shouldn't need to call
1477
 * this from your own stories.
1478
 *
1479
 * @return \Prose\UsingProcessesTable
1480
 */
1481
function usingProcessesTable()
1482
{
1483
    return new UsingProcessesTable(StoryTeller::instance());
1484
}
1485
1486
/**
1487
 * returns the UsingProvisioning module
1488
 *
1489
 * This module provides support for creating a new provisioning definition,
1490
 * for use with the UsingProvisioningDefinition module.
1491
 *
1492
 * In SPv1, stories had to directly manage the provisioning of test
1493
 * environments. In SPv2, this is managed for you through the --targets
1494
 * switch.
1495
 *
1496
 * You shouldn't need to call this module directly from your stories, but
1497
 * we're leaving this functionality in just in case.
1498
 *
1499
 * It's likely that a future SPv2 release will add support for passing a
1500
 * PHP file to the --targets switch. I think this will be easier to use
1501
 * than the current JSON-based approach.
1502
 *
1503
 * @return \Prose\UsingProvisioning
1504
 */
1505
function usingProvisioning()
1506
{
1507
    return new UsingProvisioning(StoryTeller::instance());
1508
}
1509
1510
/**
1511
 * returns the UsingProvisioningDefinition module
1512
 *
1513
 * This module provides support for building up a description of what needs
1514
 * provisioning onto which computer(s) in your test environment.
1515
 *
1516
 * In SPv1, stories had to build up this definition directly. In SPv2, this
1517
 * is managed for you through the --targets switch.
1518
 *
1519
 * You shouldn't need to call this module directly from your stories, but
1520
 * we're leaving this functionality in just in case.
1521
 *
1522
 * @param  ProvisioningDefinition $def
1523
 *         a provisioning definition created by UsingProvisioning
1524
 * @return \Prose\UsingProvisioningDefinition
1525
 */
1526
function usingProvisioningDefinition(ProvisioningDefinition $def)
1527
{
1528
    return new UsingProvisioningDefinition(StoryTeller::instance(), [$def]);
1529
}
1530
1531
/**
1532
 * returns the UsingProvisioningEngine module
1533
 *
1534
 * This module provides support for invoking a supported provisioning
1535
 * engine.
1536
 *
1537
 * Provisioning engines are used to install software into a test environment.
1538
 * In SPv1, stories had to control this directly. In SPv2, this is managed
1539
 * for you through the --targets switch.
1540
 *
1541
 * You shouldn't need to call this module directly from your stories, but
1542
 * we're leaving this functionality in just in case.
1543
 *
1544
 * @param  string $engine
1545
 *         the name of a supported provisioning engine
1546
 * @return \Prose\UsingProvisioningEngine
1547
 */
1548
function usingProvisioningEngine($engine)
1549
{
1550
    return new UsingProvisioningEngine(StoryTeller::instance(), [$engine]);
1551
}
1552
1553
/**
1554
 * returns the UsingRedis module
1555
 *
1556
 * This module provides support for opening connections to a Redis server.
1557
 * Once the connection is open, you can then use the UsingRedisConn module
1558
 * to work with the Redis server.
1559
 *
1560
 * Redis is a very popular key/value store (and a whole lot more) - it's a
1561
 * bit like Memcached on steroids :)
1562
 *
1563
 * @return \Prose\UsingRedis
1564
 */
1565
function usingRedis()
1566
{
1567
    return new UsingRedis(StoryTeller::instance());
1568
}
1569
1570
/**
1571
 * returns the UsingRedisConn module
1572
 *
1573
 * This module provides support for working with a Redis server. Redis is
1574
 * a very popular key/value store (and a whole lot more) - it's a bit like
1575
 * Memcached on steroids :)
1576
 *
1577
 * To make a connection to Redis, use the UsingRedis module first.
1578
 *
1579
 * @param  PredisClient $client
1580
 *         a Redis client created by the UsingRedis module
1581
 * @return \Prose\UsingRedisConn
1582
 */
1583
function usingRedisConn(PredisClient $client)
1584
{
1585
    return new UsingRedisConn(StoryTeller::instance(), [$client]);
1586
}
1587
1588
/**
1589
 * returns the UsingReporting module
1590
 *
1591
 * This module was originally added to SPv1 as a way of logging that a
1592
 * particular Story phase was deliberately empty. In practice, it was rarely
1593
 * used. It will be removed in SPv3.
1594
 *
1595
 * @deprecated 2.1.0 old SPv1 functionality that isn't really needed
1596
 * @return \Prose\UsingReporting
1597
 */
1598
function usingReporting()
1599
{
1600
    return new UsingReporting(StoryTeller::instance());
1601
}
1602
1603
/**
1604
 * returns the UsingRolesTable module
1605
 *
1606
 * This module provides access to Storyplayer's table of active roles in
1607
 * your test environment.
1608
 *
1609
 * This module is for internal use inside Storyplayer. You shouldn't need to
1610
 * use it from your own stories.
1611
 *
1612
 * @return \Prose\UsingRolesTable
1613
 */
1614
function usingRolesTable()
1615
{
1616
    return new UsingRolesTable(StoryTeller::instance());
1617
}
1618
1619
/**
1620
 * returns the UsingRuntimeTable module
1621
 *
1622
 * This module provides access to Storyplayer's internal state, also known
1623
 * as the table of tables.
1624
 *
1625
 * This module is for internal use inside Storyplayer. You shouldn't need to
1626
 * use it from your own stories.
1627
 *
1628
 * @return \Prose\UsingRuntimeTable
1629
 */
1630
function usingRuntimeTable($tableName)
1631
{
1632
    return new UsingRuntimeTable(StoryTeller::instance(), [$tableName]);
1633
}
1634
1635
/**
1636
 * returns the UsingRuntimeTableForTargetEnvironment module
1637
 *
1638
 * this module is for internal use inside Storyplayer
1639
 * you shouldn't need to use it from your own stories
1640
 *
1641
 * @return \Prose\UsingRuntimeTableForTargetEnvironment
1642
 */
1643
function usingRuntimeTableForTargetEnvironment($tableName)
1644
{
1645
    return new UsingRuntimeTableForTargetEnvironment(StoryTeller::instance(), [$tableName]);
1646
}
1647
1648
/**
1649
 * returns the UsingSauceLabs module
1650
 *
1651
 * At the moment, this module is a placeholder. We hope to add full support
1652
 * for the SauceLabs API soon.
1653
 *
1654
 * You can use the --device switch to run your tests using SauceLabs'
1655
 * platform today. Supported devices start with the prefix 'sl_'.
1656
 *
1657
 * @return \Prose\UsingSauceLabs
1658
 */
1659
function usingSauceLabs()
1660
{
1661
    return new UsingSauceLabs(StoryTeller::instance());
1662
}
1663
1664
/**
1665
 * returns the UsingSavageD module
1666
 *
1667
 * This module provides support for controlling the SavageD data-gathering
1668
 * daemon via its REST API. SavageD is a great tool for realtime (to the
1669
 * second) monitoring of processes and memory in your test environment. It
1670
 * was originally built to help load test the DataSift platform.
1671
 *
1672
 * @return \Prose\UsingSavageD
1673
 */
1674
function usingSavageD($hostId)
1675
{
1676
    return new UsingSavageD(StoryTeller::instance(), [$hostId]);
1677
}
1678
1679
/**
1680
 * returns the UsingShell module
1681
 *
1682
 * This module provides support for running commands via the UNIX shell.
1683
 * These commands will run on the same computer where Storyplayer is running.
1684
 *
1685
 * If you want to run commands on a computer in your test environment, you
1686
 * need the UsingHost module.
1687
 *
1688
 * @return \Prose\UsingShell
1689
 */
1690
function usingShell()
1691
{
1692
    return new UsingShell(StoryTeller::instance());
1693
}
1694
1695
/**
1696
 * returns the UsingSupervisor module
1697
 *
1698
 * This module provides support for working the the Supervisor daemon on
1699
 * a host in your test environment. Supervisor is an increasingly popular
1700
 * solution for looking after network daemons.
1701
 *
1702
 * @param  string $hostId
1703
 *         the ID of the host you want to use Supervisor on
1704
 * @return \Prose\UsingSupervisor
1705
 */
1706
function usingSupervisor($hostId)
1707
{
1708
    return new UsingSupervisor(StoryTeller::instance(), [$hostId]);
1709
}
1710
1711
/**
1712
 * returns the UsingTargetsTable module
1713
 *
1714
 * This module provides access to Storyplayer's internal table of active
1715
 * test environments. It's intended for internal use by Storyplayer.
1716
 *
1717
 * You shouldn't need to call this module from your own stories.
1718
 *
1719
 * @return \Prose\UsingTargetsTable
1720
 */
1721
function usingTargetsTable()
1722
{
1723
    return new UsingTargetsTable(StoryTeller::instance());
1724
}
1725
1726
/**
1727
 * returns the UsingTimer module
1728
 *
1729
 * This module provides useful helpers for when your story needs to pause
1730
 * or wait for something to happen. If you use these helpers in your stories,
1731
 * you'll get entries in storyplayer.log (and on the --dev console) telling
1732
 * you about these pauses and wait states.
1733
 *
1734
 * You don't get these log messages if you call PHP's sleep() directly from
1735
 * your story.
1736
 *
1737
 * @return \Prose\UsingTimer
1738
 */
1739
function usingTimer()
1740
{
1741
    return new UsingTimer(StoryTeller::instance());
1742
}
1743
1744
/**
1745
 * returns the UsingUsers module
1746
 *
1747
 * This module provides support for working with the test users loaded via
1748
 * the --users switch.
1749
 *
1750
 * Storyplayer uses this module internally to load and save the test users
1751
 * file. You can also load your own files directly from your stories if you
1752
 * need to.
1753
 *
1754
 * @return \Prose\UsingUsers
1755
 */
1756
function usingUsers()
1757
{
1758
    return new UsingUsers(StoryTeller::instance());
1759
}
1760
1761
/**
1762
 * returns the UsingVagrant module
1763
 *
1764
 * This module provides support for working with Vagrant, the CLI tool for
1765
 * managing virtual machines locally and (increasingly) in the cloud
1766
 *
1767
 * A big change in Storyplayer v2 was to move responsibility for creating
1768
 * and destroying test environments out of stories, and into their own
1769
 * configuration files. As a result, it's unlikely you'll need to call this
1770
 * module from your own stories.
1771
 *
1772
 * @return \Prose\UsingVagrant
1773
 */
1774
function usingVagrant()
1775
{
1776
    return new UsingVagrant(StoryTeller::instance());
1777
}
1778
1779
/**
1780
 * returns the UsingYamlFile module
1781
 *
1782
 * This module provides support for working with YAML format files.
1783
 *
1784
 * @param  string $filename
1785
 *         the filename to work with
1786
 * @return \Prose\UsingYamlFile
1787
 */
1788
function usingYamlFile($filename)
1789
{
1790
    return new UsingYamlFile(StoryTeller::instance(), [$filename]);
1791
}
1792
1793
/**
1794
 * returns the UsingZmq module
1795
 *
1796
 * This module provides support for working with ZeroMQ, the no-broker
1797
 * inter-process queuing library.
1798
 *
1799
 * @return \Prose\UsingZmq
1800
 */
1801
function usingZmq()
1802
{
1803
    return new UsingZmq(StoryTeller::instance());
1804
}
1805
1806
/**
1807
 * returns the UsingZmqContext module
1808
 *
1809
 * This module provides support for creating ZeroMQ sockets
1810
 *
1811
 * @param  \ZMQContext|null $zmqContext
1812
 *         the ZMQContext to use when creating the socket
1813
 *         (leave empty and we'll create a context for you)
1814
 * @return \Prose\UsingZmqContext
1815
 */
1816
function usingZmqContext($zmqContext = null, $ioThreads = 1)
1817
{
1818
    return new UsingZmqContext(StoryTeller::instance(), [$zmqContext, $ioThreads]);
1819
}
1820
1821
/**
1822
 * returns the UsingZmqSocket module
1823
 *
1824
 * This module provides support for sending messages via a ZeroMQ socket
1825
 *
1826
 * @param  \ZMQSocket $zmqSocket
1827
 *         the socket to send on
1828
 * @return \Prose\UsingZmqSocket
1829
 */
1830
function usingZmqSocket($zmqSocket)
1831
{
1832
    return new UsingZmqSocket(StoryTeller::instance(), [$zmqSocket]);
1833
}
1834
1835
// ==================================================================
1836
//
1837
// Iterators go here
1838
//
1839
// ------------------------------------------------------------------
1840
1841
/**
1842
 * iterate over all hosts that match the given role, and return only the first one
1843
 *
1844
 * @param  string $roleName
1845
 *         The role that we want
1846
 *
1847
 * @return Iterator
1848
 *         a hostid that matches the role
1849
 */
1850 View Code Duplication
function firstHostWithRole($roleName)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1851
{
1852
    $listOfHosts = fromRolesTable()->getDetailsForRole($roleName);
1853
    if (!count(get_object_vars($listOfHosts))) {
1854
        throw new E5xx_ActionFailed(__METHOD__, "unknown role '{$roleName}' or no hosts for that role");
1855
    }
1856
1857
    // what are we doing?
1858
    $log = usingLog()->startAction("for the first host with role '{$roleName}' ... ");
1859
1860
    // we yield a single host ID ...
1861
    $hostsAsArray = get_object_vars($listOfHosts);
1862
    $hostDetails  = array_pop($hostsAsArray);
1863
    yield($hostDetails->hostId);
1864
1865
    // all done
1866
    $log->endAction();
1867
}
1868
1869
/**
1870
 * iterate over all hosts that match the given role, and return only the last one
1871
 *
1872
 * @param  string $roleName
1873
 *         The role that we want
1874
 *
1875
 * @return Iterator
1876
 *         a hostid that matches the role
1877
 */
1878 View Code Duplication
function lastHostWithRole($roleName)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1879
{
1880
    $listOfHosts = fromRolesTable()->getDetailsForRole($roleName);
1881
    if (!count(get_object_vars($listOfHosts))) {
1882
        throw new E5xx_ActionFailed(__METHOD__, "unknown role '{$roleName}' or no hosts for that role");
1883
    }
1884
1885
    // what are we doing?
1886
    $log = usingLog()->startAction("for the last host with role '{$roleName}' ... ");
1887
1888
    // we yield a single host ID ...
1889
    $hostsAsArray = get_object_vars($listOfHosts);
1890
    $hostDetails = end($hostsAsArray);
1891
    yield($hostDetails->hostId);
1892
1893
    // all done
1894
    $log->endAction();
1895
}
1896
1897
/**
1898
 * iterate over all hosts that match the given role
1899
 *
1900
 * @param  string $roleName
1901
 *         The role that we want
1902
 *
1903
 * @return Iterator
1904
 *         a hostid that matches the role
1905
 */
1906
function hostWithRole($roleName)
1907
{
1908
    // special case
1909
    if ($roleName instanceof StoryTeller) {
1910
        throw new E5xx_ActionFailed(__METHOD__, "first param to hostWithRole() is no longer \$st");
1911
    }
1912
1913
    $listOfHosts = fromRolesTable()->getDetailsForRole($roleName);
1914
    if (!count(get_object_vars($listOfHosts))) {
1915
        throw new E5xx_ActionFailed(__METHOD__, "unknown role '{$roleName}' or no hosts for that role");
1916
    }
1917
1918
    // what are we doing?
1919
    $log = usingLog()->startAction("for each host with role '{$roleName}' ... ");
1920
1921
    foreach ($listOfHosts as $hostDetails) {
1922
        yield($hostDetails->hostId);
1923
    }
1924
1925
    // all done
1926
    $log->endAction();
1927
}