Skip to content

Commit

Permalink
* added test project with NUnit
Browse files Browse the repository at this point in the history
* fixed issue introduced in commit 4141d69 (wrong variable test in constructor)
* implemented fix for #37
* added test for GetCustomVariable()
  • Loading branch information
ptr1120 committed Jan 17, 2017
1 parent 0b75159 commit 2697fe6
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 17 deletions.
67 changes: 67 additions & 0 deletions Piwik.Tracker.Tests/Piwik.Tracker.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A6F87572-AB3D-477E-A97E-300DFED8CB5C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Piwik.Tracker.Tests</RootNamespace>
<AssemblyName>Piwik.Tracker.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.6.0\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PiwikTrackerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Piwik.Tracker\Piwik.Tracker.csproj">
<Project>{c3bb0e94-f45c-4691-81ff-061fc20e3209}</Project>
<Name>Piwik.Tracker</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
62 changes: 62 additions & 0 deletions Piwik.Tracker.Tests/PiwikTrackerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using NUnit.Framework;

namespace Piwik.Tracker.Tests
{
[TestFixture]
public class PiwikTrackerTests
{
private const string UA = "Firefox";
private static readonly string PiwikBaseUrl = "http://piwik.local";
private static readonly int SiteId = 1;

[Test]
[TestCase(Scopes.Page, null, null, null)]
[TestCase(Scopes.Page, 2, null, null)]
[TestCase(Scopes.Page, 2, "myPageVar", "myPageVarValue")]
[TestCase(Scopes.Event, null, null, null)]
[TestCase(Scopes.Event, 3, null, null)]
[TestCase(Scopes.Event, 3, "myEventVar", "myEventVarValue")]
[TestCase(Scopes.Visit, null, null, null)]
[TestCase(Scopes.Visit, 4, null, null)]
[TestCase(Scopes.Visit, 4, "myVisitVar", "myVisitVarValue")]
public void GetCustomVariable_Test(Scopes variableScope, int? variableId, string variableName, string variableValue)
{
//Arrange
var sut = new PiwikTracker(SiteId, PiwikBaseUrl);
if (variableId != null)
{
sut.SetCustomVariable(variableId.Value, variableName, variableValue, variableScope);
}
//Act

var actual = sut.GetCustomVariable(variableId ?? 99, variableScope);
//Assert
if (variableId != null)
{
Assert.That(actual.Name, Is.EqualTo(variableName));
Assert.That(actual.Value, Is.EqualTo(variableValue));
}
else
{
Assert.That(actual, Is.Null);
}
}

[Test]
public void GetCustomVariable_WhenScopeIsVisit_ReturnsVariableFromCookie()
{
Assert.That(false, Is.True, "Todo: Provide abillity to mock cookie!");
}

[Test]
public void GetCustomVariable_WhenInvalidScopeArgument_Throws()
{
//Arrange
var sut = new PiwikTracker(SiteId, PiwikBaseUrl);

//Act & Assert
Assert.Throws<ArgumentException>(() => sut.GetCustomVariable(1, (Scopes)1234));
}
}
}
36 changes: 36 additions & 0 deletions Piwik.Tracker.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Piwik.Tracker.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Piwik.Tracker.Tests")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a6f87572-ab3d-477e-a97e-300dfed8cb5c")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions Piwik.Tracker.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.6.0" targetFramework="net45" />
</packages>
10 changes: 10 additions & 0 deletions Piwik.Tracker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piwik.Tracker.Samples", "Pi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piwik.Tracker.Web.Samples", "Piwik.Tracker.Web.Samples\Piwik.Tracker.Web.Samples.csproj", "{3D74EB7C-F2A4-49E4-8B2C-8247322F7DE0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piwik.Tracker.Tests", "Piwik.Tracker.Tests\Piwik.Tracker.Tests.csproj", "{A6F87572-AB3D-477E-A97E-300DFED8CB5C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -40,6 +42,14 @@ Global
{3D74EB7C-F2A4-49E4-8B2C-8247322F7DE0}.Release|Any CPU.Build.0 = Release|Any CPU
{3D74EB7C-F2A4-49E4-8B2C-8247322F7DE0}.Release|x86.ActiveCfg = Release|Any CPU
{3D74EB7C-F2A4-49E4-8B2C-8247322F7DE0}.Release|x86.Build.0 = Release|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Debug|x86.ActiveCfg = Debug|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Debug|x86.Build.0 = Debug|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Release|Any CPU.Build.0 = Release|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Release|x86.ActiveCfg = Release|Any CPU
{A6F87572-AB3D-477E-A97E-300DFED8CB5C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
39 changes: 22 additions & 17 deletions Piwik.Tracker/PiwikTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public class PiwikTracker
/// <exception cref="ArgumentException">apiUrl must not be null or empty</exception>
public PiwikTracker(int idSite, string apiUrl)
{
if (string.IsNullOrEmpty(PiwikBaseUrl))
if (string.IsNullOrEmpty(apiUrl))
{
throw new ArgumentException("Piwik api url must not be emty or null.", nameof(apiUrl));
}
Expand Down Expand Up @@ -360,24 +360,29 @@ public void SetCustomTrackingParameter(string trackingApiParameter, string value
public CustomVar GetCustomVariable(int id, Scopes scope = Scopes.Visit)
{
var stringId = Convert.ToString(id);

if (scope.Equals(Scopes.Page))
{
return _pageCustomVar.ContainsKey(stringId) ? new CustomVar(_pageCustomVar[stringId][0], _pageCustomVar[stringId][1]) : null;
}
else if (!scope.Equals(Scopes.Event))
{
return _eventCustomVar.ContainsKey(stringId) ? new CustomVar(_eventCustomVar[stringId][0], _eventCustomVar[stringId][1]) : null;
}
else if (!scope.Equals(Scopes.Visit))
{
throw new ArgumentException("Invalid 'scope' parameter value", nameof(scope));
}
// todo: unreachable code, this will newer be called since we return before!
if (_visitorCustomVar.ContainsKey(stringId))
switch (scope)
{
return new CustomVar(_visitorCustomVar[stringId][0], _visitorCustomVar[stringId][1]);
case Scopes.Page:
return _pageCustomVar.ContainsKey(stringId)
? new CustomVar(_pageCustomVar[stringId][0], _pageCustomVar[stringId][1])
: null;

case Scopes.Event:
return _eventCustomVar.ContainsKey(stringId)
? new CustomVar(_eventCustomVar[stringId][0], _eventCustomVar[stringId][1])
: null;

case Scopes.Visit:
if (_visitorCustomVar.ContainsKey(stringId))
{
return new CustomVar(_visitorCustomVar[stringId][0], _visitorCustomVar[stringId][1]);
}
break;

default:
throw new ArgumentException("Invalid 'scope' parameter value", nameof(scope));
}

var cookieDecoded = GetCustomVariablesFromCookie();
if (!cookieDecoded.ContainsKey(stringId)
|| cookieDecoded[stringId].Count() != 2)
Expand Down

0 comments on commit 2697fe6

Please sign in to comment.