Skip to content

Commit

Permalink
[stopwatch] Added test and cleanup. (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored Aug 24, 2021
1 parent bedd05c commit a09f230
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 39 deletions.
1 change: 0 additions & 1 deletion src/Phing/Parser/ExpatParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public function __construct(Reader $reader, $filename = null)
$this->file = new SplFileObject($filename);
}
$this->parser = xml_parser_create();
$this->buffer = 4096;
$this->location = new Location();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, [$this, 'startElement'], [$this, 'endElement']);
Expand Down
31 changes: 16 additions & 15 deletions src/Phing/Task/Optional/StopwatchTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class StopwatchTask extends DispatchTask
*
* @var Stopwatch
*/
private static $timer = null;
private static $timer;

/**
* Initialize Task.
Expand All @@ -68,7 +68,7 @@ public function init()
/**
* Start timer.
*/
public function start()
public function start(): void
{
$timer = $this->getStopwatchInstance();
$timer->start($this->name, $this->category);
Expand All @@ -77,31 +77,32 @@ public function start()
/**
* Stop timer.
*/
public function stop()
public function stop(): void
{
$timer = $this->getStopwatchInstance();
$event = $timer->stop($this->name);

foreach ($event->getPeriods() as $period) {
$this->log(
'Starttime: ' . $period->getStartTime() . ' - Endtime: ' . $period->getEndTime() . ' - Duration: ' . $period->getDuration() . ' - Memory: ' . $period->getMemory(),
'Starttime: ' . $period->getStartTime() . ' - Endtime: ' . $period->getEndTime() .
' - Duration: ' . $period->getDuration() . ' - Memory: ' . $period->getMemory(),
Project::MSG_INFO
);
}

$this->log('Name: ' . $this->name, Project::MSG_INFO);
$this->log('Category: ' . $event->getCategory(), Project::MSG_INFO);
$this->log('Origin: ' . $event->getOrigin(), Project::MSG_INFO);
$this->log('Start time: ' . $event->getStartTime(), Project::MSG_INFO);
$this->log('End time: ' . $event->getEndTime(), Project::MSG_INFO);
$this->log('Duration: ' . $event->getDuration(), Project::MSG_INFO);
$this->log('Memory: ' . $event->getMemory(), Project::MSG_INFO);
$this->log('Name: ' . $this->name);
$this->log('Category: ' . $event->getCategory());
$this->log('Origin: ' . $event->getOrigin());
$this->log('Start time: ' . $event->getStartTime());
$this->log('End time: ' . $event->getEndTime());
$this->log('Duration: ' . $event->getDuration());
$this->log('Memory: ' . $event->getMemory());
}

/**
* Measure lap time.
*/
public function lap()
public function lap(): void
{
$timer = $this->getStopwatchInstance();
$timer->lap($this->name);
Expand All @@ -112,7 +113,7 @@ public function lap()
*
* @param string $name the name of the stopwatch timer
*/
public function setName($name)
public function setName(string $name): void
{
$this->name = $name;
}
Expand All @@ -122,7 +123,7 @@ public function setName($name)
*
* @param string $category
*/
public function setCategory($category)
public function setCategory(string $category): void
{
$this->category = $category;
}
Expand Down Expand Up @@ -160,7 +161,7 @@ public function main()
*
* @return Stopwatch
*/
private function getStopwatchInstance()
private function getStopwatchInstance(): Stopwatch
{
if (null === self::$timer) {
self::$timer = new Stopwatch();
Expand Down
54 changes: 54 additions & 0 deletions tests/Phing/Task/Optional/StopwatchTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

namespace Phing\Test\Task\Optional;

use Phing\Test\Support\BuildFileTest;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* Test for StopwatchTask
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
*/
class StopwatchTaskTest extends BuildFileTest
{
/**
* Sets up the fixture.
* @throws \Phing\Io\IOException
*/
public function setUp(): void
{
if (!class_exists('Symfony\\Component\\Stopwatch\\Stopwatch')) {
$this->markTestSkipped('Need stopwatch installed to test');
}

$this->configureProject(PHING_TEST_BASE . '/etc/tasks/ext/StopwatchTaskTest.xml');
}

public function testStopwatch()
{
$this->expectLogContaining(__FUNCTION__, 'Category: test-cat');
}

public function testStopwatchFails()
{
$this->expectBuildExceptionContaining(__FUNCTION__, 'method does not exists', 'Method foo does not exist');
}
}
36 changes: 13 additions & 23 deletions tests/Phing/Task/Optional/SymfonyConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,8 @@ protected function setUp(): void
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown(): void
{
}

/**
* @covers \SymfonyConsoleTask::getCommand
* @covers \SymfonyConsoleTask::setCommand
* @covers SymfonyConsoleTask::getCommand
* @covers SymfonyConsoleTask::setCommand
*/
public function testSetGetCommand(): void
{
Expand All @@ -67,8 +59,8 @@ public function testSetGetCommand(): void
}

/**
* @covers \SymfonyConsoleTask::getConsole
* @covers \SymfonyConsoleTask::setConsole
* @covers SymfonyConsoleTask::getConsole
* @covers SymfonyConsoleTask::setConsole
*/
public function testSetGetConsole(): void
{
Expand All @@ -78,8 +70,8 @@ public function testSetGetConsole(): void
}

/**
* @covers \SymfonyConsoleTask::getDebug
* @covers \SymfonyConsoleTask::setDebug
* @covers SymfonyConsoleTask::getDebug
* @covers SymfonyConsoleTask::setDebug
*/
public function testSetGetDebug(): void
{
Expand All @@ -89,8 +81,8 @@ public function testSetGetDebug(): void
}

/**
* @covers \SymfonyConsoleTask::getSilent
* @covers \SymfonyConsoleTask::setSilent
* @covers SymfonyConsoleTask::getSilent
* @covers SymfonyConsoleTask::setSilent
*/
public function testSetGetSilent(): void
{
Expand All @@ -100,7 +92,7 @@ public function testSetGetSilent(): void
}

/**
* @covers \SymfonyConsoleTask::createArg
* @covers SymfonyConsoleTask::createArg
*/
public function testCreateArg(): void
{
Expand All @@ -110,7 +102,7 @@ public function testCreateArg(): void
}

/**
* @covers \SymfonyConsoleTask::getArgs
* @covers SymfonyConsoleTask::getArgs
*/
public function testGetArgs(): void
{
Expand All @@ -122,9 +114,7 @@ public function testGetArgs(): void
}

/**
* @covers \SymfonyConsoleTask::getCmdString
*
* @todo Implement testMain().
* @covers SymfonyConsoleTask::getCmdString
*/
public function testGetCmdString(): void
{
Expand All @@ -142,7 +132,7 @@ public function testGetCmdString(): void
}

/**
* @covers \SymfonyConsoleTask::getCmdString
* @covers SymfonyConsoleTask::getCmdString
*/
public function testNoDebugGetCmdString(): void
{
Expand All @@ -161,7 +151,7 @@ public function testNoDebugGetCmdString(): void
}

/**
* @covers \SymfonyConsoleTask::getCmdString
* @covers SymfonyConsoleTask::getCmdString
*/
public function testNoDebugOnlyOnce(): void
{
Expand Down
17 changes: 17 additions & 0 deletions tests/etc/tasks/ext/StopwatchTaskTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>

<project name="stopwatch test" default="testStopwatch">

<target name="testStopwatch">
<stopwatch name="test" category="test-cat" action="start"/>
<sleep milliseconds="10"/>
<stopwatch name="test" category="test-cat" action="lap"/>
<sleep milliseconds="10"/>
<stopwatch name="test" category="test-cat" action="stop"/>
</target>

<target name="testStopwatchFails">
<stopwatch name="test" action="foo"/>
</target>

</project>

0 comments on commit a09f230

Please sign in to comment.