From 3ad1668a2101bacabc2e8c4d049b374e4784a10d Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 28 May 2008 16:06:52 +0000 Subject: [PATCH] Promote the getAllProducts to Asset.pm as getIsa. Move the tests from Asset/Sku/Product.t into Asset/Asset.t, and add tests to check that inheritance is respected. --- lib/WebGUI/Asset.pm | 34 ++++++++++++++++++++++++++++ lib/WebGUI/Asset/Sku/Product.pm | 32 -------------------------- t/Asset/Asset.t | 40 ++++++++++++++++++++++++++++++++- t/Asset/Sku/Product.t | 19 ---------------- 4 files changed, 73 insertions(+), 52 deletions(-) diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index dfcfeac45..f61358ace 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -1058,6 +1058,40 @@ sub getImportNode { +#------------------------------------------------------------------- + +=head2 getIsa ( $session ) + +A class method to return an iterator for getting all Assets by class (and all sub-classes) +as Asset objects, one at a time. When the end of the assets is reached, then the iterator +will close the database handle that it uses and return undef. + +It should be used like this: + +my $productIterator = WebGUI::Asset::Product->getIsa($session); +while (my $product = $productIterator->()) { + ##Do something useful with $product +} + +=cut + +sub getIsa { + my $class = shift; + my $session = shift; + my $def = $class->definition($session); + my $tableName = $def->[0]->{tableName}; + my $sth = $session->db->read("select distinct(assetId) from $tableName"); + return sub { + my ($assetId) = $sth->array; + if (!$assetId) { + $sth->finish; + return undef; + } + return WebGUI::Asset->newPending($session, $assetId); + }; +} + + #------------------------------------------------------------------- =head2 getMedia ( session ) diff --git a/lib/WebGUI/Asset/Sku/Product.pm b/lib/WebGUI/Asset/Sku/Product.pm index eaec11507..0e6227c32 100644 --- a/lib/WebGUI/Asset/Sku/Product.pm +++ b/lib/WebGUI/Asset/Sku/Product.pm @@ -272,38 +272,6 @@ sub getAllCollateral { } -#------------------------------------------------------------------- - -=head2 getAllProducts ( $session ) - -A class method to return an iterator for getting all Products (and Product sub-classes) -as Asset objects, one at a time. The iterator will return undef when you've reached -the end of the list of products. - -It should be used like this: - -my $productIterator = WebGUI::Asset::Product->getAllProducts($session); -while (my $product = $productIterator->()) { - ##Do something useful with $product -} - -=cut - -sub getAllProducts { - my $class = shift; - my $session = shift; - my $sth = $session->db->read('select distinct(assetId) from Product'); - return sub { - my ($assetId) = $sth->array; - if (!$assetId) { - $sth->finish; - return undef; - } - return WebGUI::Asset->newPending($session, $assetId); - }; -} - - #------------------------------------------------------------------- =head2 getCollateral ( tableName, keyName, keyValue ) diff --git a/t/Asset/Asset.t b/t/Asset/Asset.t index 754a27e21..c6ef2e5f2 100644 --- a/t/Asset/Asset.t +++ b/t/Asset/Asset.t @@ -19,6 +19,8 @@ use WebGUI::Asset; use WebGUI::User; use WebGUI::Asset::Wobject::Navigation; use WebGUI::Asset::Wobject::Folder; +use WebGUI::Asset::Sku; +use WebGUI::Asset::Sku::Product; use WebGUI::AssetVersioning; use WebGUI::VersionTag; @@ -144,7 +146,7 @@ $canViewMaker->prepare( }, ); -plan tests => 89 +plan tests => 94 + scalar(@fixIdTests) + scalar(@fixTitleTests) + 2*scalar(@getTitleTests) #same tests used for getTitle and getMenuTitle @@ -701,6 +703,42 @@ diag $assetProps->{title}; isnt( $rootAsset->get('title'), $funkyTitle, 'get returns a safe copy of the Asset properties'); +################################################################ +# +# getIsa +# +################################################################ +my $node = WebGUI::Asset::Sku::Product->getProductImportNode($session); +my $product1 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); +my $product2 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); +my $product3 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); + +my $getAProduct = WebGUI::Asset::Sku::Product->getIsa($session); +isa_ok($getAProduct, 'CODE', 'getIsa returns a sub ref'); +my $counter = 0; +my $productIds = []; +while( my $product = $getAProduct->()) { + ++$counter; + push @{ $productIds }, $product->getId; +} +is($counter, 3, 'getIsa: returned only 3 Products'); +cmp_bag($productIds, [$product1->getId, $product2->getId, $product3->getId], 'getIsa returned the correct 3 products'); + +my $getASku = WebGUI::Asset::Sku->getIsa($session); +$counter = 0; +my $skuIds = []; +while( my $sku = $getASku->()) { + ++$counter; + push @{ $skuIds }, $sku->getId; +} +is($counter, 3, 'getIsa: returned only 3 Products for a parent class'); +cmp_bag($skuIds, [$product1->getId, $product2->getId, $product3->getId], 'getIsa returned the correct 3 products for a parent class'); + +$product1->purge; +$product2->purge; +$product3->purge; + + END { $session->config->set( 'extrasURL', $origExtras); $session->config->set( 'uploadsURL', $origUploads); diff --git a/t/Asset/Sku/Product.t b/t/Asset/Sku/Product.t index c79604916..e51f7ab0c 100644 --- a/t/Asset/Sku/Product.t +++ b/t/Asset/Sku/Product.t @@ -41,25 +41,6 @@ my $node = WebGUI::Asset::Sku::Product->getProductImportNode($session); isa_ok($node, 'WebGUI::Asset::Wobject::Folder', 'getProductImportNode returns a Folder'); is($node->getId, 'PBproductimportnode001', 'Product Import Node has the correct GUID'); -my $product1 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); -my $product2 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); -my $product3 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); - -my $getAProduct = WebGUI::Asset::Sku::Product->getAllProducts($session); -isa_ok($getAProduct, 'CODE', 'getAllProducts returns a sub ref'); -my $counter = 0; -my $productIds = []; -while( my $product = $getAProduct->()) { - ++$counter; - push @{ $productIds }, $product->getId; -} -is($counter, 3, 'getAllProducts: returned only 3 Products'); -cmp_bag($productIds, [$product1->getId, $product2->getId, $product3->getId], 'getAllProduct returned the correct 3 products'); - -$product1->purge; -$product2->purge; -$product3->purge; - #---------------------------------------------------------------------------- # Cleanup END {