2020-12-05 11:12:55 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-04-04 16:13:28 +02:00
|
|
|
using System;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Builders.Interfaces;
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.Common.Builders;
|
|
|
|
|
|
|
|
|
|
public class PropertyBuilder
|
|
|
|
|
: BuilderBase<IProperty>,
|
|
|
|
|
IBuildPropertyTypes,
|
|
|
|
|
IWithIdBuilder,
|
|
|
|
|
IWithKeyBuilder,
|
|
|
|
|
IWithCreateDateBuilder,
|
|
|
|
|
IWithUpdateDateBuilder
|
2020-04-04 16:13:28 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private DateTime? _createDate;
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private int? _id;
|
|
|
|
|
private Guid? _key;
|
|
|
|
|
private IPropertyType _propertyType;
|
|
|
|
|
private PropertyTypeBuilder<PropertyBuilder> _propertyTypeBuilder;
|
|
|
|
|
private DateTime? _updateDate;
|
2020-10-20 14:49:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
DateTime? IWithCreateDateBuilder.CreateDate
|
|
|
|
|
{
|
|
|
|
|
get => _createDate;
|
|
|
|
|
set => _createDate = value;
|
|
|
|
|
}
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
int? IWithIdBuilder.Id
|
|
|
|
|
{
|
|
|
|
|
get => _id;
|
|
|
|
|
set => _id = value;
|
|
|
|
|
}
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Guid? IWithKeyBuilder.Key
|
|
|
|
|
{
|
|
|
|
|
get => _key;
|
|
|
|
|
set => _key = value;
|
|
|
|
|
}
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
DateTime? IWithUpdateDateBuilder.UpdateDate
|
|
|
|
|
{
|
|
|
|
|
get => _updateDate;
|
|
|
|
|
set => _updateDate = value;
|
|
|
|
|
}
|
2020-10-20 14:49:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public PropertyBuilder WithPropertyType(IPropertyType propertyType)
|
|
|
|
|
{
|
|
|
|
|
_propertyTypeBuilder = null;
|
|
|
|
|
_propertyType = propertyType;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-10-20 14:49:52 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public PropertyTypeBuilder<PropertyBuilder> AddPropertyType()
|
|
|
|
|
{
|
|
|
|
|
_propertyType = null;
|
|
|
|
|
var builder = new PropertyTypeBuilder<PropertyBuilder>(this);
|
|
|
|
|
_propertyTypeBuilder = builder;
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public override IProperty Build()
|
|
|
|
|
{
|
|
|
|
|
var id = _id ?? 1;
|
|
|
|
|
var key = _key ?? Guid.NewGuid();
|
|
|
|
|
var createDate = _createDate ?? DateTime.Now;
|
|
|
|
|
var updateDate = _updateDate ?? DateTime.Now;
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
if (_propertyTypeBuilder is null && _propertyType is null)
|
2020-04-04 16:13:28 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
"A property cannot be constructed without providing a property type. Use AddPropertyType() or WithPropertyType().");
|
2020-04-04 16:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var propertyType = _propertyType ?? _propertyTypeBuilder.Build();
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// Needs to be within collection to support publishing.
|
|
|
|
|
var propertyTypeCollection = new PropertyTypeCollection(true, new[] { propertyType });
|
2020-04-04 16:13:28 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return new Property(id, propertyTypeCollection[0])
|
2020-04-04 16:13:28 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
Key = key,
|
|
|
|
|
CreateDate = createDate,
|
|
|
|
|
UpdateDate = updateDate
|
|
|
|
|
};
|
2020-04-04 16:13:28 +02:00
|
|
|
}
|
|
|
|
|
}
|