<?php

// ========================================================================
//
// STORY DETAILS
//
// ------------------------------------------------------------------------
$story = newStoryFor('Storyplayer')->inGroup('Config')->called('Can get system-under-test storySettings');
$story->requiresStoryplayerVersion(2);
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // nothing to do
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $storySettings = fromSystemUnderTest()->getStorySetting('testData');
    assertsObject($storySettings)->isNotNull();
    assertsObject($storySettings)->hasAttribute('name');
    assertsObject($storySettings)->hasAttribute('version');
    assertsObject($storySettings)->hasAttribute('isStorySettings');
});
    $checkpoint = getCheckpoint();
    // load our test page
    usingBrowser()->gotoPage("file://" . __DIR__ . '/../../testpages/WorkingWithWindows.html');
    // get the h1
    $checkpoint->mainHeader = fromBrowser()->getText()->fromHeadingWithId('storyplayer_working_with_windows');
    // open the second window
    usingBrowser()->click()->linkWithText('open a second window');
    // switch to the second window
    usingBrowser()->switchToWindow("Storyplayer: Second Window");
    // get the h1
    $checkpoint->secondHeader = fromBrowser()->getText()->fromHeadingWithId('storyplayer_second_window');
    // close the second window
    // this leaves the browser in a bit of a state
    usingBrowser()->closeCurrentWindow();
    // switch back to the first window
    usingBrowser()->switchToWindow("Storyplayer: Working With Windows");
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    // get the checkpoint
    $checkpoint = getCheckpoint();
    // do we have the content we expected?
    assertsObject($checkpoint)->hasAttribute('mainHeader');
    assertsString($checkpoint->mainHeader)->equals("Storyplayer: Working With Windows");
    assertsObject($checkpoint)->hasAttribute('secondHeader');
    assertsString($checkpoint->secondHeader)->equals("Storyplayer: Second Window");
});
        $checkpoint->test108Passed = true;
    }
    try {
        assertsObject($actualData)->hasAttributeWithValue('attribute11', 99);
    } catch (Exception $e) {
        $checkpoint->test109Passed = true;
    }
    try {
        assertsObject($actualData)->hasAttributeWithValue('attribute11', $checkpoint);
    } catch (Exception $e) {
        $checkpoint->test110Passed = true;
    }
    try {
        assertsObject($actualData)->hasAttributeWithValue('attribute11', "");
    } catch (Exception $e) {
        $checkpoint->test111Passed = true;
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    for ($x = 1; $x <= 111; $x++) {
        $attributeName = "test{$x}Passed";
        assertsObject($checkpoint)->hasAttribute($attributeName);
        assertsBoolean($checkpoint->{$attributeName})->equals(true);
    }
});
//
// STORY DETAILS
//
// ------------------------------------------------------------------------
$story = newStoryFor('Storyplayer')->inGroup('Stories > Checkpoint')->called('Each story starts with empty checkpoint (pt 2)');
$story->requiresStoryplayerVersion(2);
// ========================================================================
//
// TEST SETUP / TEARDOWN
//
// ------------------------------------------------------------------------
$story->addTestSetup(function () {
    // do nothing
});
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->isEmpty();
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function ($st) {
    // nothing to do
});
    assertsString($stringData)->doesNotEndWith("name");
    $stringData = "filename.json";
    assertsString($stringData)->doesNotEndWith(".jso");
    $stringData = "filename.json";
    assertsString($stringData)->doesNotEndWith("file");
    // and these should fail
    try {
        $stringData = "hello, Storyplayer";
        assertsString($stringData)->doesNotEndWith("player");
    } catch (Exception $e) {
        $checkpoint->test1Passed = true;
    }
    try {
        $stringData = "filename.json";
        assertsString($stringData)->doesNotEndWith(".json");
    } catch (Exception $e) {
        $checkpoint->test2Passed = true;
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    for ($i = 1; $i <= 2; $i++) {
        $attribute = "test{$i}Passed";
        assertsObject($checkpoint)->hasAttributeWithValue($attribute, true);
    }
});
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    $checkpoint = getCheckpoint();
    // this should pass
    $testData1 = ["alpha" => "a", "bravo" => "b", "charlie" => "c", "delta" => "d", "echo" => [1, 2, 3, 4, 5]];
    $testData2 = ["alpha" => "a", "bravo" => "b", "charlie" => "c", "delta" => "d", "echo" => [1, 2, 3, 4, 5]];
    assertsArray($testData1)->equals($testData2);
    // and this should fail
    $testData3 = [1];
    $checkpoint->test2Exception = false;
    try {
        assertsArray($testData3)->equals($testData1);
    } catch (Exception $e) {
        $checkpoint->test2Exception = true;
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->hasAttribute("test2Exception");
    assertsBoolean($checkpoint->test2Exception)->isTrue();
});
    try {
        $stringData = "";
        assertsDouble($stringData)->isDouble();
    } catch (Exception $e) {
        $checkpoint->stringTestPassed = true;
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->hasAttribute("nullTestPassed");
    assertsBoolean($checkpoint->nullTestPassed)->isTrue();
    assertsObject($checkpoint)->hasAttribute("arrayTestPassed");
    assertsBoolean($checkpoint->arrayTestPassed)->isTrue();
    assertsObject($checkpoint)->hasAttribute("booleanTest1Passed");
    assertsBoolean($checkpoint->booleanTest1Passed)->isTrue();
    assertsObject($checkpoint)->hasAttribute("booleanTest2Passed");
    assertsBoolean($checkpoint->booleanTest2Passed)->isTrue();
    assertsObject($checkpoint)->hasAttribute("intTest1Passed");
    assertsBoolean($checkpoint->intTest1Passed)->isTrue();
    assertsObject($checkpoint)->hasAttribute("intTest2Passed");
    assertsBoolean($checkpoint->intTest2Passed)->isTrue();
    assertsObject($checkpoint)->hasAttribute("objectTestPassed");
    assertsBoolean($checkpoint->objectTestPassed)->isTrue();
    assertsObject($checkpoint)->hasAttribute("stringTestPassed");
    assertsBoolean($checkpoint->stringTestPassed)->isTrue();
});
    $checkpoint->expectedData = "new test data";
});
$story->addTestTeardown(function () {
    $test1 = fromUsers()->getUser("test1");
    if (isset($test1->extraData)) {
        unset($test1->extraData);
    }
});
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    $checkpoint = getCheckpoint();
    $test1 = fromUsers()->getUser("test1");
    assertsObject($test1)->doesNotHaveAttribute("extraData");
    $test1->extraData = $checkpoint->expectedData;
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function ($st) {
    $checkpoint = getCheckpoint();
    $test1 = fromUsers()->getUser("test1");
    assertsObject($test1)->isObject();
    assertsObject($test1)->hasAttribute("extraData");
    assertsString($test1->extraData)->equals($checkpoint->expectedData);
});
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // get the checkpoint, to store data in
    $checkpoint = getCheckpoint();
    // load our test page
    usingBrowser()->gotoPage("file://" . __DIR__ . '/../../testpages/WorkingWithIFrames.html');
    // get a h1
    $checkpoint->mainHeader = fromBrowser()->getText()->fromHeadingWithId('storyplayer_working_with_iframes');
    // switch to the iFrame
    usingBrowser()->switchToIframe('iframe1');
    // get the h1 now
    $checkpoint->iFrameHeader = fromBrowser()->getText()->fromHeadingWithId('storyplayer_working_with_iframes');
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    // get the checkpoint
    $checkpoint = getCheckpoint();
    // do we have the content we expected?
    assertsObject($checkpoint)->hasAttribute('mainHeader');
    assertsString($checkpoint->mainHeader)->equals("Storyplayer: Working With IFrames");
    assertsObject($checkpoint)->hasAttribute('iFrameHeader');
    assertsString($checkpoint->iFrameHeader)->equals("IFrame Content");
});
<?php

// ========================================================================
//
// STORY DETAILS
//
// ------------------------------------------------------------------------
$story = newStoryFor('Storyplayer')->inGroup('Config')->called('Can get system-under-test appSettings');
$story->requiresStoryplayerVersion(2);
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // nothing to do
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $appSettings = fromSystemUnderTest()->getAppSettings('testData');
    assertsObject($appSettings)->isNotNull();
    assertsObject($appSettings)->hasAttribute('name');
    assertsObject($appSettings)->hasAttribute('version');
    assertsObject($appSettings)->hasAttribute('isAppSettings');
});
// STORY SETUP / TEAR-DOWN
//
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    $checkpoint = getCheckpoint();
    // this should pass
    $objectData = new stdClass();
    assertsObject($objectData)->isNotNull();
    // and these should fail
    try {
        $nullData = null;
        assertsObject($nullData)->isNotNull();
    } catch (Exception $e) {
        $checkpoint->nullTestPassed = true;
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->hasAttribute("nullTestPassed");
    assertsBoolean($checkpoint->nullTestPassed)->isTrue();
});
    // do we have the UUID extension installed?
    expectsUuid()->requirementsAreMet();
});
// ========================================================================
//
// PRE-TEST INSPECTION
//
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // get the checkpoint, to store data in
    $checkpoint = getCheckpoint();
    // create a UUID
    $checkpoint->uuid = fromUuid()->generateUuid();
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    // get the checkpoint
    $checkpoint = getCheckpoint();
    // do we have the content we expected?
    assertsObject($checkpoint)->hasAttribute('uuid');
    assertsString($checkpoint->uuid)->isUuid();
});
// ========================================================================
//
// STORY DETAILS
//
// ------------------------------------------------------------------------
$story = newStoryFor('Storyplayer')->inGroup('Modules')->called('Users: Has loaded test users file');
$story->requiresStoryplayerVersion(2);
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // no op
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function ($st) {
    $test1 = fromUsers()->getUser("test1");
    $test2 = fromUsers()->getUser("test2");
    assertsObject($test1)->isObject();
    assertsObject($test1)->hasAttribute("username");
    assertsString($test1->username)->equals("test 1");
    assertsObject($test2)->isObject();
    assertsObject($test2)->hasAttribute("username");
    assertsString($test2->username)->equals("test 2");
});
// ------------------------------------------------------------------------
// ========================================================================
//
// PRE-TEST INSPECTION
//
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // get the checkpoint, to store data in
    $checkpoint = getCheckpoint();
    // load our test page
    usingBrowser()->gotoPage("file://" . __DIR__ . '/../../testpages/WorkingWithForms.html');
    // get a field from a form
    $checkpoint->field1 = fromForm("test_form")->getValue()->fieldLabelled('Page Name');
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    // get the checkpoint
    $checkpoint = getCheckpoint();
    // do we have the title we expected?
    assertsObject($checkpoint)->hasAttribute('field1');
    assertsString($checkpoint->field1)->equals("Storyplayer: Working With Forms");
});
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    $checkpoint = getCheckpoint();
    $checkpoint->test3Passed = false;
    // this should pass
    $testData1 = true;
    assertsBoolean($testData1)->isNotNull();
    $testData2 = false;
    assertsBoolean($testData2)->isNotNull();
    // these should all fail
    $testData3 = null;
    try {
        assertsBoolean($testData3)->isNotNull();
    } catch (Exception $e) {
        $checkpoint->test3Passed = true;
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->hasAttribute('test3Passed');
    assertsBoolean($checkpoint->test3Passed)->isTrue();
});
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // get the checkpoint, to store data in
    $checkpoint = getCheckpoint();
    // load our test page
    usingBrowser()->gotoPage("file://" . __DIR__ . '/../../testpages/index.html');
    // resize the window
    usingBrowser()->resizeCurrentWindow($checkpoint->expectedDimensions['width'], $checkpoint->expectedDimensions['height']);
    // remember the new size for later
    $checkpoint->actualDimensions = fromBrowser()->getCurrentWindowSize();
    // did the browser change size?
    expectsBrowser()->currentWindowSizeIs($checkpoint->expectedDimensions['width'], $checkpoint->expectedDimensions['height']);
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    // get the checkpoint
    $checkpoint = getCheckpoint();
    // do we have the dimensions at all?
    assertsObject($checkpoint)->hasAttribute('expectedDimensions');
    assertsObject($checkpoint)->hasAttribute('actualDimensions');
    // do they match?
    assertsArray($checkpoint->actualDimensions)->equals($checkpoint->expectedDimensions);
});
//
// ------------------------------------------------------------------------
$story = newStoryFor('Storyplayer')->inGroup('Stories > Checkpoint')->called('Each story starts with empty checkpoint (pt 1)');
$story->requiresStoryplayerVersion(2);
// ========================================================================
//
// TEST SETUP / TEARDOWN
//
// ------------------------------------------------------------------------
$story->addTestSetup(function () {
    // do nothing
});
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    $checkpoint = getCheckpoint();
    $checkpoint->thisDataShouldDisappearInPt2 = true;
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function ($st) {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->hasAttribute('thisDataShouldDisappearInPt2');
    assertsBoolean($checkpoint->thisDataShouldDisappearInPt2)->isTrue();
});
// ------------------------------------------------------------------------
// ========================================================================
//
// PRE-TEST INSPECTION
//
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // get the checkpoint, to store data in
    $checkpoint = getCheckpoint();
    // load our test page
    usingBrowser()->gotoPage("file://" . __DIR__ . '/../../testpages/index.html');
    // get a h2 by its ID
    $checkpoint->content = fromBrowser()->getText()->fromHeadingWithId('self_test_website');
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    // get the checkpoint
    $checkpoint = getCheckpoint();
    // do we have the content we expected?
    assertsObject($checkpoint)->hasAttribute('content');
    assertsString($checkpoint->content)->equals("Self-Test Website");
});
//
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    $checkpoint = getCheckpoint();
    // this should pass
    $actualData = 2.0;
    $expectedData1 = 1;
    assertsDouble($actualData)->isGreaterThan($expectedData1);
    // and these should fail
    try {
        $expectedData2 = 2;
        assertsDouble($actualData)->isGreaterThan($expectedData2);
    } catch (Exception $e) {
        $checkpoint->test2Passed = true;
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->hasAttribute("test2Passed");
    assertsBoolean($checkpoint->test2Passed)->isTrue();
});
//
// PRE-TEST INSPECTION
//
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // get the checkpoint, to store data in
    $checkpoint = getCheckpoint();
    // load our test page
    usingBrowser()->gotoPage("http://news.bbc.co.uk");
    // get the title of the test page
    $checkpoint->title = fromBrowser()->getTitle();
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    // get the checkpoint
    $checkpoint = getCheckpoint();
    // what title are we expecting?
    $expectedTitle = fromStoryplayer()->getStorySetting("modules.http.remotePage.title");
    // do we have the title we expected?
    assertsObject($checkpoint)->hasAttribute('title');
    assertsString($checkpoint->title)->equals($expectedTitle);
});
//
// ------------------------------------------------------------------------
// ========================================================================
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    // we're going to store the received message in here
    $checkpoint = getCheckpoint();
    foreach (firstHostWithRole("zmq_target") as $hostId) {
        $context = usingZmqContext()->getZmqContext();
        $inPort = fromHost($hostId)->getStorySetting("zmq.single.inPort");
        $outPort = fromHost($hostId)->getStorySetting("zmq.single.outPort");
        $inSocket = usingZmqContext($context)->connectToHost($hostId, $inPort, 'PUSH');
        $outSocket = usingZmqContext($context)->connectToHost($hostId, $outPort, 'PULL');
        usingZmqSocket($inSocket)->send($checkpoint->expectedMessage);
        $checkpoint->actualMessage = fromZmqSocket($outSocket)->recv();
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    $checkpoint = getCheckpoint();
    assertsObject($checkpoint)->hasAttribute("expectedMessage");
    assertsObject($checkpoint)->hasAttribute("actualMessage");
    assertsString($checkpoint->actualMessage)->equals($checkpoint->expectedMessage);
});
//
// POSSIBLE ACTION(S)
//
// ------------------------------------------------------------------------
$story->addAction(function () {
    foreach (hostWithRole('upload_target') as $hostId) {
        // get the default user details for this test environment
        $hostUser = fromHost($hostId)->getStorySetting("user.username");
        $hostGroup = fromHost($hostId)->getStorySetting("user.group");
        // get the details for this file
        $details = fromHost($hostId)->getFileDetails('testfile.txt');
        // make sure we have the details that we expect
        assertsObject($details)->isNotNull();
        assertsObject($details)->hasAttribute("user");
        assertsString($details->user)->equals($hostUser);
        assertsObject($details)->hasAttribute("group");
        assertsString($details->group)->equals($hostGroup);
    }
});
// ========================================================================
//
// POST-TEST INSPECTION
//
// ------------------------------------------------------------------------
$story->addPostTestInspection(function () {
    foreach (hostWithRole('upload_target') as $hostId) {
        // get the default user details for this test environment
        $hostUser = fromHost($hostId)->getStorySetting("user.username");
        $hostGroup = fromHost($hostId)->getStorySetting("user.group");
        // check the details for this file
        expectsHost($hostId)->hasFileWithPermissions('testfile.txt', $hostUser, $hostGroup, 0644);