Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Namespace Loaders & Models for PSR-4
  • Loading branch information
AntonyThorpe committed Jun 5, 2024
1 parent 94a0a38 commit bc12538
Show file tree
Hide file tree
Showing 22 changed files with 65 additions and 85 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"autoload": {
"psr-4": {
"AntonyThorpe\\Consumer\\": "src/",
"AntonyThorpe\\Consumer\\Tests\\": "tests/"
"AntonyThorpe\\Consumer\\Tests\\": "tests/",
"AntonyThorpe\\Consumer\\Tests\\Loader\\": "tests/Loader/",
"AntonyThorpe\\Consumer\\Tests\\Model\\": "tests/Model/"
}
},
"extra": {
Expand Down
38 changes: 18 additions & 20 deletions src/BulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ class BulkLoader extends \SilverStripe\Dev\BulkLoader

/**
* Add new records while importing
* @var Boolean
* @var bool
*/
protected $addNewRecords = true;

/**
* The default behaviour for linking relations
* @var boolean
* @var bool
*/
protected $relationLinkDefault = true;

/**
* The default behaviour creating relations
* @var boolean
* @var bool
*/
protected $relationCreateDefault = true;

/**
* Determines whether pages should be published during loading
* @var boolean
* @var bool
*/
protected $publishPages = true;

Expand Down Expand Up @@ -99,7 +99,7 @@ public function getSource()

/**
* Set the default behaviour for linking existing relation objects.
* @param boolean $default
* @param bool $default
* @return \AntonyThorpe\Consumer\BulkLoader
*/
public function setRelationLinkDefault($default)
Expand All @@ -110,7 +110,7 @@ public function setRelationLinkDefault($default)

/**
* Set the default behaviour for creating new relation objects.
* @param boolean $default
* @param bool $default
* @return \AntonyThorpe\Consumer\BulkLoader
*/
public function setRelationCreateDefault($default)
Expand All @@ -121,7 +121,7 @@ public function setRelationCreateDefault($default)

/**
* Set pages to published upon upload
* @param boolean $default
* @param bool $default
* @return \AntonyThorpe\Consumer\BulkLoader
*/
public function setPublishPages($dopubilsh)
Expand Down Expand Up @@ -165,7 +165,7 @@ public function preview($filepath)
/**
* Start loading of data
* @param string $filepath
* @param boolean $preview Create results but don't write
* @param bool $preview Create results but don't write
* @return BulkLoaderResult
*/
public function load($filepath = null, $preview = false)
Expand All @@ -180,7 +180,7 @@ public function load($filepath = null, $preview = false)
/**
* Import all records from the source
* @param string $filepath
* @param boolean $preview
* @param bool $preview
* @return BulkLoaderResult
*/
protected function processAll($filepath, $preview = false)
Expand All @@ -205,7 +205,7 @@ protected function processAll($filepath, $preview = false)
* Process a single record from source
* @param object $record
* @param array $columnMap
* @param boolean $preview
* @param bool $preview
* @param BulkLoaderResult $results
* @param string $preview set to true to prevent writing to the dataobject
* @return BulkLoaderResult
Expand Down Expand Up @@ -336,7 +336,7 @@ protected function columnMapRecord($record)
/**
* Check if the given mapped record has the required data.
* @param array $mappedrecord
* @return boolean
* @return bool
*/
protected function hasRequiredData($mappedrecord)
{
Expand Down Expand Up @@ -378,7 +378,7 @@ protected function transformField($placeholder, $field, mixed $value)
$relationName = $field;
}
//get the list that relation is added to/checked on
$relationlist = isset($this->transforms[$field]['list']) ? $this->transforms[$field]['list'] : null;
$relationlist = $this->transforms[$field]['list'] ?? null;

//check for the same relation set on the current record
if ($placeholder->{$relationName."ID"}) {
Expand Down Expand Up @@ -452,7 +452,7 @@ protected function transformField($placeholder, $field, mixed $value)
/**
* Detect if a given record field is a relation field.
* @param string $field
* @return boolean
* @return bool
*/
protected function isRelation($field)
{
Expand Down Expand Up @@ -556,7 +556,7 @@ public function getMappableColumns()

/**
* Generate a field-label list of fields that data can be mapped into.
* @param boolean $includerelations
* @param bool $includerelations
* @return array
*/
public function scaffoldMappableFields($includerelations = true)
Expand Down Expand Up @@ -634,7 +634,7 @@ public function preprocessChecks(): void
* Update the dataobject with data from the external API
*
* @param array $apidata An array of arrays
* @param boolean $preview Set to true to not write
* @param bool $preview Set to true to not write
* @return BulkLoaderResult
*/
public function updateRecords(array $apidata, $preview = false)
Expand All @@ -648,7 +648,7 @@ public function updateRecords(array $apidata, $preview = false)
/**
* Update/create many dataobjects with data from the external api
*
* @param boolean $preview Set to true to not write
* @param bool $preview Set to true to not write
* @return BulkLoaderResult
*/
public function upsertManyRecords(array $apidata, $preview = false)
Expand All @@ -661,7 +661,7 @@ public function upsertManyRecords(array $apidata, $preview = false)
/**
* Delete dataobjects that match to the API data
*
* @param boolean $preview Set to true to not write
* @param bool $preview Set to true to not write
* @return BulkLoaderResult
*/
public function deleteManyRecords(array $apidata, $preview = false)
Expand Down Expand Up @@ -709,7 +709,7 @@ public function deleteManyRecords(array $apidata, $preview = false)
* @param array $apidata An array of arrays
* @param string $key The key that matches the column within the Object Class
* @param string $property_name The property name of the class that matches to the key from the API data
* @param boolean $preview Set to true to not save
* @param bool $preview Set to true to not save
* @return BulkLoaderResult
*/
public function clearAbsentRecords(array $apidata, $key, $property_name, $preview = false)
Expand All @@ -719,13 +719,11 @@ public function clearAbsentRecords(array $apidata, $key, $property_name, $previe

foreach ($modelClass::get() as $record) {
$property_value = $record->{$property_name};

if (!empty($property_value)) {
$match = array_filter(
$apidata,
fn($value): bool => $value[$key] == $property_value
);

if ($match === []) { // The property value doesn't exist in the API data (therefore clear)
$record->{$property_name} = "";
$results->addUpdated($record, _t('Consumer.CLEARABSENT', 'Clear absent'), $this->duplicateChecks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use SilverStripe\Dev\SapphireTest;
use AntonyThorpe\Consumer\ArrayBulkLoaderSource;

class ConsumerArrayBulkLoaderSourceTest extends SapphireTest
class ArrayBulkLoaderSourceTest extends SapphireTest
{
public function testIterator(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Dev\SapphireTest;
use AntonyThorpe\Consumer\Tests\User;
use AntonyThorpe\Consumer\Tests\UserBulkLoader;
use AntonyThorpe\Consumer\Tests\Loader\UserBulkLoader;
use AntonyThorpe\Consumer\Tests\Model\User;

class BulkLoaderClearAbsentRecordsTest extends SapphireTest
{
protected static $fixture_file = ['fixtures/User.yml'];
protected static $fixture_file = ['Fixtures/User.yml'];

protected $usesDatabase = true;

protected static $extra_dataobjects = [User::class];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace AntonyThorpe\Consumer\Tests;

use SilverStripe\Dev\SapphireTest;
use AntonyThorpe\Consumer\Tests\User;
use AntonyThorpe\Consumer\Tests\Loader\UserBulkLoader;
use AntonyThorpe\Consumer\Tests\Model\User;

class BulkLoaderDeleteManyRecordsTest extends SapphireTest
{
protected static $fixture_file = ['fixtures/User.yml'];
protected static $fixture_file = ['Fixtures/User.yml'];

protected static $extra_dataobjects = [User::class];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace AntonyThorpe\Consumer\Tests;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ArrayList;
use AntonyThorpe\Consumer\BulkLoader;
use AntonyThorpe\Consumer\ArrayBulkLoaderSource;
use AntonyThorpe\Consumer\Tests\CourseSelection;
use AntonyThorpe\Consumer\Tests\Course;
use AntonyThorpe\Consumer\Tests\Model\CourseSelection;
use AntonyThorpe\Consumer\Tests\Model\Course;

class BulkLoaderRelationTest extends SapphireTest
{
protected static $fixture_file = 'fixtures/BulkLoaderRelationTest.yml';
protected static $fixture_file = 'Fixtures/BulkLoaderRelationTest.yml';

protected static $extra_dataobjects = [
Course::class,
Expand Down
6 changes: 3 additions & 3 deletions tests/ConsumerBulkLoaderTest.php → tests/BulkLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use SilverStripe\Dev\SapphireTest;
use AntonyThorpe\Consumer\BulkLoader;
use AntonyThorpe\Consumer\ArrayBulkLoaderSource;
use AntonyThorpe\Consumer\Tests\Person;
use AntonyThorpe\Consumer\Tests\Country;
use AntonyThorpe\Consumer\Tests\Model\Person;
use AntonyThorpe\Consumer\Tests\Model\Country;

class BulkLoaderTest extends SapphireTest
{
protected static $fixture_file = 'fixtures/BulkLoaderTest.yml';
protected static $fixture_file = 'Fixtures/BulkLoaderTest.yml';

protected static $extra_dataobjects = [
Person::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

use SilverStripe\Core\Convert;
use SilverStripe\Dev\SapphireTest;
use AntonyThorpe\Consumer\Tests\User;
use AntonyThorpe\Consumer\Tests\Product;
use AntonyThorpe\Consumer\Tests\Loader\ProductBulkLoader;
use AntonyThorpe\Consumer\Tests\Loader\UserBulkLoader;
use AntonyThorpe\Consumer\Tests\Model\User;
use AntonyThorpe\Consumer\Tests\Model\Product;

class BulkLoaderUpdateRecordsTest extends SapphireTest
{
protected static $fixture_file = [
'fixtures/User.yml',
'fixtures/Product.yml'
'Fixtures/User.yml',
'Fixtures/Product.yml'
];

protected static $extra_dataobjects = [
Expand All @@ -24,7 +26,8 @@ public function setUp(): void
parent::setUp();

//publish some product categories and products so as to test the Live version
$this->objFromFixture(Product::class, 'products')->copyVersionToStage('Stage', 'Live');
$test = $this->objFromFixture(Product::class, 'products');
$test->copyVersionToStage('Stage', 'Live');
$this->objFromFixture(Product::class, 'pm1')->copyVersionToStage('Stage', 'Live');
$this->objFromFixture(Product::class, 'pm2')->copyVersionToStage('Stage', 'Live');
$this->objFromFixture(Product::class, 'pm3')->copyVersionToStage('Stage', 'Live');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace AntonyThorpe\Consumer\Tests;

use SilverStripe\Dev\SapphireTest;
use AntonyThorpe\Consumer\Tests\User;
use AntonyThorpe\Consumer\Tests\Loader\UserBulkLoader;
use AntonyThorpe\Consumer\Tests\Model\User;

class BulkLoaderUpsertManyRecordsTest extends SapphireTest
{
protected static $fixture_file = ['fixtures/User.yml'];
protected static $fixture_file = ['Fixtures/User.yml'];

protected static $extra_dataobjects = [User::class];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Loader;

use SilverStripe\Dev\TestOnly;
use AntonyThorpe\Consumer\BulkLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Loader;

use SilverStripe\Dev\TestOnly;
use AntonyThorpe\Consumer\BulkLoader;
Expand Down
2 changes: 1 addition & 1 deletion tests/models/Country.php → tests/Model/Country.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Model;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
Expand Down
2 changes: 1 addition & 1 deletion tests/models/Course.php → tests/Model/Course.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Model;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Model;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
Expand Down
2 changes: 1 addition & 1 deletion tests/models/Person.php → tests/Model/Person.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Model;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
Expand Down
3 changes: 2 additions & 1 deletion tests/models/Product.php → tests/Model/Product.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Model;

use Page;
use SilverStripe\Dev\TestOnly;

class Product extends Page implements TestOnly
Expand Down
2 changes: 1 addition & 1 deletion tests/models/User.php → tests/Model/User.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace AntonyThorpe\Consumer\Tests;
namespace AntonyThorpe\Consumer\Tests\Model;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/BulkLoaderRelationTest.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
AntonyThorpe\Consumer\Tests\Course:
AntonyThorpe\Consumer\Tests\Model\Course:
math101:
Title: 'Math 101'
tech102:
Title: 'Tech 102'
mus103:
Title: 'Music 103'

AntonyThorpe\Consumer\Tests\CourseSelection:
AntonyThorpe\Consumer\Tests\Model\CourseSelection:
tech102:
Course: =>AntonyThorpe\Consumer\Tests\Course.tech102
Course: =>AntonyThorpe\Consumer\Tests\Model\Course.tech102
Term: 3
2 changes: 1 addition & 1 deletion tests/fixtures/BulkLoaderTest.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AntonyThorpe\Consumer\Tests\Country:
AntonyThorpe\Consumer\Tests\Model\Country:
nz:
Title: New Zealand
Code: NZ
Expand Down
Loading

0 comments on commit bc12538

Please sign in to comment.